Extjs堆叠条形图中各条的颜色如何修改?

5

让我们以这个示例为例...如何更改这些条形的颜色?

我知道可以通过renderer进行更改,但这不会改变图例。

我尝试使用:

style: {fill: 'red'}

但它会改变每个条形的颜色。

我尝试将颜色放入数组中,但不起作用。

我尝试将每种样式放入数组中,像这样:

style: [{fill: 'red'}, {fill: 'green'}, {fill: 'blue'}]

但这样也行不通,因为我可以像这样将标题放入数组中:
title: ['title1', 'title2', 'title3']

我认为样式应该也可以起作用,但实际上并没有。

那么我该如何改变每个“数据”条的颜色?

6个回答

10

柱状图的颜色实际上是由图表主题决定的:

Ext.create('Ext.chart.Chart', {
    theme: 'myTheme'
    //the remainder of your code...
});
为了使用自定义颜色,您需要扩展现有的“Base”主题,就像custom area chart example中所示。

4

图表使用 Theme 作为 mixins,因此您可以直接使用名为 themeAttrs 的主题属性。

例如,在柱形/堆叠柱形图的构造函数中,如果想要更改柱子的颜色,您可以指定:

this.themeAttrs.colors = ['#F2C72B','#a5c249','#E88712','#9D5FFA'];

3
Ext.onReady(function() 
{

  Ext.create('Ext.window.Window', 
  {
    title: 'Pie1 Chart',
    height: 400,
    layout: 'fit',
    width: 400,
    items: 
    [{
      xtype: 'chart',
      animate:false ,
      insetPadding:0,

      legend: 
    {
        position: 'right'
        },
      shadow: true,
      store: 
    {
        fields: 
    [{
          name: 'category', type: 'string'
        },

    {
          name: 'data', type: 'float'
        }],
        data: 
    [{
          category: 'Alive', data: 1.5
        },{
          category: 'Dead', data: 2 
        },{
          category: 'Standby', data: 1
        }]
      },
      series: [{
        type: 'pie',
        field: 'data',
     colorSet: ['#0000FF','#FFffff','#00FF00'],

        showInLegend: true, <!--It is use to display data in which color.-->
        highlight: {
          segment: {
            margin: 20
          }
        },
        label: {
          field: 'category',
          display: 'rotate',
          contrast: true,
          font: '18px Arial'
        }
      }]
    }]
  }).show();  
});

使用colorSet,你可以改变颜色。

1
我决定添加完整的代码:
Ext.require('EAM.view.chart.*');
Ext.define('EAM.view.chart.integral.IntegralChart',
{
    extend : 'Ext.chart.Chart',
    alias : 'widget.GroupsIntegralChart',
    animate : true,
    shadow : true,
    // theme : 'Browser:gradients',

    initComponent : function()
    {
        var me = this;
        me.themeAttrs.colors =
        [
            'orange',
            'red',
            'blue',
            'green',
        ];
        me.callParent(arguments);
    },
...

0

我正在使用Ext JS 5.0。不用担心,只需在系列中包含颜色配置即可。

series : [{
        type : 'bar',
        colors:['#f23f3f','#ff8809','#ff0','#0039a6','#00ee88','#ff8aed'],
        xField : ['Q1']
        yField : ['Critical', 'Major', 'Minor', 'Warning', 'Informational',   'Indeterminent']
}]

0

如果您不想使用主题,可以尝试使用以下方法(至少对我有效):

this.series = [{
type: 'column',
axis: 'left',
showInLegend : true,
xField: 'f1',
style :{
    fill :  '#ff00ff',
    'stroke-width': 3,
    width: 20
},
renderer: function(sprite, storeItem, barAttr, i, store) {
    barAttr.fill = '#ff00ff';
    barAttr.width = 20;
    return barAttr;
},
title : 'title',
yField: 'f2'
}]

注意你需要将颜色写在两行中(我认为ExtJS有时候是一个有点蠢的框架)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接