echarts使用自定义图形实现3D柱状图

news/2024/10/17 19:51:37/

先看下效果吧

custom<a class=3dBar" />

实现思路

  1. 使用graphic创建并注册自定义图形。根据每组的数据值,得到一个对应的点,从点出发用canvas绘制一组图形,分别为
    顶部的菱形
    top
    const CubeTop = echarts.graphic.extendShape({buildPath: function (ctx, shape) {const c1 = [shape.x, shape.y]; // 下const c2 = [shape.x + 9, shape.y - 7]; // 右const c3 = [shape.x, shape.y - 12]; // 上const c4 = [shape.x - 9, shape.y - 7]; // 左ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();}
    });
    
    左侧的四边形 left
    const CubeLeft = echarts.graphic.extendShape({buildPath: function (ctx, shape) {const xAxisPoint = shape.xAxisPoint;const c0 = [shape.x, shape.y]; // 右上const c1 = [shape.x - 9, shape.y - 7]; //左上const c2 = [xAxisPoint[0] - 9, xAxisPoint[1] - 6]; // 左下const c3 = [xAxisPoint[0], xAxisPoint[1]]; // 右下ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath();}
    });
    
    右侧的四边形
    right
    const CubeRight = echarts.graphic.extendShape({buildPath: function (ctx, shape) {const xAxisPoint = shape.xAxisPoint;const c1 = [shape.x, shape.y]; // 左上const c2 = [xAxisPoint[0], xAxisPoint[1]]; // 左下const c3 = [xAxisPoint[0] + 9, xAxisPoint[1] - 7]; //右下const c4 = [shape.x + 9, shape.y - 7]; // 右上ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();}
    });
    
  2. 用series自定义系列(custom)的renderItem将这一组图形元素返回,组合形成3D柱状图

代码实现

<template><div id="graphicBar"></div>
</template><script setup>import {reactive, onMounted} from 'vue'import * as echarts from "echarts";const barData = reactive({xAxis: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],data: [200, 180, 120, 220, 80, 160, 150]})const customShape = () => {// 创建自定义的shape类型const CubeLeft = echarts.graphic.extendShape({buildPath: function (ctx, shape) {const xAxisPoint = shape.xAxisPoint;const c0 = [shape.x, shape.y]; // 右上const c1 = [shape.x - 9, shape.y - 7]; //左上const c2 = [xAxisPoint[0] - 9, xAxisPoint[1] - 6]; // 左下const c3 = [xAxisPoint[0], xAxisPoint[1]]; // 右下ctx.moveTo(c0[0], c0[1]).lineTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).closePath();}});const CubeRight = echarts.graphic.extendShape({buildPath: function (ctx, shape) {const xAxisPoint = shape.xAxisPoint;const c1 = [shape.x, shape.y]; // 左上const c2 = [xAxisPoint[0], xAxisPoint[1]]; // 左下const c3 = [xAxisPoint[0] + 9, xAxisPoint[1] - 7]; //右下const c4 = [shape.x + 9, shape.y - 7]; // 右上ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();}});const CubeTop = echarts.graphic.extendShape({buildPath: function (ctx, shape) {const c1 = [shape.x, shape.y]; // 下const c2 = [shape.x + 9, shape.y - 7]; // 右const c3 = [shape.x, shape.y - 12]; // 上const c4 = [shape.x - 9, shape.y - 7]; // 左ctx.moveTo(c1[0], c1[1]).lineTo(c2[0], c2[1]).lineTo(c3[0], c3[1]).lineTo(c4[0], c4[1]).closePath();}});// 注册创建的自定义的shape类型echarts.graphic.registerShape('CubeLeft', CubeLeft);echarts.graphic.registerShape('CubeRight', CubeRight);echarts.graphic.registerShape('CubeTop', CubeTop);}const draw_bar = () => {customShape()const option = {xAxis: {data: barData.xAxis,axisLabel: {fontSize: 12,color: '#FFFFFF'},axisLine: {lineStyle: {color: '#3A4547',}},axisTick: {show: false}},yAxis: {type: 'value',axisLabel: {fontSize: 12,color: '#A8B5C1'},splitLine: {lineStyle: {color: ['#303638'],type: 'dashed'}}},grid: {containLabel: true,top: 10,bottom: 0,right: 0,left: 0},series: [        {type: 'custom',renderItem: (params, api) => {// coord 将数据值映射到坐标系上// api.value 给定维度的数据值const location = api.coord([api.value(0), api.value(1)]);return {type: 'group',children: [{type: 'CubeLeft',shape: {api,x: location[0], // 图形元素的右上角在父节点坐标系中的横坐标值y: location[1], // 图形元素的右上角在父节点坐标系中的纵坐标值xAxisPoint: api.coord([api.value(0), 0]) // 图形元素的右下角在父节点坐标系中的坐标值},style: {// 渐变色填充fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{offset: 0,color: 'rgba(35, 153, 254, 1)'},{offset: 1,color: 'rgba(70, 207, 255, 1)'},])}},{type: 'CubeRight',shape: {api,x: location[0], // 中间上的xy: location[1], // 中间上的yxAxisPoint: api.coord([api.value(0), 0]) // 中间下},style: {fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{offset: 0,color: 'rgba(32, 147, 255, 1)'},{offset: 1,color: 'rgba(71, 237, 255, 1)'},])}},{type: 'CubeTop',shape: {api,x: location[0],y: location[1],},style: {fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{offset: 0,color: 'rgba(107, 230, 254, 1)'},{offset: 1,color: 'rgba(48, 211, 255, 1)'}])}}]};},data: barData.data}]};return option}const chart_init = () => {let curChart = echarts.init(document.getElementById('graphicBar'))const exampleOption = draw_bar()curChart.setOption(exampleOption);}onMounted(() => {chart_init()})
</script><style scoped>#graphicBar{width: 460px;height: 300px;}
</style>

补充说明

  1. 以上内容是vite构建的vue3项目
  2. echarts版本5.5.1

http://www.ppmy.cn/news/1474449.html

相关文章

bqplot教程:在Jupyter Notebook中进行交互式数据可视化

文章目录 介绍1.1 数据可视化的重要性1.2 bqplot库的概述安装和快速入门 安装和导入2.1 安装bqplot使用pip安装使用conda安装 2.2 导入必要的库示例&#xff1a;导入并使用bqplot创建简单图表 数据集准备3.1 导入数据集使用 pandas 导入 CSV 文件使用 pandas 导入其他格式的数据…

ubuntu系统下使用gelsight

一、背景 创建 conda create -n gelsight python3.8 conda activate gelsight cd GelSight/gsrobotics-main/demos/marker_tracking/ python3 mean_shift_marker_tracking.py ModuleNotFoundError: No module named ‘numpy’ pip3 install numpy ModuleNotFoundError: N…

2024机器遗忘(Machine Unlearning)技术分类-思维导图

1 介绍 机器遗忘&#xff08;Machine Unlearning&#xff09;是指从机器学习模型中安全地移除或"遗忘"特定的数据点或信息。这个概念源于数据隐私保护的需求&#xff0c;尤其是在欧盟通用数据保护条例&#xff08;GDPR&#xff09;等法规中提出的"被遗忘的权利…

Rust开发环境搭建

Rust开发环境搭建 环境 rust: 1.79.0(2024-06-13)1. Rustup下载器在线安装 windows&#xff1a; https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe unix&#xff1a; curl --proto https --tlsv1.2 -sSf https://sh.rustup.rs | sh2. R…

【NLP学习笔记】transformers中的tokenizer切词时是否返回token_type_ids

结论 先说结论&#xff1a; 是否返回token_type_ids&#xff0c;可以在切词时通过 return_token_type_idsTrue/False指定&#xff0c;指定了True就肯定会返回&#xff0c;指定False&#xff0c;不一定就不返回。 分析 Doc地址 https://huggingface.co/docs/transformers/main…

已解决 javax.xml.transform.TransformerFactoryConfigurationError 异常的正确解决方法,亲测有效!!!

已解决 javax.xml.transform.TransformerFactoryConfigurationError 异常的正确解决方法&#xff0c;亲测有效&#xff01;&#xff01;&#xff01; 目录 一、问题分析 二、报错原因 三、解决思路 四、解决方法 五、总结 博主v&#xff1a;XiaoMing_Java 博主v&#x…

防火墙基础实验配置

一&#xff0c;实验拓扑 二&#xff0c;实验需求&#xff1a; 1.DMZ区内的服务器&#xff0c;办公区仅能在办公时间内&#xff08;9&#xff1a;00 - 18&#xff1a;00&#xff09;可以访问&#xff0c;生产区的设备全天可以访问 2.生产区不允许访问互联网&#xff0c;办公区…

Java基础(第一章)

Java语言是面向对象的&#xff08;oop)java语言是健壮的&#xff0c;Java的强类型机制&#xff0c;异常处理&#xff0c;垃圾自动搜集等是Java程序健壮性的重要保障。Java语言是跨平台的&#xff0c;是一种解释型语言。 Java运行机制及运行过程 *java核心机制-Java虚拟机&…