最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)

 更新時(shí)間:2022年05月24日 10:46:16   作者:C.DLording  
最近在項(xiàng)目中需要對(duì)數(shù)據(jù)進(jìn)行可視化處理,而眾所周知echarts是非常強(qiáng)大的插件,下面這篇文章主要給大家介紹了關(guān)于Vue使用echarts的完整步驟及解決各種報(bào)錯(cuò)的相關(guān)資料,需要的朋友可以參考下

前言:

Echarts,它是一個(gè)與框架無關(guān)的 JS 圖表庫,但是它基于Js,這樣很多框架都能使用它,例如Vue,估計(jì)IONIC也能用,下次研究。

因?yàn)槲业牧?xí)慣,每次新嘗試做一個(gè)功能的時(shí)候,總要新創(chuàng)建個(gè)小項(xiàng)目,做做Demo。

首先看自己電腦是否安裝了Vue,打開終端命令:vue --version,我以前安裝過Vue,但是不知道為何報(bào)錯(cuò):

vue/cli Error: Cannot find module ‘@vue/cli-shared-utils‘

注意:如果是全局module出錯(cuò),就全局更新,如果是項(xiàng)目中module出錯(cuò),就刪除(rimraf node_modules)重新安裝 (npm i)

解決方法(更新或者重裝):

npm update -g @vue/cli
# 或者
yarn global upgrade --latest @vue/cli

在自己特定的項(xiàng)目文件夾下cmd打開終端

1.vue create echarts

第一個(gè)是我之前的默認(rèn)習(xí)慣創(chuàng)建

2.默認(rèn)習(xí)慣創(chuàng)建,選擇package.json之后輸入 n 或者 y ,過程簡(jiǎn)略。

cd echarts 到該文件夾下,npm run serve顯示項(xiàng)目運(yùn)行正常:

正式開始嘗試Echarts

建議大家要學(xué)會(huì)看官網(wǎng)文檔:https://echarts.apache.org/handbook/zh/get-started/

能學(xué)習(xí)到兩點(diǎn):

  • 在繪圖前我們需要為 ECharts 準(zhǔn)備一個(gè)定義了高寬的 DOM 容器。
  • 通過 echarts.init 方法初始化一個(gè) echarts 實(shí)例并通過 setOption 方法生成一個(gè)簡(jiǎn)單的柱狀圖。

基于這兩句話進(jìn)行研究:我通常是在About中嘗試進(jìn)行寫Demo。

Ctrl + C結(jié)束項(xiàng)目。

在項(xiàng)目終端安裝echarts:npm install echarts --save

請(qǐng)注意,2022年后安裝的echarts都是5.X版本,可以在package.json中看到,不知為何,這個(gè)和Vue項(xiàng)目不匹配,導(dǎo)致發(fā)生錯(cuò)誤,知道原因的麻煩在下面留言。

全局引入:在 main.js 中全局引入 echarts

import echarts from "echarts";
Vue.prototype.$echarts = echarts;

在About.Vue中的<template> </template>寫<div id="main" style="width: 600px; height: 400px"></div>,如上圖,對(duì)應(yīng)1. 在繪圖前我們需要為 ECharts 準(zhǔn)備一個(gè)定義了高寬的 DOM 容器。

<div id="main" style="width: 600px; height: 400px"></div>

對(duì)應(yīng) 2.通過 echarts.init 方法初始化一個(gè) echarts 實(shí)例并通過 setOption 方法生成一個(gè)簡(jiǎn)單的柱狀圖。這個(gè)需要在script中進(jìn)行操作。

示例一:

    drawChart() {
      // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例  這個(gè)和上面的main對(duì)應(yīng)
      let myChart = this.$echarts.init(document.getElementById("main"));
      // 指定圖表的配置項(xiàng)和數(shù)據(jù)
      let option = {
        title: {
          text: "ECharts 入門示例",
        },
        tooltip: {},
        legend: {
          data: ["銷量"],
        },
        xAxis: {
          data: ["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"],
        },
        yAxis: {},
        series: [
          {
            name: "銷量",
            type: "bar",
            data: [5, 20, 36, 10, 10, 20],
          },
        ],
      };
      // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
      myChart.setOption(option);
    },

然后新建monted(){},否則會(huì)出現(xiàn)init沒有定義等等錯(cuò)誤。

mounted() {
    this.drawChart();
  },

到這一步,其實(shí)示例一已經(jīng)完成,但是我們運(yùn)行發(fā)現(xiàn)圖片無法顯示,只有一處警告錯(cuò)誤:"export ‘default’ (imported as ‘echarts’) was not found in ‘echarts’

起著懷疑的態(tài)度進(jìn)行百度,https://blog.csdn.net/Aom_yt/article/details/110947734 ,給出的原因是“可能還不能支持最新版的echarts5.0” 這句話不一定對(duì),麻煩知道原因的在下面進(jìn)行評(píng)論。

解決方法:

  • 卸載: npm uninstall echarts
  • 重裝echarts: npm install echarts@4.9.0
  • 重新運(yùn)行項(xiàng)目,發(fā)現(xiàn)成功了。

示例二:

我的需求和目標(biāo)是將https://echarts.apache.org/examples/zh/editor.html?c=bar-race-country&version=5.2.1導(dǎo)入到我的Vue項(xiàng)目中,基于上面1和2原理,上代碼:

