" として作成します; 2. "mounted(){this" を渡します.fetchData ()window.addEventListener('resize',()=>{if (this.chart){...}" 3 次元ヒストグラムを実装するだけです。"/> " として作成します; 2. "mounted(){this" を渡します.fetchData ()window.addEventListener('resize',()=>{if (this.chart){...}" 3 次元ヒストグラムを実装するだけです。">

ホームページ  >  記事  >  ウェブフロントエンド  >  vue を使用して縦棒グラフを実装する方法

vue を使用して縦棒グラフを実装する方法

藏色散人
藏色散人オリジナル
2023-01-29 14:12:222541ブラウズ

vue を使用して縦棒グラフを実装する方法: 1. div 属性を "

"; として作成します。 2 "mounted(){this.fetchData()window.addEventListener('resize',()=>{if (this.chart){...}" を渡して 3 次元ヒストグラムを実装するだけです。

vue を使用して縦棒グラフを実装する方法

このチュートリアルの動作環境: Windows 10 システム、vue3 バージョン、DELL G3 コンピューター

vue を使用して縦棒グラフを実装するにはどうすればよいですか?

vue は 3 次元ヒストグラムを実装します

スタイルは次の図に示すとおりです:
vue を使用して縦棒グラフを実装する方法

三次元ヒストグラムは yData の底部と上部とみなすことができます。その構成、対応するコードは次のとおりです。

<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>

上記の JS コードでは、次の注意事項があります。

  • 色の選択: colors[params.dataIndex % 4]#this.colorOptions[params.dataIndex % 4] に置き換えることができます。つまり、colorOptions を使用します。カラー塗りつぶしのコードで定義されています
  • colorStops により 3 次元効果が保証されます
  • コード内
  • colors[params.dataIndex % 4] の 4 の選択は可変です, インデックス値が色変数の長さの範囲内にあることを確認します。例: この例では、色の長さは 4, params. dataIndex % 4can be not more than 4
上記の棒グラフを引用するには、次のコードを適用できます:

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

vue ビデオ チュートリアル

以上がvue を使用して縦棒グラフを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。