0

这里我有以下 JSON 数据,使用它我必须创建图表视图。

 Ext.data.JsonP.callback12({"totalCount":0,"success":true,"message":"Successfully retrieved data for report #1",
"content": {
"fields":["name","2014","2013","2012"],
"data":[{"name":"1","2012":208.95,"2013":229.92,"2014":0},
{"name":"2","2013":265.92,"2014":0,"2012":0},
{"name":"3","2012":227.98,"2013":558.13,"2014":0},
{"name":"4","2012":390,"2013":282.09,"2014":0},
{"name":"5","2013":461.1},
{"name":"6","2012":396.8,"2013":427.2,"2014":0},
{"name":"7","2012":305.48,"2013":110.76,"2014":0},
{"name":"8","2012":379.35,"2013":428.46,"2014":0},
{"name":"9","2012":206.5,"2013":535.35,"2014":0},
{"name":"10","2012":376,"2013":168.51,"2014":0},
{"name":"11","2012":275.28,"2013":231.93,"2014":0},
{"name":"12","2012":195.75,"2013":340.94,"2014":0}]}})

使用上面的 JSON,我正在尝试绘制图表,现在的问题是,如果我在商店中提供 rootProperty: 'content.data' 并在我的商店中添加静态字段(字段:[2014,2013,2012]),图表工作正常看法。但我希望我的字段从提供 rootProperty: 'content' 的商店动态添加,以便我可以在图表(轴和系列)中使用字段和数据。我正在添加我的图表视图。请帮助了解如何将上述字段和数据添加到我的图表中。

看法

Ext.define('Sample.view.ChartsView', {
   extend: 'Ext.Panel',
   xtype: 'myreportsview',

  requires: ['Ext.chart.axis.Numeric', 'Ext.data.JsonStore', 'Ext.chart.CartesianChart'],
  config: {
    title: 'Reports',
    iconCls: 'icon-stats',
    layout: 'fit',
    fullscreen: true,
    items: [
        {
            xtype: 'chart',
            style: 'background:#fff',
            store: 'YearlyReports',
            animate: true,
            theme: 'category1',
            legend: {
                position: 'bottom'
            },
            axes: [
                {
                    type: 'numeric',
                    position: 'left',
                    fields: ['2012', '2013', '2014'], -- these fields should be added from store
                    title: 'Purchases',
                    minorTickSteps: 1,
                    grid: {
                        odd: {
                            opacity: 1,
                            fill: '#ddd',
                        }
                    },
                    style: {
                        axisLine: false,
                        estStepSize: 20,
                        stroke: '#ddd',
                        'stroke-width': 0.5
                    },

                }, 
                {
                    type: 'category',
                    position: 'bottom',
                    fields: ['name'],
                    title: 'Month of the Year',
                    style: {
                        estStepSize: 1,
                        stroke: '#999'
                    }
                }
            ],
            series: [
                {
                    type: 'line',
                    xField: 'name',
                    yField: '2012',
                    highlightCfg: {
                        scale: 7
                    },
                    axis: 'left',
                    style: {
                        smooth: true,
                        stroke: '#94ae0a',
                        lineWidth: 3,
                        shadowColor: 'rgba(0,0,0,0.7)',
                        shadowBlur: 10,
                        shadowOffsetX: 3,
                        shadowOffsetY: 3
                    },
                    marker: {
                        type: 'circle',
                        stroke: '#94ae0a',
                        fill: '#94ae0a',
                        lineWidth: 2,
                        radius: 4,
                        shadowColor: 'rgba(0,0,0,0.7)',
                        shadowBlur: 10,
                        shadowOffsetX: 3,
                        shadowOffsetY: 3,
                        fx: {
                            duration: 300
                        }
                    }
                }, 
                {
                    type: 'line',
                    smooth: true,
                    xField: 'name',
                    yField: '2013',
                    highlightCfg: {
                        scale: 7
                    },
                    axis: 'left',
                    style: {
                        stroke: '#a61120',
                        lineWidth: 3,
                        shadowColor: 'rgba(0,0,0,0.7)',
                        shadowBlur: 10,
                        shadowOffsetX: 3,
                        shadowOffsetY: 3
                    },
                    marker: {
                        type: 'circle',
                        stroke: '#a61120',
                        fill: '#a61120',
                        lineWidth: 2,
                        radius: 4,
                        shadowColor: 'rgba(0,0,0,0.7)',
                        shadowBlur: 10,
                        shadowOffsetX: 3,
                        shadowOffsetY: 3,
                        fx: {
                            duration: 300
                        }
                    }
                }, 
                {
                    type: 'line',
                    smooth: true,
                    xField: 'name',
                    yField: '2014',
                    highlightCfg: {
                        scale: 7
                    },
                    axis: 'left',
                    style: {
                        fill: "#115fa6",
                        stroke: "#115fa6",
                        fillOpacity: 0.6,
                        miterLimit: 3,
                        lineCap: 'miter',
                        lineWidth: 2
                    },
                    marker: {
                        type: 'circle',
                        stroke: '#0d1f96',
                        fill: '#115fa6',
                        lineWidth: 2,
                        radius: 4,
                        shadowColor: 'rgba(0,0,0,0.7)',
                        shadowBlur: 10,
                        shadowOffsetX: 3,
                        shadowOffsetY: 3,
                    }
                }
            ]
        }
    ]
}
});
4

