echarts 中实现伪3d柱状图的方法,直接放到官方示例运行就可以浏览效果

1:基于x轴的伪3d柱状图实现:

var MyCubeRect = echarts.graphic.extendShape({
    shape: {
        x: 0,
        y: 0,
        width: 48, //柱宽        
        zWidth: 8, //阴影折角宽        
        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 - 24, shape.y];
        const p4 = [shape.x + 24, shape.y];
        const p2 = [xAxisPoint[0] - 24, xAxisPoint[1]];
        const p3 = [xAxisPoint[0] + 24, 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: 48,
        zWidth: 8,
        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 - 24, shape.y];
        const p4 = [shape.x + 24, shape.y];
        const p6 = [shape.x + 24 + 8, shape.y - 4];
        const p7 = [shape.x - 24 + 8, shape.y - 4];
        const p3 = [xAxisPoint[0] + 24, xAxisPoint[1]];
        const p5 = [xAxisPoint[0] + 24 + 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);
option = {
    grid: {
        height: 300
    },
    xAxis: {
        data: ['one', 'two']
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        type: 'custom',
        renderItem: function (params, api) {
            let location = api.coord([api.value(0), api.value(1)]);
            return {
                type: 'group',
                children: [{
                    type: 'MyCubeRect',
                    shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1]
                    },
                    style: {
                        fill: '#5AD8A6'
                    }
                },{
                    type: 'MyCubeShadow',
                    shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1]
                    },
                    style: {
                        fill: '#1F986A'
                    }
                }]
            };
        },
        data: [20, 60]
    }]
};
2:基于y轴的伪3d柱状图实现:

var MyCubeRect = echarts.graphic.extendShape({
    shape: {
        x: 0,
        y: 0,
        width: 48, //柱宽        
        zWidth: 8, //阴影折角宽        
        zHeight: 4, //阴影折角高 
    },
    buildPath: function (ctx, shape) {
        const api = shape.api;
      
        const yAxisPoint = api.coord([shape.yValue, 0]);
        const p0 = [shape.y, shape.x];
        const p1 = [shape.y - 24, shape.x];
        const p4 = [shape.y + 24, shape.x];
        const p2 = [shape.y - 24, yAxisPoint[0]];
        const p3 = [shape.y + 24, yAxisPoint[0]];
        
    

        ctx.moveTo(p0[1],p0[0]);
        ctx.lineTo(p1[1], p1[0]); //1
        ctx.lineTo(p2[1], p2[0]); //2
        ctx.lineTo(p3[1], p3[0]); //3
        ctx.lineTo(p4[1], p4[0]); //4
        ctx.lineTo(p0[1], p0[0]); //0
        ctx.closePath();
    }
});
var MyCubeShadow = echarts.graphic.extendShape({
    shape: {
        x: 0,
        y: 0,
        width: 48,
        zWidth: 8,
        zHeight: 4,
    },
    buildPath: function (ctx, shape) {
        const api = shape.api;
        const yAxisPoint = api.coord([shape.yValue, 0]);
        const p0 = [shape.y, shape.x];
        const p1 = [shape.y - 24, shape.x];
        const p4 = [shape.y + 24, shape.x];
     
        const p6 = [shape.y + 24 - 8, shape.x + 4];
        const p7 = [shape.y - 24 - 8, shape.x + 4];
        const p3 = [shape.y - 24, yAxisPoint[0]];
        const p5 = [shape.y - 24 - 8, yAxisPoint[0] + 4];
       
        const p2 = [shape.y - 24, shape.x];
     
        const p8 = [shape.y - 24 - 8, shape.x + 4];
      
        ctx.moveTo(p2[1], p2[0]); //4
        ctx.lineTo(p3[1], p3[0]); //3
        ctx.lineTo(p5[1], p5[0]); //5
        ctx.lineTo(p8[1], p8[0]); //6
        ctx.lineTo(p2[1], p2[0]); //4
    
      
        ctx.moveTo(p4[1], p4[0]); //4
        ctx.lineTo(p6[1], p6[0]); //6
        ctx.lineTo(p7[1], p7[0]); //7
        ctx.lineTo(p1[1], p1[0]); //1
        ctx.lineTo(p4[1], p4[0]); //4
        ctx.closePath();
    }
});
echarts.graphic.registerShape('MyCubeRect', MyCubeRect);
echarts.graphic.registerShape('MyCubeShadow', MyCubeShadow);
option = {
    grid: {
        height: 300
    },
    xAxis: {
        // data: ['one', 'two']
        type: 'value'
    },
    yAxis: {
    //   type: 'value'
    
          data: ['one', 'two','three']
    },
    series: [{
        type: 'custom',
        renderItem: function (params, api) {
            let location = api.coord([api.value(0), api.value(1)]);
            // alert(api.value(0));
            // console.info(api.value(0));
            // console.info(api.value(1));
            //20,60
            return {
                type: 'group',
                children: [{
                    type: 'MyCubeRect',
                    shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1]
                    },
                    style: {
                        fill: '#5AD8A6'
                    }
                },{
                    type: 'MyCubeShadow',
                    shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1]
                    },
                    style: {
                        fill: '#1F986A'
                    }
                }]
            };
        },
        data: [100,200,3000]
    }]
};
 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Echarts放到高德地图上可以实现在地图上展示数据的功能。具体实现步骤如下: 1. 引入Echarts和高德地图的相关库文件。 2. 创建一个地图容器,用于显示地图。 3. 初始化地图,并设置地图的心点和缩放级别。 4. 创建一个Echarts实例,并将地图容器作为参数传入。 5. 在Echarts实例配置相应的数据和样式,例如散点图、柱状图等。 6. 调用Echarts方法将配置好的数据和样式渲染到地图上。 下面是一个示例代码,演示了如何将Echarts放到高德地图上展示散点图: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Echarts放到高德地图上</title> <style> #map { width: 100%; height: 600px; } </style> </head> <body> <div id="map"></div> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> <script src="https://webapi.amap.com/maps?v=1.4.15&key=your_amap_key"></script> <script> // 创建地图容器 var map = new AMap.Map('map', { center: [116.397428, 39.90923], // 地图心点坐标 zoom: 10 // 地图缩放级别 }); // 创建Echarts实例 var myChart = echarts.init(map); // 配置散点图数据和样式 var option = { series: [{ type: 'scatter', data: [ [116.397428, 39.90923], [116.410049, 39.914889], [116.412049, 39.916889] ], symbolSize: 10 }] }; // 渲染到地图上 myChart.setOption(option); </script> </body> </html> ``` 通过以上代码,可以在地图上展示一个散点图,散点的坐标为[116.397428, 39.90923]、[116.410049, 39.914889]和[116.412049, 39.916889]。你可以根据自己的需求修改数据和样式。
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值