简体   繁体   English

ExtJS 5饼图无法使用远程存储呈现

[英]ExtJS 5 Pie Chart Not Rendering Using Remote Store

I have a basic pie chart in ExtJS 5. The issue I am having is that the chart renders with a static JsonStore but won't render properly with a remote data.store? 我在ExtJS 5中有一个基本的饼图。我遇到的问题是,该图表使用静态JsonStore呈现,但不能使用远程data.store正确呈现?

Here is my code: 这是我的代码:

View (Chart) 查看(图表)

Ext.define('APP.view.core.graphs.Countytotals', {
    extend: 'Ext.Panel',
    alias: 'widget.countytotalchart',
    id: 'countyTotalsGraph',

    width: 650,

    initComponent: function() {
        var me = this;

        //  Doesn't work?
        var countyStore = Ext.create('APP.store.Countytotalsgraph');


        // Works
        var store = Ext.create('Ext.data.JsonStore', {
            fields: ['COUNTY', 'AMOUNT'],
            data: [{
                'COUNTY': 'London',
                'AMOUNT': 10.92
            }, {
                'COUNTY': 'Lancashire',
                'AMOUNT': 6.61
            }, {
                'COUNTY': 'Kent',
                'AMOUNT': 5.26
            }, {
                'COUNTY': 'West Yorkshire',
                'AMOUNT': 4.52
            }, {
                'COUNTY': 'Nottinghamshire',
                'AMOUNT': 4.01
            }, {
                'COUNTY': 'Other',
                'AMOUNT': 68.68
            }]
        });

        var chart = new Ext.chart.PolarChart({
            width: '100%',
            height: 500,
            insetPadding: 50,
            innerPadding: 20,
            legend: {
                docked: 'bottom'
            },
            listeners: {
              afterrender: function (chart) {
                  if (chart.isVisible()) {
                      countyStore.load();
                      chart.redraw();
                  }
              }
            },
            interactions: ['itemhighlight'],
            store: countyStore,
            series: [{
                type: 'pie',
                angleField: 'AMOUNT',
                label: {
                    field: 'COUNTY',
                    display: 'outside',
                    calloutLine: {
                        length: 60,
                        width: 3
                        // specifying 'color' is also possible here
                    }
                },
                highlight: true,
                tooltip: {
                    trackMouse: true,
                    renderer: function(storeItem, item) {
                        this.setHtml(storeItem.get('COUNTY') + ': ' + storeItem.get('AMOUNT') + '%');
                    }
                }
            }]
        });


        me.items = [chart];

        this.callParent();
    }
});

Store 商店

Ext.define('APP.store.Countytotalsgraph', {
    extend: 'Ext.data.Store',
    model: 'APP.model.Countytotalsgraph',
    autoLoad: false,
    storeId: 'countyTotalsGraphStore',

    proxy: {
        type: 'ajax',
        url : '/dashboard/countytotals',
        method : 'POST',
        reader: {
            type: 'json',
            rootProperty: 'data'
        }
    },

    listeners: {
        beforeload: function(store, eOpts) {
            //if ( this.data.items.length ) {
            //Ext.getCmp('optionsGrid').getView().refresh();
            //}
            store.proxy.extraParams = {
                percentage: 'true'
            }
        }
    }
});

Model 模型

Ext.define('APP.model.Countytotalsgraph', {
    extend: 'Ext.data.Model',
    fields: ['COUNTY', 'AMOUNT']
});

This is how is renders with the static store: 这是静态存储的渲染方式:

使用静态JsonStore

This is how it renders with the remote store: 它是如何使用远程存储呈现的:

与远程Data.Store

I am on the latest version of the GPL although the charts were built using Sencha CMD and the "sencha ant build" command in the sencha-charts directory. 尽管图表是使用Sencha CMD和sencha-charts目录中的“ sencha ant build”命令构建的,但我使用的是GPL的最新版本。

Why does the static store display it (well still there is still an issue regarding the legend at the bottom) but the remote json not? 为什么静态存储显示它(仍然在底部存在关于图例的问题),但是远程json没有显示呢?

Iv'e tried to load the store after it the chart is rendered and is visible as I have seen a previous post regarding holding off on loading the store to give the chart time to render but this did not work: 我试图在渲染图表后加载商店,并且可见,因为我之前看到过有关推迟加载商店以给图表时间渲染的帖子,但这没有用:

listeners: {
    afterrender: function (chart) {
        if (chart.isVisible()) {
            countyStore.load();
            chart.redraw();
        }
    }
},

Thanks in advance :) 提前致谢 :)

Nathan 弥敦道

Probably a bug in Ext. 可能是Ext中的错误。

The chart colors are set in Ext.chart.AbstractChart#updateColors . 图表颜色在Ext.chart.AbstractChart#updateColors中设置。 This is a "config" method, that is called automatically when setColors is called, and also from the constructor, when the config is initialized. 这是一个“ config”方法,在调用setColors会自动调用该方法,并且在初始化config时也会从构造函数中调用该方法。

In your case, it is only called at construction time, before the remote store has been loaded; 在您的情况下,它仅在构造时调用,而远程存储已加载; and it happens that polar series need to know the number of records in the store in order to know how many colors it must used (unlike other kind of charts that rely on number of axis or so). 碰巧极地系列需要知道存储中的记录数才能知道它必须使用多少种颜色(与其他依赖于轴数左右的图表不同)。

Here's the code of that method: 这是该方法的代码:

updateColors: function (newColors) {
    var me = this,
        colors = newColors || (me.themeAttrs && me.themeAttrs.colors),
        colorIndex = 0, colorCount = colors.length, i,
        series = me.getSeries(),
        seriesCount = series && series.length,
        seriesItem, seriesColors, seriesColorCount;

    if (colorCount) {
        for (i = 0; i < seriesCount; i++) {
            seriesItem = series[i];

            // Ext.chart.series.Polar#themeColorCount uses store.getCount()
            // so seriesColorCount will be 0
            seriesColorCount = seriesItem.themeColorCount();

            // ... hence seriesColor will be an empty array
            seriesColors = me.circularCopyArray(colors, colorIndex, seriesColorCount);
            colorIndex += seriesColorCount;
            seriesItem.updateChartColors(seriesColors);
        }
    }
    me.refreshLegendStore();
}

You could probably get it working by creating the chart after the load event of the store, but that's kind of kinky given your usage is as intended, and the bug will probably get smashed in a coming release. 您可以通过在商店的load事件之后创建图表来使其正常工作,但是考虑到您的用法是按预期使用的,这有点古怪,而该错误可能会在以后的发行版中被粉碎。

For now, a possible fix is to override the onRefresh of the chart, that is called, well, when the store is refreshed, and force colors to be updated at this time: 现在,一个可能的解决方法是重写图表的onRefresh ,即在刷新商店时调用该方法,并在此时强制更新颜色:

Ext.define(null, {
    override: 'Ext.chart.PolarChart'
    ,onRefresh: function() {
        this.callParent(arguments);
        var colors = this.getColors();
        if (colors) {
            this.updateColors(colors);
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM