简体   繁体   English

apache echarts中根据逻辑改变线条颜色

[英]line color change based on logic in apache echarts

Due to the company policy I can't share any kind of code but I have been struggling with apche echarts.由于公司政策,我不能分享任何类型的代码,但我一直在努力使用 apche echarts。 The requirement is I have a line whose color should change at certain points based on logic.要求是我有一条线,其颜色应该根据逻辑在某些点改变。 I tried achieving using linestyle and piecewise using dimension.我尝试使用 linestyle 实现并使用 dimension 进行分段。 I came pretty close to visualmap but the issue persisted when The parallel or adjacent lines started to get affected and caused wrong color changes on the graph.我非常接近 visualmap,但是当平行线或相邻线开始受到影响并导致图表上出现错误的颜色变化时,问题仍然存在。 Here is the requirement the line on the chart is supposed to be red unless we come across a point where the color could be changed to green and back to red when the event is done.这是图表上的线应该是红色的要求,除非我们遇到一个点,在该点上颜色可以更改为绿色并在事件完成时变回红色。 please refer to the image and I am open to any and all suggestions.请参考图片,我愿意接受任何和所有建议。 I would prefer a sandbox as an answer link so i can test if that works with the requirement or not.我更喜欢沙箱作为答案链接,这样我就可以测试它是否符合要求。 需求图片

EDIT 1 The closest I came is this编辑 1 我最接近的是这个

option = {
    title: {
        text: '一天用电量分布',
        subtext: '纯属虚构'
    },
    tooltip: {
        trigger: 'axis',
        axisPointer: {
            type: 'cross'
        }
    },
    toolbox: {
        show: true,
        feature: {
            saveAsImage: {}
        }
    },
    xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['00:00', '01:15', '02:30', '03:45', '05:00', '06:15', '07:30', '08:45', '10:00', '11:15', '12:30', '13:45', '15:00', '16:15', '17:30', '18:45', '20:00', '21:15', '22:30', '23:45']
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: '{value} W'
        },
        axisPointer: {
            snap: true
        }
    },
    visualMap: {
        show: false,
        dimension: 0,
        pieces: [{
            lte: 6,
            color: 'green'
        }, {
            gt: 6,
            lte: 8,
            color: 'red'
        }, {
            gt: 8,
            lte: 14,
            color: 'green'
        }, {
            gt: 14,
            lte: 17,
            color: 'red'
        }, {
            gt: 17,
            color: 'green'
        }]
    },
    series: [
        {
            name: '用电量',
            type: 'line',
            smooth: true,
            data: [300, 280, 250, 260, 270, 300, 550, 500, 400, 390, 380, 390, 400, 500, 600, 750, 800, 700, 600, 400],
        }
    ]
};

which renders呈现这个

how can I use logic to calculate the visualmap points based on event.我如何使用逻辑来计算基于事件的视觉地图点。

You can do that using the visualMap component which maps the data to visual channels.您可以使用将数据映射到视觉通道的visualMap组件来做到这一点。

Like in your example, in a Line series chart you have:就像在您的示例中一样,在 Line 系列图表中,您有:

visualMap: {
 show: false,
 dimension: 0,
 pieces: [
  {
    lte: 6,
    color: 'green'
  },
  {
    gt: 6,
    lte: 8,
    color: 'red'
  },
  {
    gt: 8,
    lte: 14,
    color: 'green'
  },
  {
    gt: 14,
    lte: 17,
    color: 'red'
  },
  {
    gt: 17,
    color: 'green'
      }
   ]
 },

The data for the Line chart is an array of 20 values so the above means: draw the line green for the first 6 values, draw red for values between 6 and 8, then green till 14th value, then red for 14th to 17th value and then green for the values thereon.折线图的数据是一个包含 20 个值的数组,因此上面的意思是:为前 6 个值绘制绿色线,为 6 到 8 之间的值绘制红色,然后为第 14 个值绘制绿色,然后为第 14 到 17 个值绘制红色,然后然后绿色表示其上的值。

折线图示例

See the example here .请参阅此处的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM