在EXT JS 5中将自定义图表栏颜色标记为图例

时间:2014-10-10 06:06:20

标签: extjs charts sencha-architect extjs5

在EXTJS 5图表中使用自定义图例颜色时出现问题。我可以将自定义颜色应用于图表图例,但我无法将其应用于图例项目。在EXT JS 4中,我可以使用一个覆盖函数来串行处理它。像

getLegendColor: function(index) {
    var proSpeciality = ["#EFD07B","#0082AD"];
    return proSpeciality[index];
}

但这在EXT JS 5中不可用。

示例代码。

Ext.create('Ext.Container', {
renderTo: Ext.getBody(),
width: 600,
height: 400,
layout: 'fit',
items: {
    xtype: 'cartesian',
    store: {
        fields: ['name', 'value','value2'],
        data: [
            {name: 'metric one', value: 10,value2: 15},
            {name: 'metric two', value: 7,value2: 15},
            {name: 'metric three', value: 5,value2: 15},
            {name: 'metric four', value: 2,value2: 15},
            {name: 'metric five', value: 27,value2: 15}
        ]
    },
    axes: [{
        type: 'numeric',
        position: 'left',
        title: {
            text: 'Sample Values',
            fontSize: 15
        },
        fields: 'value'
    }, {
        type: 'category',
        position: 'bottom',
        title: {
            text: 'Sample Values',
            fontSize: 15
        },
        fields: 'name'
    }],
        legend: {
            docked: 'top',
             style: {
                stroke: '#ffffff',
                'stroke-width': 2,
                opacity: 1
            },
            tpl: [
                '<tpl for=".">               ',
                '                <div class="myLegendItem" style="float:left;margin:5px;padding:0px;cursor:pointer;">',
                '                      <div class="" style="float:left;margin:2px;width:12px;height: 12px; background:{mark};"></div><div style="float:left;">{name}</div>                                     ',
                '                </div>                    ',
                '            </tpl>'
            ],
            itemSelector: '.myLegendItem'
        },
    series: {
        type: 'bar',
        xField: ['name'],
        yField: ['value','value1'],
        stacked: false,
        renderer: function(sprite, record, attributes, index, store) {
                            var proSpeciality = ["#EFD07B","#0082AD","#00A67B","#AD1808","#520084"];
                            attributes.fill = proSpeciality[index % proSpeciality.length];
                            return attributes;
                        },
        getLegendColor: function(index) {
                    var proSpeciality = ["#EFD07B","#0082AD"];
                    return proSpeciality[index];
        }
    }

}

});

enter image description here

如果我需要更改某些内容,请告诉我。

提前致谢

2 个答案:

答案 0 :(得分:2)

尝试在系列中使用“colors”属性:

series: {
    type: 'bar',
    colors: ['orange', 'yellow'],
    ...
}

示例:https://fiddle.sencha.com/#fiddle/bk1

答案 1 :(得分:0)

回答有关全球色彩阵列的问题&#39; (我无法在评论部分回答 - 没有足够的声誉.Aleksei Mialkin在他的解决方案中是完全正确的),你可以使用单身来保存这些价值

Ext.define("MyApp.colours", {
    singleton:  true,
    COL1: 'orange',
    COL2: 'red',
    COL3: 'blue',
    COL4: 'green'
});

然后引用为

    ...
    colors: [MyApp.colours.COL1,  MyApp.colours.COL2],
    ...
相关问题