vue中实现3d饼图

项目中要求实现3d饼图,但是看了echarts图标,代码太多,想找个简单点的,就找到了highcharts

目前 Highcharts的默认版本已经进入了 v12系列,所以代码上也有所改动

实现效果图

在这里插入图片描述

实现代码

安装
// 目前所需要的插件 
npm install highcharts
// 目前下载时所用的版本为12.4.0
引入
import Highcharts from 'highcharts';
import 'highcharts/highcharts-3d';
3D饼图完整代码
<template>
  <div id="EchartsPie" class="EchartsPie"></div>
</template>

<script setup>
import Highcharts from 'highcharts';
import 'highcharts/highcharts-3d';

// 设置全局语言选项,修复 Invalid language tag 错误
Highcharts.setOptions({
  lang: {
    locale: 'zh-CN' // 使用标准的简体中文语言标签
  }
});

// 数据
const list = [
  { value: 1048, name: "Search Engine" },
  { value: 735, name: "Direct" },
  { value: 580, name: "Email" },
  { value: 484, name: "Union Ads" },
  { value: 300, name: "Video Ads" },
];

const data = list.map(e => ({
  name: e.name,
  y: e.value,
}));

const queryPie = () => {
  Highcharts.chart('EchartsPie', {
    chart: {
      type: 'pie',
      options3d: {
        enabled: true,
        alpha: 45, // 饼图倾斜角度
        beta: 0,   // 饼图旋转角度
        depth: 35, // 3D饼图的厚度
      },
    },
    title: {
      text: '3D饼图 - 流量来源分布',
      style: {
        fontSize: '18px',
        fontWeight: 'bold'
      }
    },
    subtitle: {
      text: '点击切片可分离查看详情',
      style: {
        fontSize: '12px',
        color: '#666'
      }
    },
    tooltip: {
      pointFormat: '<b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
      pie: {
        allowPointSelect: true, // 允许点击选择切片
        cursor: 'pointer',
        depth: 35, // 饼图厚度
        dataLabels: {
          enabled: true,
          format: '<b>{point.name}</b>: {point.percentage:.1f} %',
          style: {
            color: '#333',
            fontSize: '12px',
            textOutline: 'none'
          }
        },
        showInLegend: true // 显示图例
      }
    },
    legend: {
      layout: 'vertical',
      align: 'right',
      verticalAlign: 'middle',
      itemStyle: {
        fontSize: '12px',
        color: '#333'
      }
    },
    colors: ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7'],
    series: [{
      name: '占比',
      data: data,
      colorByPoint: true, // 让每个切片有不同的颜色
    }],
  });
};

import { onMounted } from 'vue';
onMounted(() => {
  queryPie();
});
</script>

<style scoped>
.EchartsPie {
  width: 100%;
  height: 500px;
}
::v-deep .highcharts-credits {
  display: none;
}
</style>

改的时候 刚好写到了3D柱状图,那就也记录下吧

3D柱状图的完整代码
<template>
  <div id="EchartsBar" class="EchartsBar"></div>
</template>

<script setup>
import Highcharts from 'highcharts';
import 'highcharts/highcharts-3d';

// 设置全局语言选项,修复 Invalid language tag 错误
Highcharts.setOptions({
  lang: {
    locale: 'zh-CN' // 使用标准的简体中文语言标签
  }
});

// 数据
const list = [
  { value: 1048, name: "Search Engine" },
  { value: 735, name: "Direct" },
  { value: 580, name: "Email" },
  { value: 484, name: "Union Ads" },
  { value: 300, name: "Video Ads" },
];

const data = list.map(e => ({
  name: e.name,
  y: e.value,
}));

const queryBar = () => {
  Highcharts.chart('EchartsBar', {
    chart: {
      type: 'column',
      options3d: {
        enabled: true,
        alpha: 45,
        beta: 0,
        depth: 100,
      },
    },
    title: {
      text: '3D柱状图',
    },
    tooltip: {
      pointFormat: '<b>{point.y}</b>'
    },
    xAxis: {
      type: 'category',
    },
    yAxis: {
      title: {
        text: '数值',
      },
    },
    legend: {
      enabled: false,
    },
    plotOptions: {
      column: {
        depth: 100,
        dataLabels: {
          enabled: true,
          format: '{point.y}'
        },
      },
    },
    series: [
      {
        name: '数据',
        data: data,
        colorByPoint: true, // 让每个柱子有不同的颜色
      },
    ],
  });
};

import { onMounted } from 'vue';
onMounted(() => {
  queryBar();
});
</script>

<style scoped>
.EchartsBar {
  width: 100%;
  height: 500px;
}
::v-deep .highcharts-credits {
  display: none;
}
</style>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值