Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Echarts multicolored line chart

Is it possible to make echarts line chart with 1 line painted in different colors? For example if value <= 0 the color is red, if > 0 the color is green?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Echarts has an option called visualMap that does exactly what you are looking for.

  • visualMap doc
  • visualMap code example (I selected the example that fits best, but there are others)

In your case you'll have something like that :

visualMap: {
   show: false, // Wether to show the legend or not (default: true)
   pieces: [
     {
       min: -9999, // Normally not needed but doesn't work without that (1)
       max: 0,
       color: '#F35E07' // Red
     },
     {
       min: 0,
       color: '#93CE07' // Green
     },
   ],
   outOfRange: {
     color: '#F35E07'
   }
},

It'll split your line in 2 pieces :

  • below 0 (written max: 0) : red
  • above 0 (written min: 0) : green

In addition, the visualMap option has more to offer : you can have more than 2 pieces (like in this example), have a smooth gradient instead of pieces (using type: 'continuous' like in this example), and many other things that are explained in its doc.

(1) Note about the bug: Normally if you don't specify min or max, it's set to -Infinity or Infinity. But here you have to specify min AND max in one of the two pieces (I don't know why).

about 4 years ago · Juan Pablo Isaza Relatório

0

If you consider plotting two charts and ignoring some of its points with '-', you can achieve the same visual result of a multicolored line chart.


Although the documentation does not provide an immediate example of how to color specific segments of a line chart, it actually instructs about how you can use empty data,

While there are empty elements, the lines chart will ignore that point without passing through it----empty elements will not be connected by the points next. In ECharts, we use '-' to represent null data, It is applicable for data in other series.

With that in mind, by overlaying two equal charts and emptying some points, you can actually construct the logic of a multicolored chart. Notice that it is better if you can do it programmatically so that you can vary the number of line segments to be colored.

var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;

option = {
  xAxis: {},
  yAxis: {},
  series: [
    {
      data: [
        [-20, -20],
        [-10, 30],
        [0, 40],
        ['-', '-'],
        ['-', '-']
      ],
      type: 'line',
      lineStyle: {
        color: "red"
      }
    },
    {
      data: [
        ['-', '-'],
        ['-', '-'],
        [0, 40],
        [10, 100],
        [20, 60]
      ],
      type: 'line',
      lineStyle: {
        color: "green"
      }
    }
  ]
};

option && myChart.setOption(option);
.as-console-wrapper { max-heght: 100% !important }
<script src="https://cdn.jsdelivr.net/npm/echarts@5.3.2/dist/echarts.js"></script>
<body>
  <div id="main" style="width: 225px;height:225px;"></div>
</body>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda