动态更改图表系列extjs 4

时间:2013-09-27 14:56:17

标签: javascript json extjs charts extjs-mvc

我正在使用Extjs 4和MVC架构。

我有一个输出这个Json数据的python脚本:

{
"data": [
    {
        "inAnalysis": 3, 
        "inQuest": 2, 
        "inDevelopment": 6, 
        "total": 12, 
        "inValidation": 1, 
        "Month": 1303
    }, 
    {
        "inAnalysis": 1, 
        "total": 5, 
        "Month": 1304, 
        "inDevelopment": 4
    }
], 
"success": true, 
"metaData": {
    "fields": [
        {
            "name": "inAnalysis"
        }, 
        {
            "name": "inQuest"
        }, 
        {
            "name": "inDevelopment"
        }, 
        {
            "name": "inValidation"
        }, 
        {
            "name": "isDuplicate"
        }, 
        {
            "name": "New"
        }, 
        {
            "name": "total"
        }
    ], 
    "root": "data"
}

}

我想将MetaData的字段用作图表系列,所以我有这样的商店:

Ext.define('Proj.store.ChartData', {
extend: 'Ext.data.Store',
autoload: true,
proxy: {
    type: 'ajax',
    url : 'data/getParams.py',
    reader: new Ext.data.JsonReader({
        fields:[]
    }),
    root: 'data'  
}

为了在图表中添加系列,我做了这个:

var chart = Ext.widget('drawchart');
var fields = [];

chartStore.each(function (field) {
    fields.push(Ext.create('Ext.data.Field', {
        name: field.get('name')
    }));
});
chartModel.prototype.fields.removeAll();
chartModel.prototype.fields.addAll(fields);

var series = [];
for (var i = 1; i < fields.length; i++) {
    var newSeries = new Ext.chart.BarSeries({
        type: 'column',
        displayName: fields[i].name,
        xField: ['Month'],
        yField: fields[i].name,
        style: {
            mode: 'stretch',
            color: this.chartColors[i + 1]
        }
    });
    series.push(newSeries);
    chart.series = series;
};

chart.bindStore(chartStore);
chart.redraw();
chart.refresh();

但它没有用,我认为fields数组总是空的... 任何帮助将不胜感激:

2 个答案:

答案 0 :(得分:7)

交换或重新加载商店很容易,但是您很难重新配置轴和系列后续... Ext图表不支持。可以替换myChart.axes集合中的轴,对于系列也是如此,然后仔细研究代码,替换删除现有的精灵等等。然而,这是一个傻瓜&#39;因为,一旦你的代码对Ext的图表代码(将要发生)的未来演变非常脆弱,其次是一个更容易和可靠的解决方案。那就是创建一个新图表,删除旧图表,将新图表放在原位,然后坐下!用户不会看到差异。

您没有提供有关代码的大量信息,因此我将使用Bar chart example解决方案。

首先,您需要修理您的商店:

Ext.define('Proj.store.ChartData', {
    extend: 'Ext.data.Store',
    //autoload: true,
    autoLoad: true, // there was a type in there
    fields: [], // was missing
    proxy: {
        type: 'ajax',
        url : 'data/getParams.py',
        // better to inline the proxy (lazy init)
        reader: {
            type: 'json'
            ,root: 'data' // and root is an option of the reader, not the proxy
        }
//      reader: new Ext.data.JsonReader({
//          fields:[]
//      }),
//      root: 'data'
    }
});

然后,让我们稍微丰富您的回答,以便将之前客户端的模型知识最小化为无。我已将totalFieldcategoryField添加到metaData节点,我们将其用于轴和系列:

{
    "data": [
        {
            "inAnalysis": 3,
            "inQuest": 2,
            "inDevelopment": 6,
            "total": 12,
            "inValidation": 1,
            "Month": 1303
        },
        {
            "inAnalysis": 1,
            "total": 5,
            "Month": 1304,
            "inDevelopment": 4
        }
    ],
    "success": true,
    "metaData": {
        "totalField": "total",
        "categoryField": "Month",
        "fields": [
            {
                "name": "Month"
            },
            {
                "name": "inAnalysis"
            },
            {
                "name": "inQuest"
            },
            {
                "name": "inDevelopment"
            },
            {
                "name": "inValidation"
            },
            {
                "name": "isDuplicate"
            },
            {
                "name": "New"
            },
            {
                "name": "total"
            }
        ],
        "root": "data"
    }
}

请注意,代理会自动捕获响应中的metaData并相应地重新配置其商店(隐式)模型...所以您不需要您的gloubiboulga来执行此操作你自己。值得注意的是,读者将在其rawData属性中保留原始响应数据的副本;这对于获取我们已添加的自定义信息非常有用。

现在我们有一个适当的商店,会收到详细的回复,让我们使用它:

new Proj.store.ChartData({
    listeners: {
        load: replaceChart
    }
});

这将触发replaceChart方法,该方法将根据服务器提供的元数据和数据创建一个全新的图表,并销毁并替换旧的图表。这是功能:

function replaceChart(chartStore) {

    // Grab the name of the total & category fields as instructed by the server
    var meta = chartStore.getProxy().getReader().rawData.metaData,
        totalField = meta.totalField,
        categoryField = meta.categoryField;

    // Build a list of all field names, excluding the total & category ones
    var fields = Ext.Array.filter(
        Ext.pluck(chartStore.model.getFields(), 'name'),
        function(field) {
            return field !== categoryField && field !== totalField;
        }
    );

    // Create a pimping new chat like you like
    var chart = Ext.create('Ext.chart.Chart', {
        store: chartStore,
        legend: true,
        axes: [{
            type: 'Numeric',
            position: 'bottom',
            fields: [totalField]
        }, {
            type: 'Category',
            position: 'left',
            fields: [categoryField]
        }],
        series: [{
            type: 'bar',
            axis: 'bottom',
            label: {
                display: 'insideEnd',
                field: fields
            },
            xField: categoryField,
            yField: fields,
            stacked: true // or not... like you want!
        }]
    });

    // Put it in the exact same place as the old one, that will trigger
    // a refresh of the layout and a render of the chart
    var oldChart = win.down('chart'),
        oldIndex = win.items.indexOf(oldChart);
    win.remove(oldChart);
    win.insert(oldIndex, chart);

    // Mission complete.
}

答案 1 :(得分:0)

尝试清除未使用系列的行缓存:

Ext.Array.each(chart.series.items, function(item){
            if(!item.items.length){
                item.line = null;
            }
        });