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

 前提: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>

Highcharts中,您可以使用渐变色3D饼图添加视觉效果。在给定的引用代码中,使用官方示例代码创建了渐变色渐变色的构建是通过修改Highcharts选项中的颜色数组实现的。首先,使用`Highcharts.getOptions().colors`获取默认颜色数组,然后使用`Highcharts.map()`方法对每个颜色进行处理,并返回一个包含渐变设置的对象。对象中的`radialGradient`属性定义了渐变的配置,其中`cx`和`cy`表示圆心的位置,`r`表示渐变的半径。`stops`属性定义了渐变的颜色范围,包含起始颜色和结束颜色。在示例代码中,结束颜色是通过`Highcharts.Color(color).brighten(-0.3).get('rgb')`方法计算得到的。 但是在使用Vue的热更新时,可能会遇到报错`TypeError: b.indexOf is not a function`。这是因为在第二次加载时,颜色值从字符串变为了一个对象,导致设置颜色值时出现错误。为了解决这个问题,可以对代码进行优化,以确保每次都可以正确获取颜色值。可以使用`instanceof`操作符来判断颜色是否为一个对象。如果是对象,则从对象中获取起始颜色和结束颜色的值,否则直接使用该颜色和通过`Highcharts.Color(color).brighten(0.15).get('rgb')`方法计算结束颜色的值。 通过以上方法,您可以为Highcharts3D饼图添加渐变色。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [关于vue使用highcharts饼状图渐变色报错问题](https://blog.csdn.net/developer_qi/article/details/124126398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值