简体   繁体   English

如何用 echarts 渲染自定义线?

[英]How to render custom line with echarts?

Been struggling to find this, and have even resorted to logging output of echarts' various objects in the console to try to see what's going on.一直在努力寻找这个,甚至诉诸于在控制台中记录 echarts 的各种对象的 output 以尝试查看发生了什么。 It would seem that visualMap doesn't work properly when you specify a different dimension in your data, so I've been trying to write a simple renderItem function for my line series.当您在数据中指定不同的维度时,visualMap 似乎无法正常工作,因此我一直在尝试为我的线系列编写一个简单的 renderItem function。 I can't find any documentation or examples on specifics for rendering a line - specifically, when data is passed into it, how do I get coordinate points for both the start and end of the line?我找不到任何有关渲染线条细节的文档或示例 - 具体来说,当数据传入其中时,如何获取线条的起点和终点的坐标点? I can get the values of the point (the data point which would give you the coordinates of the current data point), but a line consists of 2 points (start and end).我可以得到该点的值(该数据点将为您提供当前数据点的坐标),但一条线由 2 个点(起点和终点)组成。 What am I missing here?我在这里想念什么?

Codepen example to play with: https://codepen.io/WorldWideWebb/pen/JjbJpRJ要使用的 Codepen 示例: https://codepen.io/WorldWideWebb/pen/JjbJpRJ

visualMap that didn't work (it changes the color of the point, NOT the line; the point colors do change just fine, but I want to color the line)不起作用的visualMap(它改变了点的颜色,而不是线;点colors确实改变得很好,但我想给线上色)

    visualMap: {
        dimension: 3,
        seriesIndex: 2,
        show: false,
        pieces: [{
            gt: 0,
            lte: 50,
            color: '#93CE07'
        }, {
            gt: 50,
            lte: 100,
            color: '#FBDB0F'
        }, {
            gt: 100,
            lte: 150,
            color: '#FC7D02'
        }, {
            gt: 150,
            lte: 200,
            color: '#FD0100'
        }, {
            gt: 200,
            lte: 300,
            color: '#AA069F'
        }, {
            gt: 300,
            color: '#AC3B2A'
        }],
        outOfRange: {
            color: '#999'
        }
    },

You can see the points in the different colors if you uncomment the visualMap in the codepen example and change "showSymbol" in the last item in the series to true.如果您取消注释 codepen 示例中的 visualMap 并将系列中最后一项中的“showSymbol”更改为 true,您可以看到不同 colors 中的点。

This one was close, but I haven't been able to figure out how to get both the origin and end to create the line properly (x2 and y2 are both static);这个很接近,但我无法弄清楚如何同时获得起点和终点以正确创建线(x2 和 y2 都是静态的); also, this seems to produce a bunch of separate lines, rather than a line series:此外,这似乎产生了一堆单独的行,而不是一个行系列:

        renderItem: function (params, api) { 
            var coord = api.coord([api.value(0), api.value(1)]);
                
            return {
                type: 'line',
                shape: {
                    x1: coord[0],
                    y1: coord[1],
                    x2: 200,
                    y2: 100
                },
                style: { stroke: formatDirectionLabel(api.value(1)), lineWidth: 2 }
            }
        }

Example of the data (this is time, wind speed, wind gust, wind direction, and temp);数据示例(这是时间、风速、阵风、风向和温度); I use map to transform that into simple arrays when it's pulled:当它被拉出时,我使用 map 将其转换为简单的 arrays:

{"dt":"2021-02-18 06:33:10","w":"7.38","g":"9.84","dir":"343","f":"47.70"}

My goal is to display a line - time is the X axis, wind speed is the y axis, and color the line based on wind direction.我的目标是显示一条线 - 时间是 X 轴,风速是 y 轴,并根据风向为线着色。 Like I said, all of it would work if I just wanted to color the points, but that would make the graph really cluttered (not what I'm going for).就像我说的,如果我只想给点上色,所有这些都可以工作,但这会使图表变得非常混乱(不是我想要的)。

How do I color each line segment based on wind direction?如何根据风向为每个线段着色? Can someone provide a simple example of passing data in to render a custom line using renderItem?有人可以提供一个简单的示例来传递数据以使用 renderItem 呈现自定义线吗?

With a whole bunch of trial and error, I got it working.经过大量的反复试验,我得到了它的工作。 The solution was using params.dataIndexInside+1 to produce the next item in the series.解决方案是使用 params.dataIndexInside+1 来生成系列中的下一个项目。 renderItem contents:渲染项内容:

    let coord1 = api.coord([api.value(0, params.dataIndexInside), api.value(1, params.dataIndexInside)]),
        coord2 = api.coord([api.value(0, params.dataIndexInside+1), api.value(1, params.dataIndexInside+1)]);

    return {
        type: 'line',
        shape: {
           x1: coord1[0],
           y1: coord1[1],
           x2: coord2[0],
           y2: coord2[1]
        },
        style: {
           stroke: customFormattingFunction(api.value(3))
        }
     },

Hope that saves someone some time希望可以节省一些时间

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

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