vue-echarts + vue3 立體柱形圖

如圖:


image.png

需要結合vue-echarts、echarts完成。vue-echarts官網(wǎng)
我這里遇到一個問題,npm vue-echarts一直失敗.解決方法:npm指定下載版本號,我是下載的:

npm i vue-echarts@^6.0.0-rc.4
image.png

main.js:

import { createApp } from 'vue'
import App from './App.vue'

import ECharts from 'vue-echarts';
import { graphic } from 'echarts/index';

const app = createApp(App)
app.provide('graphic', graphic);
app.component('VChart', ECharts);
app.mount('#app')

vue文件:

<template>
    <div>
        <v-chart class="chart" :option="option" />
    </div>
</template>
<script setup>
import { inject,reactive } from 'vue'

const graphic = inject('graphic');
// 繪制左側面
const CubeLeft = graphic.extendShape({
  shape: {
    x: 0,
    y: 0
  },
  buildPath(ctx, shape) {
    // 會canvas的應該都能看得懂,shape是從custom傳入的
    const { xAxisPoint } = shape;
    // console.log(shape)
    const c0 = [shape.x + 17, shape.y];
    const c1 = [shape.x - 15, shape.y];
    const c2 = [xAxisPoint[0] - 15, xAxisPoint[1]];
    const c3 = [xAxisPoint[0] + 17, 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 = graphic.extendShape({
  shape: {
    x: 0,
    y: 0
  },
  buildPath(ctx, shape) {
    const { xAxisPoint } = shape;
    const c1 = [shape.x + 17, shape.y];
    const c2 = [xAxisPoint[0] + 17, xAxisPoint[1]];
    const c3 = [xAxisPoint[0] + 25, xAxisPoint[1] - 5];
    const c4 = [shape.x + 25, shape.y - 8];
    ctx
      .moveTo(c1[0], c1[1])
      .lineTo(c2[0], c2[1])
      .lineTo(c3[0], c3[1])
      .lineTo(c4[0], c4[1])
      .closePath();
  }
});
// 繪制頂面
const CubeTop = graphic.extendShape({
  shape: {
    x: 0,
    y: 0
  },
  buildPath(ctx, shape) {
    const c1 = [shape.x + 17, shape.y];
    const c2 = [shape.x + 25, shape.y - 8]; // 右點
    const c3 = [shape.x - 5, shape.y - 8];
    const c4 = [shape.x - 15, shape.y];
    ctx
      .moveTo(c1[0], c1[1])
      .lineTo(c2[0], c2[1])
      .lineTo(c3[0], c3[1])
      .lineTo(c4[0], c4[1])
      .closePath();
  }
});

graphic.registerShape('CubeLeft', CubeLeft);
graphic.registerShape('CubeRight', CubeRight);
graphic.registerShape('CubeTop', CubeTop);
const option = reactive({
  grid: {
    left: '10%',
    right: '5%',
    top: '10%',
    bottom: '20%'
  },
  xAxis: {
    type: 'category',
    data: [],
    axisTick: {
      show: false
    },
    axisLine: {
      show: false,
      lineStyle: {
        type: 'dotted',
        width: 1,
        color: '#ffffff',
        opacity: 0.5
      }
    },
    axisLabel: {
      show: true,
      color: '#ffffff',
      interval: 0,
      rotate: 30,
      fontSize: 12
    }
  },
  yAxis: {
    type: 'value',
    splitLine: {
      // 分隔線
      show: true, // 默認顯示,屬性show控制顯示與否
      lineStyle: {
        type: 'dotted',
        color: '#ffffff',
        opacity: 0.5
      }
    }
  },
  series: [
    {
      type: 'custom',
      renderItem: (params, api) => {
        // console.log(params)
        const location = api.coord([api.value(0), api.value(1)]);
        return {
          type: 'group',
          children: [
            {
              type: 'CubeLeft',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: graphic.LinearGradient(1, 1, 0, 0, [
                  { offset: 1, color: '#3d65ec' },
                  { offset: 0, color: '#8193d0' }
                ])
              }
            },
            {
              type: 'CubeRight',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: new graphic.LinearGradient(1, 1, 0, 0, [
                  { offset: 1, color: '#3755b8' },
                  { offset: 0, color: '#5972cb' }
                ])
              }
            },
            {
              type: 'CubeTop',
              shape: {
                api,
                xValue: api.value(0),
                yValue: api.value(1),
                x: location[0],
                y: location[1],
                xAxisPoint: api.coord([api.value(0), 0])
              },
              style: {
                fill: new graphic.LinearGradient(1, 1, 0, 0, [
                  { offset: 1, color: '#8193d0' },
                  { offset: 0, color: '#3d65ec' }
                ])
              }
            }
          ]
        };
      },
      data: [1,2,3,4,5]
    }
  ]
});

// })



</script>
<style>
.chart {
  width:  400px;
  height: 600px;   
}
</style>

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容