"; 2. Pass "mounted(){this.fetchData ()window.addEventListener('resize',()=>{if (this.chart){...}" Just implement the three-dimensional histogram."/> "; 2. Pass "mounted(){this.fetchData ()window.addEventListener('resize',()=>{if (this.chart){...}" Just implement the three-dimensional histogram.">

Home  >  Article  >  Web Front-end  >  How to use vue to implement column chart

How to use vue to implement column chart

藏色散人
藏色散人Original
2023-01-29 14:12:222590browse

How to use vue to implement a column chart: 1. Create a div attribute as "

"; 2. Pass "mounted(){this.fetchData()window.addEventListener('resize',()=>{if (this.chart){...}" Just implement a three-dimensional histogram.

How to use vue to implement column chart

The operating environment of this tutorial: Windows 10 system, vue3 version, DELL G3 computer

How to use vue to implement a column chart?

vue implements a three-dimensional histogram

The style is as shown in the figure below:
How to use vue to implement column chart

The three-dimensional histogram can be regarded as the bottom and top of yData The composition, the corresponding code is as follows:

<template>
  <div></div></template>
<script>import echarts from &#39;echarts&#39;export default {
  data() {
    return {
      chart: null,
      data: [], //数据
      xData: [], //x轴
      yBarData: [], //y轴
      yLable: [],
      colorStops: [],
      chartLegend: [], //图例
      colorOptions: [ //图例以及柱形图颜色选择
        &#39;#5ba2e4&#39;,
        &#39;#f58510&#39;,
        &#39;#afa5a5&#39;,
        &#39;#facb3d&#39;,
        &#39;#0854cf&#39;,
        &#39;#48c611&#39;,
        &#39;#082b63&#39;
      ]
    }
  },
  mounted() {
    this.fetchData()
    //图的大小自适应
    window.addEventListener(&#39;resize&#39;,()=>{
      if (this.chart){
        this.chart.resize()
      }
    })
  },
  beforeDestroy() { //实例销毁之前调用
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    fetchData() {
      this.xData = ["黑龙江",&#39;辽宁&#39;,&#39;贵州&#39;,&#39;福建&#39;,&#39;湖北&#39;,&#39;河南&#39;,&#39;河北&#39;,&#39;山西&#39;,&#39;山东&#39;,&#39;天津&#39;,&#39;吉林&#39;,&#39;北京&#39;,&#39;内蒙古&#39;,&#39;云南&#39;]
      this.yLable = [&#39;10&#39;,&#39;20&#39;,&#39;30&#39;,&#39;40&#39;,&#39;50&#39;,&#39;60&#39;,&#39;70&#39;,&#39;80&#39;,&#39;90&#39;,&#39;100&#39;,&#39;110&#39;,&#39;120&#39;,&#39;130&#39;,&#39;140&#39;]
      this.chartLegend = []
      const dateArr = []
      this.yLable.forEach((item, index) => {
        if (item !== null && item !== undefined) {
          dateArr.push(this.yLable[index])
        }
      })
      this.chartLegend = dateArr      this.initData()
      this.initChart()
    },
    initData() {
      this.yBarData = this.yLable    },
    initChart() {
      this.chart = echarts.init(this.$refs.BarChart)
      this.chart.clear() // 清空当前实例
      let colors = []
      const dom = 800
      const barWidth = dom / 20
      for (let i = 0; i < 4; i++) {
        colors.push({
          colorStops: [
            {
              offset: 0,
              color: &#39;#73fcff&#39; // 最左边
            }, {
              offset: 0.5,
              color: &#39;#86eef1&#39; // 左边的右边 颜色
            }, {
              offset: 0.5,
              color: &#39;#5ad6d9&#39; // 右边的左边 颜色
            }, {
              offset: 1,
              color: &#39;#3dc8ca&#39;
            }]
        })
      }
      this.chart.setOption({
        backgroundColor: &#39;#010d3a&#39;,
        //提示框
        tooltip: {
          trigger: &#39;axis&#39;,
          formatter: "{b} : {c}",
          axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: &#39;shadow&#39; // 默认为直线,可选为:&#39;line&#39; | &#39;shadow&#39;
          }
        },
        /**区域位置*/
        grid: {
          left: &#39;10%&#39;,
          right: &#39;10%&#39;,
          top: &#39;10%&#39;,
          bottom: &#39;10%&#39;,
        },
        //X轴
        xAxis: [{
          data: this.xData,
          type: &#39;category&#39;,
          show: true,
          axisLine: {
            show: false,
            lineStyle: {
              color: &#39;rgba(255,255,255,1)&#39;,
              shadowColor: &#39;rgba(255,255,255,1)&#39;,
              // shadowOffsetX: &#39;20&#39;
            },
            symbol: [&#39;none&#39;, &#39;arrow&#39;],
            symbolOffset: [0, 25]
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            margin: 20,
            fontSize: 10
          }
        }],
        yAxis: {
          show: true,
          splitNumber: 4,
          axisLine: {
            show: false
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: &#39;dashed&#39;,
              color: &#39;#075858&#39;
            },
          },
          axisLabel: {
            show: true,
            color: &#39;#FFFFFF&#39;,
            margin: 30,
            fontSize: 15
          }
        },
        series: [
          {//数据颜色
            name: &#39;日付费用户数&#39;,
            type: &#39;bar&#39;,
            barWidth: barWidth,
            itemStyle: {
              normal: {
                color: (params) => {
                  return colors[params.dataIndex % 4];
                }
              }
            },
            label: {
              show: true,
              position: [barWidth / 2, -(barWidth + 20)],
              color: &#39;#ffffff&#39;,
              fontSize: 14,
              fontStyle: &#39;bold&#39;,
              align: &#39;center&#39;
            },
            data: this.yBarData          },
          {//底
            z: 2,
            type: &#39;pictorialBar&#39;,
            data: this.yBarData,
            symbol: &#39;diamond&#39;,
            symbolOffset: [0, &#39;50%&#39;],
            symbolSize: [barWidth, barWidth * 0.5],
            itemStyle: {
              normal: {
                color: (params) => {
                  return colors[params.dataIndex % 4]
                }
              }
            }
          },
          {//顶
            z: 3,
            type: &#39;pictorialBar&#39;,
            symbolPosition: &#39;end&#39;,
            data: this.yBarData,
            symbol: &#39;diamond&#39;,
            symbolOffset: [0, &#39;-50%&#39;],
            symbolSize: [barWidth, barWidth * 0.5],
            itemStyle: {
              normal: {
                borderWidth: 0,
                 color: (params) => {
                  return colors[params.dataIndex % 4].colorStops[0].color;
                }
              }
            }
          }
        ]
      }, true)
    }
  }}</script>

In the above js code, there are the following precautions:

  • Color selection: You can colors[params.dataIndex % 4]Replace with this.colorOptions[params.dataIndex % 4], that is, use the colorOptions defined in the code for color filling
  • colorStops ensures a three-dimensional effect
  • In the code colors[params.dataIndex % 4]The selection of 4 in is variable, ensuring that the index value is within the length range of the colors variable. For example: in this example, the length of colors is 4, params. dataIndex % 4can be no more than 4

To quote the above barchart, the following code can be applied:

<template>
  <bar-chart></bar-chart></template>
<script>import barChart from "./components/barChart"export default {
  name: &#39;barchart&#39;,
  components: {  barChart }}</script>

Recommended learning: "vue video tutorial

The above is the detailed content of How to use vue to implement column chart. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn