HighEcharts在Vue中的组件封装

1、安装

cnpm install highcharts --save

2、组件封装

<!--
 * @message: highcharts组件
-->
<template>
  <div ref="chart" id="container" :style="{width:width,height:height}"></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';
import cylinder from 'highcharts/modules/cylinder'

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


export default {
  name: 'highcharts',
  props: {
    width: {
      type: String,
      required: false,
      default: '0px'
    },
    height: {
      type: String,
      required: false,
      default: '0px'
    },
    option: {
      type: Object,
      required: false,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    // 初始化
    init() {
      // 全局配置,针对页面上所有图表有效,设置背景颜色透明,禁用版权信息
      Highcharts.setOptions({
        chart: {
          backgroundColor: 'rgba(0,0,0,0)',
        },
        credits: {
          enabled: false
        }
      });
      // 图表初始化
      if (this.chart) {
        this.chart.destroy()
      }
      // 3D效果初始化
      this.init3D()
      this.chart = new Highcharts.Chart(this.$refs.chart, this.option)
    },
    // 重绘
    reDraw() {
      this.chart = new Highcharts.Chart(this.$refs.chart, this.option)
    },
    // 初始3D效果
    init3D() {
      var each = Highcharts.each,
          round = Math.round,
          cos = Math.cos,
          sin = Math.sin,
          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;
        }

        //不同高度柱状图
        var series = this,
            chart = series.chart,
            options = chart.options,
            seriesOptions = series.options,
            depth = seriesOptions.depth || 0,
            options3d = options.chart.options3d,
            alpha = options3d.alpha,
            beta = options3d.beta,
            z = seriesOptions.stacking ? (seriesOptions.stack || 0) * depth : series._i * depth;
        z += depth / 2;
        if (seriesOptions.grouping !== false) {
          z = 0;
        }
        each(series.data, function (point) {
          var shapeArgs = point.shapeArgs,
              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
          var ret = proceed.apply(this, [].slice.call(arguments, 1));
          ret.zTop = (ret.zOut + 0.5) / 100;
          return ret;
        });
      }(Highcharts));
    }
  },
  // 监听Option的改变,当改变时重新渲染图表
  watch: {
    option: function () {
      this.reDraw()
    }
  }
}
</script>

<style scoped>
</style>

3、基本使用

<template>
  <div id="situation-map">
      <HighCharts :width="'100%'" :height="'100%'" :option="educationOption"/>
  </div>
</template>
<script>
export default {
  data() {
    return {
      educationOption: {}
    }
  },
  components: {
    HighCharts: () => import('@/components/HighCharts/highcharts')
  },
 
  mounted() {
    this.educationOption = option(this.dataLists[this.activeIndex]).educationOption
    // 此处传入具体Option
  }

}
</script>

<style scoped="scoped" lang="scss">
</style>

4、相关文档

  1. 中文API https://api.highcharts.com.cn/legacy#colors
  2. 官网 兼容 IE6+、完美支持移动端、图表类型丰富的 HTML5 交互图表 | Highcharts
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值