PestoChan
PestoChan

Reputation: 11

ECharts - Apply two different colors to labels on the same axis

I want to apply two different colors to the labels of the same X-axis on a chart.

I'm using Version 4.2.1 of echarts. The X axis labels ranges from 0 to 1,000 in increments of 100.

I want to make the first six labels (i.e. 0, 100, 200, 300, 400, 500) red; then the rest of the labels (i.e. 600, 700, 800, 900, 1000) blue.

In the official documentation it shows that we can specify ONE color to the X-axis labels through the following code:

           xAxis: {
                      name: 'Population',
                      axisLabel: {
                          textStyle: {
                              color: 'red'
                          }
                      },
                  }

I tried running a basic if function as below but it does not work. Not sure what variable name I should use in the if brackets.

             axisLabel: {
                          textStyle: {
                              if (axisLabel < 600) {
                                color: 'red';
                              }
                              else {
                                color: 'blue';
                              }
                          }
                      },

Upvotes: 1

Views: 8146

Answers (1)

Ovilia
Ovilia

Reputation: 7310

axis.axisLabel.color supports function, which can be set like:

color: function(value, index) {
    if (index < 6) {
        return 'red';
    }
    else {
        return 'blue';
    }
}

Upvotes: 3

Related Questions