maks
maks

Reputation: 29

How to draw a custom triangle in renderItem in Apache Echarts

I want to draw custom triangle using renderItem method:

series: [
                    {
                        ....
                        type: 'custom',
                        renderItem: renderGanttItem,
                        ....
                    }
                ]

I see some build in functions how to draw rectangle in customer render item method. There are some good examples for it. But I could not find good examples how to draw some triangles. Maybe it is better to use some 'polygon'? Or maybe there are some other opportunities.

var rectNormal = clipRectByRect(params, {
                x: x,
                y: y,
                width: barLength,
                height: barHeight
            });
            return {
                type: 'group',
                children: [
                    {
                        type: 'rect',
                        ignore: !rectNormal,
                        shape: rectNormal,
                        style: api.style()
                    }
                ]
            };
        }

        function clipRectByRect(params, rect) {
            return echarts.graphic.clipRectByRect(rect, {
                x: params.coordSys.x,
                y: params.coordSys.y,
                width: params.coordSys.width,
                height: params.coordSys.height
            });
        }

How to draw a triangle? I need something like this enter image description here

Upvotes: 0

Views: 1082

Answers (1)

Mounir Messaoudi
Mounir Messaoudi

Reputation: 373

You can use ECharts API function clipPointsByRect to draw polygons/triangle:

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



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)
        })
    };
}
    
option = {
    xAxis: {},
    yAxis: {},
    series: [
        ...other charts here ...
        {
            type: 'custom',
            renderItem: renderItem,
            data: data
        }
    ]
};

Upvotes: 0

Related Questions