echarts custom 实现立体柱状图综合代码


效果图:

 

 

 

 

 


const myshape={
x: 0,
y: 0,
width: 20, //柱宽
zWidth: 10, //阴影折角宽
zHeight: 8 //阴影折角高
};
const color = ['rgb(112,173,71)','#47ad7e',
'rgb(255,255,0)','#EEDC82',
'rgb(255,0,0)','#8B3A3A'

];
//颜色两个一对,先高亮浅色,后阴影深色,然后第二个


const MyShape = echarts.graphic.extendShape({shape:myshape,

buildPath: function (ctx, shape) {
const api = shape.api;
const xAxisPoint = api.coord([0, shape.xValue]);
ctx.rect(xAxisPoint[0], shape.y-shape.width/2,shape.x-xAxisPoint[0],shape.width);
ctx.closePath();
}
});
const MyShadow = echarts.graphic.extendShape({
shape:myshape,
buildPath: function (ctx, shape) {
const api = shape.api;
const xAxisPoint = api.coord([0, shape.xValue]);

ctx.moveTo(xAxisPoint[0],shape.y+shape.width/2);
ctx.lineTo(shape.x,shape.y+shape.width/2);

ctx.lineTo(shape.x,shape.y-shape.width/2);

ctx.lineTo(shape.x+shape.zHeight,shape.y-shape.width/2+shape.zWidth);

ctx.lineTo(shape.x+shape.zHeight,shape.y+shape.width/2+shape.zWidth);

ctx.lineTo(xAxisPoint[0],shape.y+shape.width/2+shape.zWidth);

ctx.lineTo(xAxisPoint[0],shape.y+shape.width/2);

ctx.closePath();
}
});

echarts.graphic.registerShape('MyShape', MyShape);
echarts.graphic.registerShape('MyShadow', MyShadow);

const myshapeinserve={
x: 0,
y: 0,
width: 20, //柱宽
zWidth: 10, //阴影折角宽
zHeight: 8 //阴影折角高
};

//颜色两个一对,先高亮浅色,后阴影深色,然后第二个


const MyShapeInserve = echarts.graphic.extendShape({shape:myshapeinserve,

buildPath: function (ctx, shape) {
const api = shape.api;
const xAxisPoint = api.coord([0, shape.xValue]);
ctx.rect(xAxisPoint[0], shape.y-shape.width/2,shape.x-xAxisPoint[0],shape.width);
ctx.closePath();
}
});
const MyShadowInserve = echarts.graphic.extendShape({
shape:myshapeinserve,
buildPath: function (ctx, shape) {
const api = shape.api;
const xAxisPoint = api.coord([0, shape.xValue]);

ctx.moveTo(xAxisPoint[0],shape.y+shape.width/2);
ctx.lineTo(shape.x,shape.y+shape.width/2);

ctx.lineTo(shape.x,shape.y-shape.width/2);

ctx.lineTo(shape.x-shape.zHeight,shape.y-shape.width/2+shape.zWidth);

ctx.lineTo(shape.x-shape.zHeight,shape.y+shape.width/2+shape.zWidth);

ctx.lineTo(xAxisPoint[0],shape.y+shape.width/2+shape.zWidth);

ctx.lineTo(xAxisPoint[0],shape.y+shape.width/2);

ctx.closePath();
}
});

echarts.graphic.registerShape('MyShapeInserve', MyShapeInserve);
echarts.graphic.registerShape('MyShadowInserve', MyShadowInserve);
option = {
tooltip: {
trigger: 'item',
formatter: '{a} <br/>{b}: {c} '
},

grid: {
left:'-10%',
containLabel: true
},

xAxis: {
// type: 'value',
inverse: true ,//反转坐标轴
show:true,

splitLine:{ show:false }
},
yAxis: {
show:true,
splitLine:{ show:false },
data: ['one', 'two','three','four','five']
},

series: [
{
type: 'custom',

renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);

return {
type: 'group',
children: [
{
name:"item1",
type:'MyShape',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]-myshape.width
},
style: {
fill: color[0]
}
},
{
name:"item1",
type:'MyShadow',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]-myshape.width
},
style: {
fill: color[1]
}
}]
}
},
data: [-10, -20,-15,-25,-8]
},
{
type: 'custom',

renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);

return {
type: 'group',
children: [
{
name:"item1",
type:'MyShape',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]
},
style: {
fill: color[2]
}
},
{
name:"item1",
type:'MyShadow',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]
},
style: {
fill: color[3]
}
}]
}
},
data: [-12, -20,-10,-15,-10]
},
{
type: 'custom',

renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);

return {
type: 'group',
children: [
{
name:"item1",
type:'MyShape',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]+myshape.width
},
style: {
fill: color[4]
}
},
{
name:"item1",
type:'MyShadow',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]+myshape.width
},
style: {
fill: color[5]
}
}]
}
},
data: [-10, -20,-15,-25,-8]
},

{
type: 'custom',

renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);

return {
type: 'group',
children: [
{
name:"item1",
type:'MyShapeInserve',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]-myshapeinserve.width
},
style: {
// fill:'#5AD8A6',
fill: color[0]
}
},
{
name:"item1",
type:'MyShadowInserve',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]-myshapeinserve.width
},
style: {
fill: color[1],
// fill:'#2DCF8E',
}
}]
}
},
data: [20,15,25]
},
{
type: 'custom',

renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);

return {
type: 'group',
children: [
{
name:"item1",
type:'MyShapeInserve',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]
},
style: {
// fill:'#5AD8A6',
fill: color[2]
}
},
{
name:"item1",
type:'MyShadowInserve',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]
},
style: {
fill: color[3],
// fill:'#2DCF8E',
}
}]
}
},
data: [10, 20,25]
},
{
type: 'custom',

renderItem: function (params, api) {
let location = api.coord([api.value(0), api.value(1)]);

return {
type: 'group',
children: [
{
name:"item1",
type:'MyShapeInserve',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]+myshapeinserve.width
},
style: {
// fill:'#5AD8A6',
fill: color[4]
}
},
{
name:"item1",
type:'MyShadowInserve',
shape: {
api,
xValue: api.value(0),
yValue: api.value(1),
x: location[0],
y: location[1]+myshapeinserve.width
},
style: {
fill: color[5],
// fill:'#2DCF8E',
}
}]
}
},
data: [15,25,8]
}

]
};


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM