第一篇:VUE 使用 HighCharts 画 3D环 饼图_vue 3d饼图

<template>
  <div class="container" id="container">
    <div :id="id" :option="option" class="echart-container"></div>
  </div>
</template>
<script>
import HighCharts from 'highcharts'
export default {
  props: {
    // id用于区分多处复用highcharts文件
    id: {
      type: String
    },
    //option 是图表的配置数据
    option: {
      type: Object
    }
  },
  data() {
    return {
      charts: null
    }
  },
  mounted() {
    // 页面加载完成之后再渲染图表
    this.setOption()
  },
  methods: {
    setOption() {
      if (this.charts) {
        this.charts.destroy()
      }
      this.charts = HighCharts.chart(this.id, this.option)
      this.charts.reflow()
    }
  }
}
</script>

<style lang="scss" scoped>
/* 容器 */
::v-deep.container {
  width: 100%;
  height: 100%;
  background: #043b8c;
  .echart-container {
    width: 100%;
    height: 100%;
  }
  // 去除水印
  .highcharts-credits {
    display: none;
  }
}
</style>

4.书写 3D饼/环 需要的相关数据和相关配置。

<template>
  <div class="charts">
    <pie :id="id" :option="option"></pie>
  </div>
</template>

<script>
import pie from './pip.vue'
import HighCharts from 'highcharts'
export default {
  components: {
    pie
  },
  data() {
    return {
      id: 'echart-container',
      option: {
        title: {
          text: null //图表的标题文字
        },
        subtitle: {
          text: null //副标题文字
        },
        tooltip: {
          backgroundColor: '#b2bec3',
          borderColor: '#b2bec3',
          style: {
            color: '#FFFFFF'
          }
        },
        legend: {
          labelFormatter: function() {
            /*
             *  格式化函数可用的变量:this, 可以用 console.log(this) 来查看包含的详细信息
             *  this 代表当前数据列对象,所以默认的实现是 return this.name
             */
            return this.name
          },
          align: 'center', //程度标的目标地位
          verticalAlign: 'bottom', //垂直标的目标地位
          x: 0, //间隔x轴的间隔
          y: 0, //间隔Y轴的间隔
          symbolRadius: 0,
          itemStyle: {
            cursor: 'pointer',
            color: '#FFFFFF'
          },
          itemHoverStyle: {
            color: '#FFFFFF'
          }
        },
        // colors: ['#99FCFF', '#028EEF', '#F04864', '#854BF7'],
        chart: {
          type: 'pie',
          renderTo: 'container',
          plotBackgroundColor: null,
          plotBorderWidth: null,
          plotShadow: false,
          backgroundColor: null,
          animation: false,
          events: {
            load: function() {
              var each = HighCharts.each,
                points = this.series[0].points
              each(points, function(p) {
                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,
            beta: 0, //图表视图旋转角度
            viewDistance: 40 //定义图表的浏览长度
          }
        },
        plotOptions: {
          pie: {
            allowPointSelect: false,
            cursor: 'pointer',
            depth: 35,
            innerSize: '80%',
            textShadow: false,
            shadow: false,
            dataLabels: {
              enabled: true,
              formatter: function() {
                return (
                  this.point.name +
                  '<br><p style="color:' +
                  this.color +
                  '">(' +
                  this.y +
                  ',&nbsp' +
                  this.percentage.toFixed(1) +
                  '%)</p>'
                )
              },
              style: {
                color: '#FFFFFF',
                fontSize: '12px',
                textOutline: 'none'
              }
            },
            states: {
              inactive: {
                opacity: 0.7,
                size: '120%'
              },
              hover: {
                halo: {
                  size: '120%',
                  attributes: {
                    fill: HighCharts.getOptions().colors[2],
                    'stroke-width': 2,
                    stroke: HighCharts.getOptions().colors[1]
                  }
                }
              }
            }
          },
          series: {
            point: {
              events: {
                mouseOver: function() {},
                mouseOut: function() {},
                hover: {
                  backgroundColor: '#000000'
                }
              }
            }
          },
          column: {
            events: {}
          }
        },
        series: [
          {
            type: 'pie',
            name: 'Browser share',
            hoverAnimation: true,
            size: '90%',
            startAngle: 0,
            showInLegend: true, // 默认值
            colorByPoint: true,
            data: [
              { name: '个人网银', y: 50, h: 20, sliced: true, selected: true }, //模块名和所占比,也可以{name: '测试1',y: 12}
              { name: '个人手机银行', y: 40, h: 15 }, //模块名和所占比,也可以{name: '测试1',y: 12}
              { name: '企业网银', y: 50, h: 5 }, //模块名和所占比,也可以{name: '测试1',y: 12}
              { name: '企业手机银行', y: 50, h: 10 } //模块名和所占比,也可以{name: '测试1',y: 12}
            ]
          }
        ]
      }
    }
  },
  created() {
    // 设置颜色渐变
    this.setcolor()
    // 设置饼图高度
    this.setOptonHeight()
    // 设置点击事件
    this.setClick()
  },
  mounted() {},
  methods: {
    setOptonHeight() {
      var each = HighCharts.each,
        round = Math.round,
        cos = Math.cos,
        sin = Math.sin,
        deg2rad = Math.deg2rad
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤在Vue使用Highcharts来绘制3D饼图: 1. 在你的项目的main.js文件中引入Highchartshighcharts-3d插件: ```javascript import Highcharts from 'highcharts' import Highcharts3d from 'highcharts/highcharts-3d' Highcharts3d(Highcharts) ``` 2. 在你的组件中创建一个容器来显示图表,并引入Highcharts库: ```vue <template> <div class="container"> <div :id="id" :option="option" class="chart-container"></div> </div> </template> <script> import Highcharts from 'highcharts' export default { props: { id: { type: String }, // 用于区分多个图表的唯一标识符 option: { type: Object } // 图表的配置选项 }, data() { return { chart: null } }, mounted() { // 在组件加载完成后初始化图表 this.setOption() }, methods: { setOption() { // 销毁之前的图表实例 if (this.chart) { this.chart.destroy() } // 创建一个新的图表实例并渲染到指定容器中 this.chart = Highcharts.chart(this.id, this.option) // 重新调整图表大小,以适应容器 this.chart.reflow() } } } </script> <style scoped> .container { width: 100%; height: 100%; background: #043b8c; .chart-container { width: 100%; height: 100%; } /* 去除水印 */ .highcharts-credits { display: none; } } </style> ``` 3. 在你的父组件中,使用刚才创建的组件,并传入相应的id和配置选项: ```vue <template> <div> <pie-chart id="chart1" :option="chartOptions"></pie-chart> </div> </template> <script> import PieChart from './PieChart.vue' export default { components: { PieChart }, data() { return { chartOptions: { chart: { type: 'pie', options3d: { enabled: true, alpha: 45, beta: 0 } }, title: { text: '3D饼图' }, series: [{ name: 'Brands', data: [ ['Chrome', 61.41], ['Internet Explorer', 11.84], ['Firefox', 10.85], ['Edge', 4.67], ['Safari', 4.18], ['Other', 7.05] ] }] } } } } </script> ``` 这样,你就可以在Vue使用Highcharts来绘制3D饼图了。记得根据你的需求修改配置选项和数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值