vue+highcharts实现3D饼图效果

 更新时间:2022年03月28日 08:40:41   作者:A孙大壮  
这篇文章主要为大家详细介绍了vue+highcharts实现3D饼图效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue+highcharts实现3D饼图效果的具体代码,供大家参考,具体内容如下

这是最终效果

<template>
<div class="big-box">
  <div class="com-container" ref="container" style="width:380px;height:300px;">
  </div>
  <div class="tulibox">
    <div v-for="(item,index) in peiData" :key="index" class="tuliboxitem">
      <span class="name">{{item.name}}</span>   <span class="value">{{item.y}}%</span>
    </div>
  </div>
</div>
</template>

<script>
import HighCharts from 'highcharts'

export default {
  props: {},
  data () {
    return {
      peiData: [
        { name: '输电', y: 28, h: 60 },
        { name: '变电', y: 20, h: 20 },
        { name: '配电', y: 10, h: 32 },
        { name: '新业务', y: 6, h: 45 }
      ]
    }
  },
  computed: {},
  updated () {},
  created () {},
  mounted () {
    this.initChart()
    const that = this
    window.onresize = function () { that.initChart() }
  },
  methods: {
    initChart () {
      // 修改3d饼图绘制过程
      const each = HighCharts.each
      const round = Math.round
      const cos = Math.cos
      const sin = Math.sin
      const deg2rad = Math.deg2rad
      HighCharts.wrap(HighCharts.seriesTypes.pie.prototype, 'translate', function (proceed) {
        proceed.apply(this, [].slice.call(arguments, 1))
        // Do not do this if the chart is not 3D
        if (!this.chart.is3d()) {
          return
        }
        const series = this
        const chart = series.chart
        const options = chart.options
        const seriesOptions = series.options
        const depth = seriesOptions.depth || 0
        const options3d = options.chart.options3d
        const alpha = options3d.alpha
        const beta = options3d.beta
        var z = seriesOptions.stacking ? (seriesOptions.stack || 0) * depth : series._i * depth
        z += depth / 2
        if (seriesOptions.grouping !== false) {
          z = 0
        }
        each(series.data, function (point) {
          const shapeArgs = point.shapeArgs
          var angle
          point.shapeType = 'arc3d'
          var ran = point.options.h
          shapeArgs.z = z
          shapeArgs.depth = depth * 0.75 + ran
          shapeArgs.alpha = alpha
          shapeArgs.beta = beta
          shapeArgs.center = series.center
          shapeArgs.ran = ran
          angle = (shapeArgs.end + shapeArgs.start) / 2
          point.slicedTranslation = {
            translateX: round(cos(angle) * seriesOptions.slicedOffset * cos(alpha * deg2rad)),
            translateY: round(sin(angle) * seriesOptions.slicedOffset * cos(alpha * deg2rad))
          }
        })
      });
      (function (H) {
        H.wrap(HighCharts.SVGRenderer.prototype, 'arc3dPath', function (proceed) {
        // Run original proceed method
          const ret = proceed.apply(this, [].slice.call(arguments, 1))
          ret.zTop = (ret.zOut + 0.5) / 100
          return ret
        })
      }(HighCharts))
      // 生成不同高度的3d饼图
      const highEcharts = this.$refs.container
      HighCharts.chart(highEcharts, {
        chart: {
          type: 'pie',
          animation: false,
          backgroundColor: 'rgba(0,0,0,0)',

          events: {
            load: function () {
              const each = HighCharts.each
              const points = this.series[0].points
              each(points, function (p, i) {
                p.graphic.attr({
                  translateY: -p.shapeArgs.ran
                })
                p.graphic.side1.attr({
                  translateY: -p.shapeArgs.ran
                })
                p.graphic.side2.attr({
                  translateY: -p.shapeArgs.ran
                })
              })
            }
          },
          options3d: {
            enabled: true,
            alpha: 65
          }
        },
        title: {
          show: 'false',
          text: null
        },
        subtitle: {
          text: null
        },
        credits: {
          enabled: false
        },
        legend: { // 【图例】位置样式
          backgroundColor: 'rgba(0,0,0,0)',
          shadow: false,
          layout: 'vertical',
          align: 'right', // 水平方向位置
          verticalAlign: 'top', // 垂直方向位置
          x: 0, // 距离x轴的距离
          y: 100, // 距离Y轴的距离
          symbolPadding: 10,
          symbolHeight: 14,
          itemStyle: {
            lineHeight: '24px',
            fontSize: '16px',
            color: '#fff'
          },
          labelFormatter: function () {
            /*
            *  格式化函数可用的变量:this, 可以用 console.log(this) 来查看包含的详细信息
            *  this 代表当前数据列对象,所以默认的实现是 return this.name
            */
            return this.name + this.h + '%'
          }
        },
        plotOptions: {
          pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            depth: 35,
            innerSize: 180,
            dataLabels: {
              enabled: false
            },
            // 显示图例
            showInLegend: false
          }
        },
        series: [{
          type: 'pie',
          name: '占比',
          // h 是高度  y是占的圆环长度
          colorByPoint: true,
          colors: [
            { // 注意!!!如果是柱状图请使用color,如果是面积图请使用fillColor
              linearGradient: {
                x1: 0,
                y1: 1,
                x2: 1,
                y2: 0
              },
              stops: [
                [0, '#19596d'],
                [1, '#2ea1b2']
              ]
            }, { // 注意!!!如果是柱状图请使用color,如果是面积图请使用fillColor
              linearGradient: {
                x1: 0,
                y1: 1,
                x2: 1,
                y2: 0
              },
              stops: [
                [0, '#ee7529'],
                [1, '#f5a86c']
              ]
            }, { // 注意!!!如果是柱状图请使用color,如果是面积图请使用fillColor
              linearGradient: {
                x1: 0,
                y1: 1,
                x2: 1,
                y2: 0
              },
              stops: [
                [0, '#f5c055'],
                [1, '#967b3d']
              ]
            }, { // 注意!!!如果是柱状图请使用color,如果是面积图请使用fillColor
              linearGradient: {
                x1: 0,
                y1: 1,
                x2: 1,
                y2: 0
              },
              stops: [
                [0, '#2d7bb5'],
                [1, '#1a5784']
              ]
            }],
          data: this.peiData
        }]
      })
    }
  },
  components: {}
}
</script>

