【图表】highcharts 3D立体饼图渐变带背景 vue

本文通过一个实例展示了如何在Vue项目中使用Highcharts库创建3D饼图,包括安装依赖、组件创建和配置图表选项。Highcharts相比echarts在创建3D图表时更为便捷,代码可直接复制使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 前提:echarts画3d图没有highcharts方便  (复制代码直接使用)

首先安装依赖:

npm install highcharts --save   

新建页面:index.vue

<template>
  <div class="safeEvent">
    <Pie3D :optionData="optionData" style="width:400px;height:200px" />
  </div>
</template>
<script>
import Pie3D from "@/components/Echarts/Pie3D/index.vue";
export default {
  name: "home",
  data() {
    return {
      optionData: [
        {
          name: "数据1",
          value: 30,
        },
        {
          name: "数据2",
          value: 20,
        },
        {
          name: "数据3",
          value: 30,
        },
        {
          name: "数据4",
          value: 10,
        },
        {
          name: "数据5",
          value: 10,
        },
      ],
    };
  },
  components: {
    Pie3D,
  },
};
</script>

  新建组件:Pie3D.vue

<!-- 渐变3D饼图样式-->
<template>
  <div id="" style="height: 100%">
    <div id="container" style="height: 100%"></div>
    <!-- 底座背景 -->
    <div class="bg"></div>
  </div>
</template>

<script>
import Highcharts from "highcharts/highstock";
import HighchartsMore from "highcharts/highcharts-more";
import HighchartsDrilldown from "highcharts/modules/drilldown";
import Highcharts3D from "highcharts/highcharts-3d";

HighchartsMore(Highcharts);
HighchartsDrilldown(Highcharts);
Highcharts3D(Highcharts);

let chart = null;
export default {
  name: "",
  props: {
    optionData: {
      type: Array,
      default: () => {
        return [];
      },
    },
  },
  watch: {
    optionData: {
      deep: true,
      handler() {
        this.$nextTick(() => {
          this.dealData();
          this.init();
        });
      },
    },
  },
  data() {
    return {
      dataList: [],
    };
  },
  mounted() {
    this.init();
    window.addEventListener("resize", () => {
      //自适应
      this.init();
      chart.reflow();
    });
  },
  created() {
    this.dealData(); //处理父组件传过来的数据
  },
  methods: {
    dealData() {
      this.dataList = this.optionData.map((item) => {
        item["y"] = item["value"];
        return item;
      });
    },
    init() {
      let color1 = [
        "rgb(187, 180, 255)",
        "rgb(234, 189, 253)",
        "rgb(255, 250, 164)",
        "rgb(29, 241, 255)",
        "rgb(42, 250, 230)",
        "rgb(255, 216, 183)",
        "rgb(187, 180, 255)",
      ];
      let color2 = [
        "rgb(109, 94, 255, 1)",
        "rgb(194, 121, 225)",
        "rgb(195, 179, 57)",
        "rgb(12, 122, 210)",
        "rgb(42, 250, 230)",
        "rgb(247, 163, 92)",
        "rgb(109, 94, 255)",
      ];

      // 饼图渐变效果
      Highcharts.getOptions().colors = Highcharts.map(
        Highcharts.getOptions().colors,
        function (color, index) {
          return {
            radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
            stops: [
              [0, color2[index]],
              [0.2, color2[index]],
              [1, color1[index]], // darken
            ],
          };
        }
      );

      if (this.charts) {
        chart.destroy();
      }
      // 初始化
      chart = Highcharts.chart("container", {
        chart: {
          type: "pie",
          backgroundColor: null, //去掉背景颜色
          animation: false,
          marginTop: 10,
          options3d: {
            enabled: true,
            alpha: 60, //倾斜角度
          },
          spacingTop: 0,
          spacingLeft: 0,
          spacingRight: 0,
        },
        legend: {
          align: "center", //程度标的目标地位
          layout: "horizontal",
          verticalAlign: "top", //垂直标的目标地位
          padding: 2,
          margin: 0,
          itemStyle: {
            cursor: "pointer",
            color: "#FFFFFF",
            fontWeight: 100,
            backgroundColor: ["#ccc"],
          },
          itemHoverStyle: {
            color: "#FFFFFF",
          },
        },
        tooltip: {
          //提示框
          backgroundColor: "rgba(20,77,155,0.9000)",
          borderColor: "#34A6FF",
          color: "#fff",
          useHTML: true,
          pointFormat: '<div style="color: #fff">{point.percentage:.1f}%</div>',
          itemStyle: {
            color: "#FFFFFF",
          },
          formatter: function () {
            let s =
              '<div  style="color: #fff">' +
              this.key +
              ":" +
              this.y +
              "</div>";
            return s;
          },
        },
        title: {
          text: null,
        },
        credits: {
          enabled: false, //去掉右下角水印
        },
        plotOptions: {
          pie: {
            allowPointSelect: false,
            cursor: "pointer",
            depth: 35,
            textShadow: false,
            softConnector: false,
            dataLabels: {
              enabled: true,
              alignTo: "toPlotEdges",
              y: -14,
              // 这里是标签的引导线
              connectorShape: function (
                labelPosition,
                connectorPosition,
                options
              ) {
                var touchingSliceAt = connectorPosition.touchingSliceAt,
                  alignment = labelPosition.alignment,
                  left = 0,
                  right = 0;
                if (alignment == "left") {
                  left = labelPosition.x + this.dataLabel.bBox.width + 14;
                  right = labelPosition.x + 2;
                } else {
                  left = labelPosition.x - this.dataLabel.bBox.width - 14;
                  right = labelPosition.x - 2;
                }
                return [
                  "M",
                  left,
                  labelPosition.y,
                  "L",
                  right,
                  labelPosition.y,
                  "L",
                  touchingSliceAt.x,
                  touchingSliceAt.y,
                ];
              },
              formatter: function () {
                return this.y + "%";
              },
              distance: 20,
              style: {
                color: "#FFFFFF",
                fontSize: "12px",
                textOutline: "none",
                fontWeight: "400",
              },
            },
            states: {
              inactive: {
                opacity: 1,
                size: "100%",
              },
            },
          },
        },
        series: [
          {
            type: "pie",
            name: "Browser share",
            showInLegend: true, // 是否显示图例
            data: this.dataList, //传入数据
          },
        ],
      });
    },
  },
};
</script>

<style lang='scss' scoped>
.bg {
  z-index: -1;
  width: 66%;
  height: 70%;
  margin-top: -37%;
  margin-left: 18%;
  position: relative;
  background-image: url("../../../assets/home/dizuo.png");
  background-size: 100% 100%;
}
</style>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值