1 回答 1

0

这可能对那些想要动态添加图表的人有用。使用上述数据创建了商店('YearlyReports')。

面板中的图表

           {
            xtype: 'chart',
            id: 'yearlyChart',
            style: 'background:#fff',
            store: {},
            animate: true,
            theme: 'category1',
            legend: {
                position: 'bottom'
            }

在 config-listeners 中编写了以下绘制函数

       listeners: {
        painted: function() {
            var store = Ext.getStore('YearlyReports').getAt(0).data,
                chart = Ext.getCmp('yearlyChart'),
                seriesArray = new Array(),
                axesArray = new Array(),
                fields = new Array(),
                lineColors = ['#115fa6','#94ae0a','#a61120'],
                markerColors = ['#94ae0a', '#a61120', '#115fa6'];

            for(var j=1;j<store.fields.length;j++) {

                fields.push(store.fields[j]);
                seriesArray.push(
                    new Ext.chart.series.Line({
                        type: 'line',
                        yField: [store.fields[j]],
                        xField: 'name',
                        stacked: false,
                        style: {
                            smooth: true,
                            stroke: lineColors[j-1],
                            lineWidth: 3,
                            shadowColor: 'rgba(0,0,0,0.7)',
                            shadowBlur: 10,
                            shadowOffsetX: 3,
                            shadowOffsetY: 3
                        },
                        marker: {
                            type: 'circle',
                            stroke: markerColors[j-1],
                            fill: markerColors[j-1],
                            lineWidth: 2,
                            radius: 4,
                            shadowColor: 'rgba(0,0,0,0.7)',
                            shadowBlur: 10,
                            shadowOffsetX: 3,
                            shadowOffsetY: 3,
                            fx: {
                                 duration: 300
                            }
                         }
                }));
            }
            axesArray.push(
                new Ext.chart.axis.Numeric({   
                    type: 'numeric',
                    position: 'left',
                    fields: fields,
                    title: 'Purchases',
                    minorTickSteps: 1,
                    grid: {
                        odd: {
                            opacity: 1,
                            fill: '#ddd',
                        }
                    },
                    style: {
                        axisLine: false,
                        estStepSize: 20,
                        stroke: '#ddd',
                        'stroke-width': 0.5
                    },
                })
            );

            axesArray.push(
                new Ext.chart.axis.Category({
                    type: 'category',
                    position: 'bottom',
                    fields: ['name'],
                    title: 'Month of the Year',
                    style: {
                        estStepSize: 1,
                        stroke: '#999'
                    }
                })
            );
            var data = {"data" : store.data};
            chart.setStore(data);
            chart.setAxes(axesArray);
            chart.setSeries(seriesArray);
        }
    },
于 2014-01-21T19:56:45.353 回答