ExtJS 5饼图无法使用远程存储呈现 [英] ExtJS 5 Pie Chart Not Rendering Using Remote Store

查看:140
本文介绍了ExtJS 5饼图无法使用远程存储呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ExtJS 5中有一个基本的饼图.我遇到的问题是,该图表使用静态JsonStore呈现,但不能使用远程data.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?

这是我的代码:

查看(图表)

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();
    }
});

商店

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'
            }
        }
    }
});

模型

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

这是静态存储的渲染方式:

This is how is renders with the static store:

这是它在远程存储中的呈现方式:

This is how it renders with the remote store:

尽管图表是使用Sencha CMD和sencha-charts目录中的"sencha ant build"命令构建的,但我使用的是GPL的最新版本.

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.

为什么静态存储显示它(底部仍然有关于图例的问题),但远程json没有显示?

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?

我试图在渲染图表之后加载商店,并且可见,因为我之前看到过有关推迟加载商店以给图表渲染时间的帖子,但这没用:

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();
        }
    }
},

先谢谢您了:)

内森

推荐答案

可能是Ext中的错误.

Probably a bug in Ext.

图表颜色在 Ext.chart.AbstractChart#updateColors .这是一个配置"方法,在调用setColors时会自动调用该方法,并且在初始化配置时也会从构造函数中调用该方法.

The chart colors are set in 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.

在您的情况下,它仅在构造时(在加载远程存储之前)被调用;碰巧极地系列需要知道存储中的记录数才能知道它必须使用多少种颜色(与其他依赖于轴数左右的图表不同).

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).

这是该方法的代码:

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();
}

您可能可以通过在商店的load事件之后创建图表来使其正常工作,但是考虑到您的用法是按预期使用的,这有点古怪,并且该错误可能会在以后的发行版中被粉碎.

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.

目前,一种可能的解决方法是覆盖图表的onRefresh,也就是在刷新商店时调用它,并在此时强制更新颜色:

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:

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

这篇关于ExtJS 5饼图无法使用远程存储呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