selbolder
selbolder

Reputation: 527

Echarts, how to use data series to switch color of line

I want to use the data field to change the color of the line for a certain pice. Like when it is 1, it should be green, and when it is 2 it should be yellow. This snippet is only changing the color of the data point, not the line? Is there any chance to archive that with echarts?

<html>
	<head>

		<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts.min.js"></script>
	</head>
	<body>
	<div id="main_chart" style="width: 1200px;height:600px;"></div>

	<script type="text/javascript">

		// based on prepared DOM, initialize echarts instance
		var myChart = echarts.init(document.getElementById('main_chart'));

		var app = {};
		option = null;
		option = {
			xAxis: {
				type: 'category',
				data: ['2012-03-01 05:06', '2012-03-01 05:07', '2012-03-01 05:08', '2012-03-01 05:09', '2012-03-01 05:10', '2012-03-01 05:11']

			},
			yAxis: {
				type: 'value'
			},
			visualMap: {
			show: false,
			dimension: 2,
			pieces: [
				{
				lt: 2,
				gt: 0,
				color: 'green'
			}, {
				gte: 2,
				color: 'red'
			}
			
			
			]


		},
			series: [{


				data: [[1,37,1],[2,36,1],[3,36,2]],


				type: 'line',
				areaStyle: {}    
	 

			}]
		};

		if (option && typeof option === "object") {
			myChart.setOption(option, true);
		}
	</script>


	</body>
	</html>

Upvotes: 1

Views: 2750

Answers (1)

mrgrechkinn
mrgrechkinn

Reputation: 908

Try this example
1) You should use the right dimension (xAxis, yAxis), for more info read documentation
2) If you want to color full area, please remove color: "aquamarine" from areaStyle, then it will be colorizate by pieces configs

<html>
  <head>

    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.1.0/echarts.min.js"></script>
  </head>
  <body>
  <div id="main_chart" style="width: 1200px;height:600px;"></div>

  <script type="text/javascript">

    // based on prepared DOM, initialize echarts instance
    var myChart = echarts.init(document.getElementById('main_chart'));

    var app = {};
    option = null;
    option = {
      xAxis: {
        type: 'category',
        data: ['2012-03-01 05:06', '2012-03-01 05:07', '2012-03-01 05:08', '2012-03-01 05:09', '2012-03-01 05:10', '2012-03-01 05:11']

      },
      yAxis: {
        type: 'value'
      },
      visualMap: {
      show: false,
      dimension: 0,
      pieces: [
        {
        lt: 2,
        gt: 0,
        color: 'green'
      }, {
        gt: 2,
        lt: 3,
        color: 'red'
      }, {
        gte: 3,
        color: 'yellow'
      }
      
      
      ]


    },
      series: [{


        data: [[1,37,1],[2,36,1],[3,36,2],[4,38,2]],


        type: 'line',
        areaStyle: {color: "aquamarine"}    
   

      }]
    };

    if (option && typeof option === "object") {
      myChart.setOption(option, true);
    }
  </script>


  </body>
  </html>

Upvotes: 3

Related Questions