1

I've googled a lot, but couldn't find any information. Is it possible to draw a polygon on the scatter plot in the ECharts to achieve the same view as on the picture below?

enter image description here

Dmitry Maksakov
  • 1,591
  • 1
  • 16
  • 22

1 Answers1

3

ECharts has an API to draw polygons:

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
        }
    ]
};

The result will look like:

enter image description here

Dmitry Maksakov
  • 1,591
  • 1
  • 16
  • 22