<div id="main2" style="width: 1600px; height: 1400px"></div>
    drawChart2() {
      // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例  這個(gè)和上面的main2對(duì)應(yīng)
      let myChart = this.$echarts.init(document.getElementById("main2"));
      // 指定圖表的配置項(xiàng)和數(shù)據(jù)
      var ROOT_PATH =
        "https://cdn.jsdelivr.net/gh/apache/echarts-website@asf-site/examples";

      // var chartDom = document.getElementById("main");
      // var myChart = echarts.init(chartDom);
      var option;

      const updateFrequency = 2000;
      const dimension = 0;
      const countryColors = {
        Australia: "#00008b",
        Canada: "#f00",
        China: "#ffde00",
        Cuba: "#002a8f",
        Finland: "#003580",
        France: "#ed2939",
        Germany: "#000",
        Iceland: "#003897",
        India: "#f93",
        Japan: "#bc002d",
        "North Korea": "#024fa2",
        "South Korea": "#000",
        "New Zealand": "#00247d",
        Norway: "#ef2b2d",
        Poland: "#dc143c",
        Russia: "#d52b1e",
        Turkey: "#e30a17",
        "United Kingdom": "#00247d",
        "United States": "#b22234",
      };
      $.when(
        $.getJSON("https://cdn.jsdelivr.net/npm/emoji-flags@1.3.0/data.json"),
        $.getJSON(ROOT_PATH + "/data/asset/data/life-expectancy-table.json")
      ).done(function (res0, res1) {
        const flags = res0[0];
        const data = res1[0];
        const years = [];
        for (let i = 0; i < data.length; ++i) {
          if (years.length === 0 || years[years.length - 1] !== data[i][4]) {
            years.push(data[i][4]);
          }
        }
        function getFlag(countryName) {
          if (!countryName) {
            return "";
          }
          return (
            flags.find(function (item) {
              return item.name === countryName;
            }) || {}
          ).emoji;
        }
        let startIndex = 10;
        let startYear = years[startIndex];
        option = {
          grid: {
            top: 10,
            bottom: 30,
            left: 150,
            right: 80,
          },
          xAxis: {
            max: "dataMax",
            axisLabel: {
              formatter: function (n) {
                return Math.round(n) + "";
              },
            },
          },
          dataset: {
            source: data.slice(1).filter(function (d) {
              return d[4] === startYear;
            }),
          },
          yAxis: {
            type: "category",
            inverse: true,
            max: 10,
            axisLabel: {
              show: true,
              fontSize: 14,
              formatter: function (value) {
                return value + "{flag|" + getFlag(value) + "}";
              },
              rich: {
                flag: {
                  fontSize: 25,
                  padding: 5,
                },
              },
            },
            animationDuration: 300,
            animationDurationUpdate: 300,
          },
          series: [
            {
              realtimeSort: true,
              seriesLayoutBy: "column",
              type: "bar",
              itemStyle: {
                color: function (param) {
                  return countryColors[param.value[3]] || "#5470c6";
                },
              },
              encode: {
                x: dimension,
                y: 3,
              },
              label: {
                show: true,
                precision: 1,
                position: "right",
                valueAnimation: true,
                fontFamily: "monospace",
              },
            },
          ],
          // Disable init animation.
          animationDuration: 0,
          animationDurationUpdate: updateFrequency,
          animationEasing: "linear",
          animationEasingUpdate: "linear",
          graphic: {
            elements: [
              {
                type: "text",
                right: 160,
                bottom: 60,
                style: {
                  text: startYear,
                  font: "bolder 80px monospace",
                  fill: "rgba(100, 100, 100, 0.25)",
                },
                z: 100,
              },
            ],
          },
        };
        // console.log(option);
        myChart.setOption(option);
        for (let i = startIndex; i < years.length - 1; ++i) {
          (function (i) {
            setTimeout(function () {
              updateYear(years[i + 1]);
            }, (i - startIndex) * updateFrequency);
          })(i);
        }
        function updateYear(year) {
          let source = data.slice(1).filter(function (d) {
            return d[4] === year;
          });
          option.series[0].data = source;
          option.graphic.elements[0].style.text = year;
          myChart.setOption(option);
        }
      });
      // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。
      myChart.setOption(option);
    },
mounted() {
    this.drawChart2();
  },

不一一解釋了,只要你弄懂了示例一,這個(gè)也能運(yùn)行出來,同時(shí)也能舉一反三。

其他:

在學(xué)習(xí)Echarts時(shí)候,發(fā)現(xiàn)這個(gè)用途很廣,很多的人都在使用,也延伸出來了很多包,比如

封裝的D3,簡(jiǎn)化了代碼。https://github.com/Finedl/Vue-echart-D3

示例:

HighCharts,在相比echarts有更多的風(fēng)格等等,如何使用請(qǐng)參考:https://www.highcharts.com/blog/download/

總結(jié)

到此這篇關(guān)于Vue使用echarts的文章就介紹到這了,更多相關(guān)Vue使用echarts內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

龙游县| 曲水县| 瑞金市| 遂溪县| 高唐县| 滦平县| 象州县| 霍林郭勒市| 鄯善县| 顺昌县| 广灵县| 漳州市| 易门县| 洛南县| 济宁市| 绵竹市| 庆阳市| 开鲁县| 白山市| 铜陵市| 瓦房店市| 茶陵县| 美姑县| 通海县| 深圳市| 龙州县| 东至县| 蒙城县| 棋牌| 和政县| 泸州市| 新兴县| 安阳县| 含山县| 容城县| 格尔木市| 遵义市| 扎鲁特旗| 清原| 昭平县| 甘肃省|