echarts使用自定義圖形實現3D柱狀圖

先看下效果吧

custom3dBar

實現思路

  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.pswp.cn/diannao/44435.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/44435.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/44435.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c++ primer plus 第15章友,異常和其他,15.3.8exception 類

c primer plus 第15章友&#xff0c;異常和其他,15.3.8exception 類 15.3.8exception 類 文章目錄 c primer plus 第15章友&#xff0c;異常和其他,15.3.8exception 類15.3.8exception 類1.stdexcept異常類3.空指針和 new 15.3.8exception 類 C異常的主要目的是為設計容錯程序…

NVIDIA良心給顯卡免費升級,只為挨更多的罵

起猛了&#xff0c;還真的以為 NVIDIA 良心發現了。 眾所周知&#xff0c;英偉達對于咱們普通游戲玩家向來不屑一顧。只因為游戲業務在 NVIDIA 收入中占比較少。 在最新的 40 系顯卡 RTX 4070 Ti Super 顯卡中&#xff0c;NVIDIA悄悄給它來了一次核心「升級」&#xff0c;將原…

ARM學習(29)NXP 雙coreMCU IMX1160學習----NorFlash 啟動引腳選擇

ARM學習&#xff08;28&#xff09;NXP 雙coreMCU IMX1160學習----NorFlash 啟動引腳選擇 1、多種啟動方式介紹 IMX1166 支持多組flexSPI 引腳啟動&#xff0c;FlexSPI1以及FlexSPI2&#xff0c;通過boot cfg可以切換FlexSPI得實例。 每個實例又支持多組引腳&#xff0c;總共…

Subclass-balancing Contrastive Learning for Long-tailed Recognition

