vue highcharts 3d环形图

2 篇文章 0 订阅

在这里插入图片描述

  1. 安装
npm install -S vue-highcharts
npm install highcharts --save
  1. main.js引入
import Highcharts from 'highcharts'
 
import Highcharts3d from 'highcharts/highcharts-3d'
 
Highcharts3d(Highcharts)
  1. 静态测试代码
<template>
  <div class="charts" style="width:100%;height:100%">
    <div :id="id" :option="option" class="echart-container"></div>
  </div>
</template>
 
<script>
import HighCharts from 'highcharts'
export default {
  data() {
    return {
      charts: null,
      id: 'echart-container',
      option:{
        chart: {
          type: "pie",
          backgroundColor: "rgba(0, 0, 0, 0)",//去掉白色背景
          marginTop:10,//上边距
          options3d: {
              enabled: true,//3D启用
              alpha: 50,//内旋转角度
              beta: 0,//外选择角度
          },
        },
        title: {
            text: "",
        },
        tooltip: {
            backgroundColor: '#939393',   // 背景颜色
            borderColor: 'black',         // 边框颜色
            borderRadius: 10,             // 边框圆角
            borderWidth: 0,               // 边框宽度
            shadow: false,                 // 是否显示阴影
            style: {                      // 文字内容相关样式
                color: "#FFFFFF",
                fontSize: "12px",
                fontWeight: "blod",
                fontFamily: "Courir new"
            }
        },
        colors: ['#637FEA', '#5AACD5', '#954CD9', '#EDCB30', '#25BD97'], //颜色
        plotOptions: {
            pie: {
              innerSize: '50%',//设置环,内环占比
              depth:50 ,//显示3d效果
              dataLabels: { //数据标签
                  enabled: true,
                  distance: "10%",
                  style: {
                    textOutline: "none", //去掉文字白边
                    color: '#b8c3d2',
                  },
                  // format: '{point.name}'
                  // formatter: function () {
                  //   return '<span >' + this.point.name + '</span><br/><span>占比:' + this.percentage.toFixed(2) + '%</span><br/>' + (this.y) + '人</span>';
                  // },
                  formatter: function () {
                    if(this.y>0){
                      return '<span >' + this.point.name + '</span><br/><span>占比:' + this.percentage.toFixed(2) + '%</span><br/>' + (this.y) + '人</span>';
                    }
                    
                  },
              },
              center: ['50%', '50%']
            }
        },
        yAxis: {        
          min:0.0001,
          type: 'logarithmic'
      },
        series: [ //数据列
            {
                type: "pie",
                data: [
                    ["邮政包裹", 30],
                    ["圆通快递", 20],
                    ["京东快递", 30],
                ],
            },
        ],
      }

    }
  },
  created() {
    this.setOption()
  },
  methods: {
    setOption() {
      if (this.charts) {
        this.charts.destroy()
      }
      this.charts = HighCharts.chart(this.id, this.option)
      this.charts.reflow()
    }
  }
}
</script>
<style lang="less" scoped>
.echart-container {
    width: 100%;
    height: 100%;
    margin:0 auto;
  }
  // 去除水印
  .highcharts-credits {
    display: none;
  }
</style>
  1. 完整代码,因为动态渲染,接口在父组件调用的,所以 测试直接看上面3
<template>
  <div class="charts" style="width:100%;height:100%">
    <div :id="id" :option="option" class="echart-container"></div>
  </div>
</template>
 
<script>
import HighCharts from 'highcharts'
export default {
  props:{ // 这是从父级传入数据,可以直接在次页面写入数据
    allData:{
      type:Object
    }
  },
  watch:{
    allData(newVal) { //监听父组件的数据变化,改变直接重新渲染
      const flg = Object.keys(newVal).length
      if(flg) {
        this.setDom(newVal) 
      }
    },
  },
  data() {
    return {
      charts: null,
      id: 'echart-container',
      option:{
        chart: {
          type: "pie",
          backgroundColor: "rgba(0, 0, 0, 0)",//去掉白色背景
          marginTop:10,//上边距
          options3d: {
              enabled: true,//3D启用
              alpha: 50,//内旋转角度
              beta: 0,//外选择角度
          },
        },
        title: {
            text: "",
        },
        tooltip: {
            backgroundColor: '#939393',   // 背景颜色
            borderColor: 'black',         // 边框颜色
            borderRadius: 10,             // 边框圆角
            borderWidth: 0,               // 边框宽度
            shadow: false,                 // 是否显示阴影
            style: {                      // 文字内容相关样式
                color: "#FFFFFF",
                fontSize: "12px",
                fontWeight: "blod",
                fontFamily: "Courir new"
            }
        },
        colors: ['#637FEA', '#5AACD5', '#954CD9', '#EDCB30', '#25BD97'], //颜色
        plotOptions: {
            pie: {
              innerSize: '50%',//设置环,内环占比
              depth:50 ,//显示3d效果
              dataLabels: { //数据标签
                  enabled: true,
                  distance: "10%",
                  style: {
                    textOutline: "none", //去掉文字白边
                    color: '#b8c3d2',
                  },
                  // format: '{point.name}'
                  // formatter: function () {
                  //   return '<span >' + this.point.name + '</span><br/><span>占比:' + this.percentage.toFixed(2) + '%</span><br/>' + (this.y) + '人</span>';
                  // },
                  formatter: function () {
                    if(this.y>0){
                      return '<span >' + this.point.name + '</span><br/><span>占比:' + this.percentage.toFixed(2) + '%</span><br/>' + (this.y) + '人</span>';
                    }
                    
                  },
              },
              center: ['50%', '50%']
            }
        },
        yAxis: {        
          min:0.0001,
          type: 'logarithmic'
      },
        series: [ //数据列
            {
                type: "pie",
                data: [
                    ["邮政包裹", 30],
                    ["圆通快递", 20],
                    ["京东快递", 30],
                ],
            },
        ],
      }

    }
  },
  created() {
    const flag = Object.keys(this.allData).length
    if(flag) {
      this.setDom(this.allData)
    }
  },
  methods: {
    setDom(res) {
      const obj1 = res.allowanceMap
      const arr1 = Object.keys(obj1)
      const arrNew = []
      arr1.forEach(item => {
        if(!obj1[item]) {
          obj1[item] = 6
        }
        const arrLin = [item,obj1[item]]
        arrNew.push(arrLin)
      })
      this.option.series[0].data = arrNew
      console.log(arrNew,'重组数据')
      this.setOption()
    },
    setOption() {
      if (this.charts) {
        this.charts.destroy()
      }
      this.charts = HighCharts.chart(this.id, this.option)
      this.charts.reflow()
    }
  }
}
</script>
<style lang="less" scoped>
.echart-container {
    width: 100%;
    height: 100%;
    margin:0 auto;
  }
  // 去除水印,不知道为什么没生效
  .highcharts-credits {
    display: none;
  }
</style>

解决水印没有去除问题

将去除水印代码放到全局当中

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值