简体   繁体   English

Extjs 5.1图表图例

[英]Extjs 5.1 chart legend

I'm using Ext JS 5.1. 我正在使用Ext JS 5.1。

I have a very simple problem: 我有一个非常简单的问题:
I have a pie chart, and a button. 我有一个饼图和一个按钮。 When I click on the button, I bind a new store to my chart. 当我单击按钮时,我将新商店绑定到我的图表。 This works fine. 这很好。 But, if I make one element invisible in my chart (by clicking on one item in the legend) and bind a new store, the item at the same index of the new store will also be invisible. 但是,如果我使图表中的一个元素不可见(通过单击图例中的一项)并绑定一个新商店,则位于新商店相同索引处的项目也将不可见。 I don't want that. 我不要 Is there any way to set everything visible for a chart ? 有什么办法可以设置图表可见的所有内容?

Here is the Sencha Fiddle to clearly explain my problem. 这是Sencha小提琴,可以清楚地解释我的问题。

And the code if you're lazy! 还有代码,如果你很懒!

Store A 商店A

var storeA = Ext.create('Ext.data.Store', {
    fields: ['name', 'value'],
    data: [
        {"name": "A-0", "value": 18.34},
        {"name": "A-1", "value": 2.67},
        {"name": "A-2", "value": 1.90}
    ]
});

Store B B店

var storeB = Ext.create('Ext.data.Store', {
    fields: ['name', 'value'],
    data: [
        {"name": "B-0", "value": 4.34},
        {"name": "B-1", "value": 54.67},
        {"name": "B-2", "value": 18.90}
    ]
});

Polar chart 极坐标图

var donut = Ext.create('Ext.chart.PolarChart', {
    title: 'Test',
    animation: true,
    width: 300,
    height: 300,
    renderTo: Ext.getBody(),

    store: storeA,

    legend: {
        docked: 'bottom'
    },

    series: [{
        type: 'pie',
        angleField: 'value',
        colors: ["#9aff4f", "#35b4e3", "#ffb400"],
        donut: 20,
        label: {
            field: 'name',
            display: 'inside'
        },
        highlight: true
    }]
});

Button - Swap stores 按钮-交换商店

var button = Ext.create('Ext.Button', {
    text: 'Click to change the pie store',
    renderTo: Ext.getBody(),
    margin: '10 10 10 63',
    handler: function() {
        if (donut.getStore() == storeA) {
            donut.bindStore(storeB);
        }
        else {
            donut.bindStore(storeA);
        }
    }
});


Ext.create('Ext.form.Label', {
    html: '<br/><br/>To produce the bug:<br/><br/>' +
          '1 - Make one item invisible by clicking on an item in the legend (A-0 for example).<br/>' +
          '2 - Change the pie store by clicking on the blue button.<br/>' +
          '3 - See with the new store the item at the same index is invisible. I don\'t want that to happen.<br/><br/>' +
          'Is it possible to "enable" (make visible) every items of a pie chart?',
    width: 300,
    height: 300,
    renderTo: Ext.getBody()
});

I took your suggested answer and created a complete example. 我接受了您的建议答案,并创建了一个完整的示例。 See the Sencha Fiddle for a live demo or refer to the code below. 有关实时演示,请参见Sencha Fiddle ,或参考下面的代码。

var storeA = Ext.create('Ext.data.Store', {
    fields: ['name', 'value'],
    data: [
        {"name": "A-0", "value": 18.34},
        {"name": "A-1", "value": 2.67},
        {"name": "A-2", "value": 1.90}
    ]
});

var storeB = Ext.create('Ext.data.Store', {
    fields: ['name', 'value'],
    data: [
        {"name": "B-0", "value": 4.34},
        {"name": "B-1", "value": 54.67},
        {"name": "B-2", "value": 18.90}
    ]
});

var donut = Ext.create('Ext.chart.PolarChart', {
    animation: true,
    width: 300,
    height: 300,

    store: storeA,

    legend: {
        docked: 'bottom'
    },

    series: [{
        type: 'pie',
        angleField: 'value',
        lengthField: 'length',
        colors: ["#9aff4f", "#35b4e3", "#ffb400"],
        donut: 20,
        label: {
            field: 'name',
            display: 'inside'
        },
        highlight: true
    }]
});

Ext.create('Ext.panel.Panel', {
    title : 'Chart Example: Swap Stores',
    renderTo: Ext.getBody(),
    layout: {
        type  : 'vbox',
        pack  : 'center',
        align : 'middle'
    },
    defaults : {
        margin : '8 0',
    },
    items: [donut, {
        xtype: 'label',
        html: 'Is it possible to "disable" (make invisible) items that have no name or 0 as value?<br/>' + 'For example after A-1, I have an "empty" record in my store, but I don\'t want it to be displayed ;)'
    }, {
        xtype: 'button',
        text: 'Click to change the pie store',
        handler: function() {
            if (donut.getStore() == storeA) {
                donut.bindStore(storeB);
            } else {
                donut.bindStore(storeA);
            }

            for (var i = 0; i < donut.legendStore.getCount(); i++) {
                donut.legendStore.getAt(i).set('disabled', false);
            }
        }
    }]
})

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

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