Subclass-balancing Contrastive Learning for Long-tailed Recognition 核心公式解析溫度參數 τ \tau τ的作用公式5解析 核心公式解析 L S B C L ? ∑ i 1 N ( 1 ∣ M ~ i ∣ ∑ z p ∈ M ~ i log ? exp ? ( z i ? z p ? / τ 1 ) ∑ z a ∈ V ~ i exp ? ( z i ? z…

LiteOS增加執行自定義源碼

開發過程注意事項&#xff1a; 源碼工程路徑不能太長 源碼工程路徑不能有中文 一定要關閉360等殺毒軟件&#xff0c;否則編譯的打包階段會出錯 增加自定義源碼的步驟: 1.創建源碼目錄 2. 創建源文件 新建myhello目錄后&#xff0c;再此目錄下再新建源文件myhello_demo.c 3. 編…

程序員學長 | PyCaret,一個超強的 python 庫

本文來源公眾號“程序員學長”&#xff0c;僅用于學術分享&#xff0c;侵權刪&#xff0c;干貨滿滿。 原文鏈接&#xff1a;PyCaret&#xff0c;一個超強的 python 庫 今天給大家分享一個超強的 python 庫&#xff0c;PyCaret。 https://github.com/pycaret/pycaret 簡介 …

[論文筆記]RAPTOR: RECURSIVE ABSTRACTIVE PROCESSING FOR TREE-ORGANIZED RETRIEVAL

引言 今天帶來又一篇RAG論文筆記&#xff1a;RAPTOR: RECURSIVE ABSTRACTIVE PROCESSING FOR TREE-ORGANIZED RETRIEVAL。 檢索增強語言模型能夠更好地適應世界狀態的變化并融入長尾知識。然而&#xff0c;大多數現有方法只能從檢索語料庫中檢索到短的連續文本片段&#xff0…

random.choices 的參數及其應用

random.choices 是 Python 的 random 模塊中的一個函數&#xff0c;用于從給定的序列中隨機選擇元素&#xff0c;可以設置權重。這個函數在需要根據特定概率分布進行隨機選擇的場景中非常有用。下面是 random.choices 的參數及其詳細介紹&#xff1a; 文章目錄 參數應用示例基本…

釋放序列和同步

#include <iostream> #include<thread> #include<atomic> #include<vector> std::atomic<int>count(0); std::vector<int>queue_data; //如果存儲操作被標記為memory_order_release、memory_order_acq_rel或memory_order_seq_cst&#xff…

FP5207+音頻功率放大器的組合解決方案-適用于便攜式音頻播放器、無線耳機、智能音箱和車載音響系統等高質量音頻輸出需求的產品,以提高電池供電的效率和輸出功率

隨著消費者對智能家居的需求增長&#xff0c;智能音響市場成為重要增長點。同時&#xff0c;音響技術也在不斷發展&#xff0c;音響及揚聲器的功能和性能不斷提升。 藍牙音箱&#xff0c;這類音箱供電是以鋰電池為主&#xff0c;一般選用內置升壓的音頻功放芯片&#xff0c;音響…

iOS input 標簽 focus 失效

解決方案 <inputv-if"show"ref"inputRef" />watch(inputRef, (ref) > {ref?.focus(); });

vivado DQS_BIAS

DQS_偏差 DQS_BIAS是驅動差分輸入緩沖器的頂級端口的屬性&#xff0c;或者 雙向緩沖基元&#xff08;IBUFDS、IOBUFDS&#xff09;。 DQS_BIAS屬性在某些的輸入端提供可選的DC偏置 偽差分I/O標準&#xff08;DIFF_SSTL&#xff09;和真差分I/O規范&#xff08;LVDS&#xff09;…

windows 構建nginx本地服務隨系統自啟

1.先去官網下載一個nginxzip 2.將zip解壓&#xff0c;將nginx-server.exe文件放入文件夾 3.創建nginx-server.xml&#xff0c;將以下內容放進文件內 <service> <id>nginx</id> <name>Nginx Service</name> <description>High Per…

強化學習中的蒙特卡洛算法和時序差分算法

在強化學習&#xff08;Reinforcement Learning, RL&#xff09;中&#xff0c;價值函數的估計是核心任務之一。蒙特卡洛&#xff08;Monte Carlo, MC&#xff09;方法和時序差分&#xff08;Temporal Difference, TD&#xff09;方法是兩種常用的策略&#xff0c;用于估計狀態…

軟件架構之架構風格

軟件架構之架構風格 9.3 軟件架構風格9.3.1 軟件架構風格分類9.3.2 數據流風格9.3.3 調用/返回風格9.3.4 獨立構件風格9.3.5 虛擬機風格9.3.6 倉庫風格 9.4 層次系統架構風格9.4.1 二層及三層 C/S 架構風格9.4.2 B/S 架構風格9.4.3 MVC 架構風格9.4.4 MVP 架構風格 9.5 面向服務…

機器學習筑基篇,?Ubuntu 24.04 編譯安裝 Python 及多版本切換

[ 知識是人生的燈塔&#xff0c;只有不斷學習&#xff0c;才能照亮前行的道路 ] Ubuntu 24.04 編譯安裝最新Python及多版本切換 描述&#xff1a;說到機器學習&#xff0c;人工智能&#xff0c;深度學習不免會提到Python這一門編程語言&#xff08;人生苦短&#xff0c;及時Pyt…

windows防火墻端口設置

PS&#xff1a;本文實例為Windows Server 2019&#xff0c;其他Windows版本大同小異。 1、首先打開windows防火墻&#xff0c;點擊“高級設置” 2、 高級設置界面 3、假設需要開放一個端口為3306應該怎么做 光標對準“入站規則”右鍵新建規則&#xff0c;選擇“端口” 協議這…

C++類和對象(一)

目錄 面向過程和面向對象 面向過程編程&#xff08;Procedural Programming&#xff09; 面向對象編程&#xff08;Object-Oriented Programming&#xff09; 一、類的定義 類定義格式 類域 二、類的訪問限定符及封裝 訪問限定符 封裝 三、實例化 實例化概念 對象大小…

軟件運行次數

題目&#xff1a; 實現一個驗證程序運行次數的小程序&#xff0c;要求如下&#xff1a; 當程序運行超過3次時給出提示&#xff1a;本軟件只能免費使用3次&#xff0c;歡迎您注冊會員后繼續使用&#xff5e;程序運行演示如下&#xff1a; 第一次運行控制臺輸出&#xff1a;歡迎…

常見WAF攔截頁面總結

(1) D盾 (2) 云鎖 (3) UPUPW安全防護 (4) 寶塔網站防火墻 (5) 網防G01 (6) 護衛神 (7) 網站安全狗 (8) 智創防火墻 (9) 360主機衛士或360webscan (10) 西數WTS-WAF (11) Naxsi WAF (12) 騰訊云 (13) 騰訊宙斯盾 (14) 百度云 圖片 (15) 華為云 (16) 網宿云 (17) 創宇盾 圖片 (…