Mark custom chart color on legend in EXT JS 5

I have a problem using a custom legend color in an EXTJS 5 chart. I can apply a custom color to the chart legend, but I can’t apply it to the legend item. In EXT JS 4, I can use one override function sequentially to handle it. as

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

      

But this is not available in EXT JS 5.

Sample code.

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

Please let me know if I need to change anything.

Thank you in advance

+3


source to share


2 answers


Try using the "colors" property inside the series:

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

      



Example: https://fiddle.sencha.com/#fiddle/bk1

+2


source


Answering the question about "global color arrays" (which I cannot answer in the comments section - not enough reputation. Alexey Miikin is absolutely right in his decision), you can use a singleton to store such values

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

      



Then specify

    ...
    colors: [MyApp.colours.COL1,  MyApp.colours.COL2],
    ...

      

0


source







All Articles