ECharts在散点图上绘制一个多边形

时间:2019-07-10 09:37:52

标签: charts scatter-plot echarts

我在Google上搜索了很多,但是找不到任何信息。是否可以在ECharts的散点图上绘制多边形以获得与下图相同的视图?

enter image description here

1 个答案:

答案 0 :(得分:0)

ECharts具有绘制多边形的API:

function renderItem(params, api) {
    if (params.context.rendered) {
        return;
    }

    params.context.rendered = true;

    let points = [];
    for (let i = 0; i < data.length; i++) {
        points.push(api.coord(data[i]));
    }

    let color = api.visual('color');

    return {
        type: 'polygon',
        shape: {
            points: echarts.graphic.clipPointsByRect(points, {
                x: params.coordSys.x,
                y: params.coordSys.y,
                width: params.coordSys.width,
                height: params.coordSys.height
            })
        },
        style: api.style({
            fill: color,
            stroke: echarts.color.lift(color)
        })
    };
}

var data = [
     [
        echarts.number.round(3.8),
        echarts.number.round(4)
     ],
     [
        echarts.number.round(3.8),
        echarts.number.round(4.5)
     ],
     [
        echarts.number.round(5),
        echarts.number.round(6)
     ]
];

option = {
    xAxis: {},
    yAxis: {},
    series: [
        ...other charts here ...
        {
            type: 'custom',
            renderItem: renderItem,
            data: data
        }
    ]
};

结果如下:

enter image description here