注意:请使用echarts v4.4.0以上版本
相关API介绍
-
graphic——ECharts 图表中原生图形元素组件 graphic 可以支持包括:image,circle,ring,polygon,rect,bezierCurve,arc 等图形元素,我们可以用这些元素组合成我们想要的图形。
-
graphic.registerShape——创建一个新的 shape class, 用 echarts.graphic.extendShape 注册生成以后,可用 getShapeClass得到。
相关配置项介绍
renderItem 函数提供了两个参数:
- params:包含了如下信息:
参数 | 数据类型 | 说明 |
---|---|---|
context: | Object | 一个可供开发者暂存东西的对象。生命周期只为:当前次的渲染 |
seriesId | string | 本系列 id |
seriesName: | string | 本系列 name |
seriesIndex | number | 本系列 index |
dataIndex | number | 数据项的 index |
dataIndexInside | number | 数据项在当前座标系中可见的数据的 index |
dataInsideLength | number | 当前座标系中可见的数据长度 |
actionType | string | 触发此次重绘的 action 的 type |
- api:是一些开发者可调用的方法集合(如 api.value()、api.coord())。
- api.coord ——将数据值映射到座标系上,画布上的点的座标,至少包含:[x, y]
- api.style ——定义的样式信息和视觉映射得到的样式信息,以下为部分参考:
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
culling | boolean | false | 是否进行裁剪 |
progressive | number | -1 | 是否渐进式渲染 |
fill | string | ‘#000’ | 描边样式 |
stroke | string | ‘#000’ | 填充样式 |
opacity | string | null | 不透明度 |
lineDash | number | null | 描边虚线样式 |
text | string | null | 在图形中显示的文字 |
font | string | null | 文字样式 |
上代码😄
var MyCubeRect = echarts.graphic.extendShape({
shape: {
x: 0,
y: 0,
width: 24, //柱宽
zWidth: 4, //阴影折角宽
zHeight: 0, //阴影折角高
},
buildPath: function (ctx, shape) {
const api = shape.api;
//座标系列
const xAxisPoint = api.coord([shape.xValue, 0]);
const p0 = [shape.x, shape.y];
const p1 = [shape.x - 15, shape.y];
const p4 = [shape.x + 15, shape.y];
const p2 = [xAxisPoint[0] - 15, xAxisPoint[1]];
const p3 = [xAxisPoint[0] + 15, xAxisPoint[1]];
// 描绘的图形
ctx.moveTo(p0[0], p0[1]); //0
ctx.lineTo(p1[0], p1[1]); //1
ctx.lineTo(p2[0], p2[1]); //2
ctx.lineTo(p3[0], p3[1]); //3
ctx.lineTo(p4[0], p4[1]); //4
ctx.lineTo(p0[0], p0[1]); //0
ctx.closePath();
}
});
var MyCubeShadow = echarts.graphic.extendShape({
shape: {
x: 0,
y: 0,
width: 15,
zWidth: 6,
zHeight: 4,
},
buildPath: function (ctx, shape) {
const api = shape.api;
const xAxisPoint = api.coord([shape.xValue, 0]);
const p0 = [shape.x, shape.y];
const p1 = [shape.x - 15, shape.y];
const p4 = [shape.x + 15, shape.y];
const p6 = [shape.x + 15 + 8, shape.y - 4];
const p7 = [shape.x - 15 + 8, shape.y - 4];
const p3 = [xAxisPoint[0] + 15, xAxisPoint[1]];
const p5 = [xAxisPoint[0] + 15 + 8, xAxisPoint[1] - 4];
ctx.moveTo(p4[0], p4[1]); //4
ctx.lineTo(p3[0], p3[1]); //3
ctx.lineTo(p5[0], p5[1]); //5
ctx.lineTo(p6[0], p6[1]); //6
ctx.lineTo(p4[0], p4[1]); //4
ctx.moveTo(p4[0], p4[1]); //4
ctx.lineTo(p6[0], p6[1]); //6
ctx.lineTo(p7[0], p7[1]); //7
ctx.lineTo(p1[0], p1[1]); //1
ctx.lineTo(p4[0], p4[1]); //4
ctx.closePath();
}
});
echarts.graphic.registerShape('MyCubeRect', MyCubeRect);
echarts.graphic.registerShape('MyCubeShadow', MyCubeShadow);
var seriesData = [20,100,50,44,12];
option = {
grid: {
height: 300
},
xAxis: {
data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
},
yAxis: {
type: 'value'
},
series: [{
type: 'custom',
data: seriesData,
renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);
return {
type: 'group',
children: [
{
type: 'MyCubeRect', // shape 属性描述了这个矩形的像素位置和大小
shape: {
api,
xValue: api.value(0),//表示取出当前 dataItem 中第一个维度的数值
yValue: api.value(0),
x: location[0],
y: location[1]
},
style: {
stroke: 'yellow', //边框颜色
fill: '#3773e1', //平面颜色
text:seriesData[params.dataIndex],
textPosition:[10,-20] //文字显示位置
}
},
{
type: 'MyCubeShadow',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]
},
style: {
stroke: 'yellow', //边框颜色
fill: '#3773e1'//平面颜色
}
}]
};
}
}]
};