4

I have an bar chart made with echarts in Angular (ngx-echarts), using the dataset + encode API:

const options: EChartOption = {
  tooltip: {},
  dataset: {source: dataSource},
  xAxis: {
    name: xAxisLabel,
    nameLocation: 'middle',
    nameGap: 22,
  },
  yAxis: {
    name: yAxisLabel,
    nameLocation: 'middle',
    nameGap: 25,
  },
  series: [{
    name: field.label,
    type: 'bar',
    encode: {x: xDataField, y: yDataField},
  }],
};

I want to highlight a specific bar by making is red, while setting the rest grey. I realize this can be done easily if I were to declare each data point individually:

series: [{
    data: [
        {
            value: 120,
            itemStyle: {color: 'grey'},
        },
        {
            value: 200,
            itemStyle: {color: 'red'},
        },
        {
            value: 150,
            itemStyle: {color: 'grey'},
        }
    ],
    type: 'bar'
}],

But if possible I would like to avoid that and stick with the dataset + encode API. Is there anyway to address an individual bar and modify its color, or highlight it by default?

liamhawkins
  • 1,301
  • 2
  • 12
  • 30

2 Answers2

4

refer to this link: https://echarts.apache.org/v4/en/option.html#series-pie.itemStyle.color

you can get it done using itemStyle.color as a callback function like this:

const colors = {
  positive: '#008000',
  neutral: '#ffa500',
  negative: '#ff0000',
}
// ... 
chart.setOption({
    id: 'pie',
    type: 'pie',
    itemStyle: { 
      // HERE IS THE IMPORTANT PART
      color: seriesIndex => colors[seriesIndex.name] 
    },
    encode: {
      itemName: 'sentiment',
      value: initialDim ,
      tooltip: initialDim
    }
  }
)
DanWyry
  • 41
  • 2
-2

I think you can do it inside the dataset like the following:

dataset:{ source: [{category:'whatever', value: this.object, itemStyle: { color: '#e67345' } }, .....]

Alaa'
  • 487
  • 3
  • 7
  • 16
  • I can't get this to work. I have the same question as the OP. Does anyone have add'l ideas? – mrcrag Oct 28 '20 at 19:18