<style scoped lang="less">
.com-container{
    width: 380px;
    height: 300px;
    padding-right: 20px;
}
.big-box{
    display: flex;
    background-image: url('../img/dizuo.png');
    background-repeat: no-repeat;
    background-position: 25px 144px;
    padding-top: 20px;
}
.tulibox{
  padding-top: 65px;
  .tuliboxitem{
    position: relative;
    margin: 10px 0;
    .name{
      font-size: 18px;
      color: #FEFEFF;
      padding-right: 20px;
    }
    .value{
      font-size: 22px;
      color: #0CD2EA;
    }
  }
  .tuliboxitem::before{
     content: "";
     position: absolute;
     width: 16px;
    height: 16px;
    top: 7px;
    border-radius: 50%;
    left: -33px;
  }
  .tuliboxitem:nth-child(1)::before{
      background-color: #fff600;
  }
  .tuliboxitem:nth-child(2)::before{
      background-color: #209FED;
  }
  .tuliboxitem:nth-child(3)::before{
      background-color: #808EC7;
  }
  .tuliboxitem:nth-child(4)::before{
      background-color: #EE6B26;
  }
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • vue3+element-plus Dialog对话框的使用与setup 写法的用法

    vue3+element-plus Dialog对话框的使用与setup 写法的用法

    这篇文章主要介绍了vue3+element-plus Dialog对话框的使用 与 setup 写法的使用,本文通过两种方式结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Vue或者React项目配置@路径别名及智能提示方案

    Vue或者React项目配置@路径别名及智能提示方案

    这篇文章主要介绍了Vue或者React项目配置@路径别名及智能提示方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue 项目软键盘回车触发搜索事件

    vue 项目软键盘回车触发搜索事件

    这篇文章主要介绍了vue 项目软键盘回车触发搜索事件,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 在Vue中实现网页截图与截屏功能详解

    在Vue中实现网页截图与截屏功能详解

    在Web开发中,有时候需要对网页进行截图或截屏,Vue作为一个流行的JavaScript框架,提供了一些工具和库,可以方便地实现网页截图和截屏功能,本文将介绍如何在Vue中进行网页截图和截屏,需要的朋友可以参考下
    2023-06-06
  • 浅谈vue-router路由切换 组件重用挖下的坑

    浅谈vue-router路由切换 组件重用挖下的坑

    今天小编就为大家分享一篇浅谈vue-router路由切换 组件重用挖下的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • 详解用webpack2.0构建vue2.0超详细精简版

    详解用webpack2.0构建vue2.0超详细精简版

    本篇文章主要介绍了详解用webpack2.0构建vue2.0超详细精简版,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • vue实现物流时间轴效果

    vue实现物流时间轴效果

    这篇文章主要为大家详细介绍了vue实现物流时间轴效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • vue 实现滑动块解锁示例详解

    vue 实现滑动块解锁示例详解

    这篇文章主要为大家介绍了vue 实现滑动块解锁示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • vue中解决微信html5原生ios虚拟键返回不刷新问题

    vue中解决微信html5原生ios虚拟键返回不刷新问题

    这篇文章主要介绍了vue中解决微信html5原生ios虚拟键返回不刷新问题,本文给大家分享解决方法,通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • 在Vue3中使用vue-qrcode库实现二维码生成的方法

    在Vue3中使用vue-qrcode库实现二维码生成的方法

    在Vue3中实现二维码生成需要使用第三方库来处理生成二维码的逻辑,常用的库有 qrcode和 vue-qrcode,这篇文章主要介绍了在Vue3中使用vue-qrcode库实现二维码生成,需要的朋友可以参考下
    2023-12-12

最新评论