uniapp引入echarts

一、uniapp引入echarts

在uniapp中引入echarts非常簡單,只需在頁面中引入echarts.js即可。

<script src="@/echarts.min.js"></script>

然後在頁面mounted生命周期中初始化echarts實例,並使用option屬性設置相關圖表配置。

<template>
  <view>
    <ec-canvas ref="mychart" @canvasInit="initChart" :canvas-id="canvasId" :canvas-type="canvasType"></ec-canvas>
  </view>
</template>
<script>
import * as echarts from '@/echarts.min.js'; // 引入 echarts
export default {
  data () {
    return {
      canvasId: 'mychart', // canvas id
      canvasType: 'ec-canvas', // canvas 類型
      chart: null // echarts 實例
    }
  },
  mounted () {
    this.initChart();
  },
  methods: {
    initChart () {
      const ecComponent = this.$refs.mychart;
      ecComponent.init((canvas, width, height) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        this.chart = chart;
        chart.setOption(this.getOption());
        return chart;
      });
    },
    getOption () {
      // echarts option 配置
    }
  }
}
</script>

以上代碼為uniapp引入echarts的基本流程,可以根據具體需求進行echarts圖表的配置。

二、uniapp引入uview

如果想要在uniapp中使用uview集成的echarts組件,則需先在項目中安裝uview組件庫。

npm install uview-ui

然後在需要使用echarts組件的頁面中引入uview的echarts組件即可。

<template>
  <view>
    <u-echarts option="{{ options }}" :height="300" />
  </view>
</template>
<script>
import uEcharts from "@/components/u-echarts/u-echarts.vue";
export default {
  components: {
    uEcharts
  },
  data () {
    return {
      options: this.getOption()
    }
  },
  methods: {
    getOption () {
      // echarts option 配置
    }
  }
}
</script>

使用uview進行echarts的配置較為方便,但需注意使用uview組件時需要將組件名聲明在components屬性中。

三、uniapp引入vant組件

引入vant組件庫也是一種方便快捷的方式來使用echarts。先進行vant組件庫的安裝。

npm i vant -S

然後在需要使用echarts組件的頁面中引入vant相關組件即可。

<template>
  <view>
    <van-echarts :option="options" width="100%" height="500px" />
  </view>
</template>
<script>
import { vantECharts } from 'vant';
export default {
  components: {
    vantECharts
  },
  data () {
    return {
      options: this.createOptions()
    }
  },
  methods: {
    createOptions () {
      // echarts option 配置
    }
  }
}
</script>

以上為uniapp引入vant組件使用echarts的方式。

四、uniapp引入高德地圖

要在uniapp中使用echarts繪製地圖,則還需要引入高德地圖JS API。

<script src="https://webapi.amap.com/maps?v=1.4.15&key=your-key-here"></script>

然後在需要地圖的頁面引入echarts和map組件,設置相關參數即可。

<template>
  <view>
    <ec-canvas ref="map" @canvasInit="initChart" :canvas-id="canvasId" :canvas-type="canvasType"></ec-canvas>
  </view>
</template>
<script>
import * as echarts from '@/echarts.min.js'; // 引入 echarts
export default {
  data () {
    return {
      canvasId: 'map',
      canvasType: 'ec-canvas',
      chart: null
    }
  },
  mounted () {
    this.initChart();
  },
  methods: {
    initChart () {
      const ecComponent = this.$refs.map;
      ecComponent.init((canvas, width, height) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        this.chart = chart;
        chart.setOption(this.getOption());
        return chart;
      });
    },
    getOption () {
      return {
        // echarts map option 配置
      }
    }
  }
}
</script>

五、uniapp引入iconfont

如果需要在echarts中使用iconfont圖標,則需先在頁面中引入iconfont的js文件。

<script src="//at.alicdn.com/t/font_xxx.js"></script>

然後將相關的iconfont配置添加進echarts option中即可。

<template>
  <view>
    <ec-canvas ref="mychart" @canvasInit="initChart" :canvas-id="canvasId" :canvas-type="canvasType"></ec-canvas>
  </view>
</template>
<script>
import * as echarts from '@/echarts.min.js'; // 引入 echarts
export default {
  data () {
    return {
      canvasId: 'mychart',
      canvasType: 'ec-canvas',
      chart: null
    }
  },
  mounted () {
    this.initChart();
  },
  methods: {
    initChart () {
      const ecComponent = this.$refs.mychart;
      ecComponent.init((canvas, width, height) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        this.chart = chart;
        chart.setOption(this.getOption());
        return chart;
      });
    },
    getOption () {
      return {
        series: {
          type: 'scatter',
          symbolSize: (val) => {
            return val[2] * 4
          },
          data: [
            [28793, 900000, 29],
            [38892, 1100000, 47],
            [29341, 500000, 72],
            [32366, 700000, 29],
            [29356, 100000, 57],
            [35467, 600000, 20],
            [45718, 1400000, 50],
            [52313, 900000, 17],
            [24617, 1000000, 50],
            [48512, 600000, 82],
            [34141, 700000, 41],
            [32531, 700000, 30],
            [46734, 800000, 80],
            [43204, 900000, 40]
          ],
          itemStyle: {
            color: '#FFC107',
            opacity: 0.8,
            /* 關鍵代碼開始 */
            symbol: (val) => {
              if (val[2] <= 30) {
                return 'path://M32 16.417A15.963 15.963 0 0 0 16.375 1 15.96 15.96 0 0 0 .104 15.976a15.962 15.962 0 0 0 14.271 15.855l.031.001a15.96 15.96 0 0 0 15.855-14.274A15.961 15.961 0 0 0 32 16.417zm-7.57 8.253l-4.593-2.645a.748.748 0 0 1-.036-1.309l1.256-.854a.748.748 0 0 1 1.053.271l3.098 4.779.032.046a.746.746 0 0 1-.052.818l-.727.701a.749.749 0 0 1-1.023.057z'
              }
              return 'circle'
            }
            /* 關鍵代碼結束 */
          }
        }
      }
    }
  }
}
</script>

以上代碼為使用iconfont圖標的例子。

六、uniapp輕量級echarts

如果項目需要使用輕量級的echarts,可以使用uniapp提供的一個輕量級echarts組件。在頁面中引入輕量級echarts組件即可。

<template>
  <view>
    <mp-echarts :echarts-options="options" width="{{ width }}" height="{{ height }}" />
  </view>
</template>
<script>
import mpEcharts from '@/components/mp-echarts/mp-echarts';
export default {
  components: {
    mpEcharts
  },
  data () {
    return {
      width: uni.getSystemInfoSync().windowWidth,
      height: 300,
      options: {
        // echarts option 配置
      }
    }
  }
}
</script>

輕量級echarts使用方式與普通版echarts較為相似,需要注意配置正確的option屬性以正常顯示圖表。

七、uniapp引入axios

如果在使用echarts時需要獲取伺服器數據,則需要使用uniapp封裝的http請求庫axios進行數據請求。

首先在項目中安裝axios。

npm i axios

然後在需要使用axios進行數據請求的頁面中引入axios並根據情況設置相應的參數。

<template>
  <view>
    <ec-canvas ref="mychart" @canvasInit="initChart" :canvas-id="canvasId" :canvas-type="canvasType"></ec-canvas>
  </view>
</template>
<script>
import * as echarts from '@/echarts.min.js'; // 引入 echarts
import axios from "axios"; // 引入 axios
export default {
  data () {
    return {
      canvasId: 'mychart',
      canvasType: 'ec-canvas',
      chart: null,
      data: []
    }
  },
  mounted () {
    this.getData().then(() => {
      this.initChart();
    })
  },
  methods: {
    getData () {
      return axios.get('/api/data').then(resp => {
        this.data = resp.data;
      })
    },
    initChart () {
      const ecComponent = this.$refs.mychart;
      ecComponent.init((canvas, width, height) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        this.chart = chart;
        chart.setOption(this.getOption());
        return chart;
      });
    },
    getOption () {
      return {
        xAxis: {
          type: 'category',
          data: this.data.map(item => item.name)
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: this.data.map(item => item.value),
          type: 'bar'
        }]
      }
    }
  }
}
</script>

以上為使用axios請求數據後,使用echarts渲染圖表的例子。

八、uniapp引入js文件

如果需要在echarts中直接使用js文件或跨域數據,可以使用uniapp封裝的uni.loadJs方法。

在需要使用js文件的頁面中引入js文件即可。

<script>
  uni.loadJs('https://example.com/example.js').then(() => {
    console.log('example.js loaded');
    // 對 js 文件進行處理
  })
</script>

以上為在echart中引入js文件的示例。

九、uniapp

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/198394.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-04 10:25
下一篇 2024-12-04 10:25

相關推薦

  • Echarts 地圖 Label 增加背景圖

    本文將從多個方面對 Echarts 地圖 Label 增加背景圖進行詳細的闡述。 一、背景圖的作用 為 Echarts 地圖添加背景圖可以使 Label 更加直觀、美觀,提升視覺效…

    編程 2025-04-29
  • ECharts地圖輪播

    本文將從插件基礎、數據準備及處理、地圖呈現、輪播控制等方面,對ECharts地圖輪播進行詳細闡述。 一、插件基礎 ECharts官方提供了一個名為「echarts-gl」的插件,它…

    編程 2025-04-27
  • uniapp分頁第二次請求用法介紹

    本文將從多個方面對uniapp分頁第二次請求進行詳細闡述,並給出對應的代碼示例。 一、請求參數的構造 在進行分頁請求時,需要傳遞的參數體包含當前頁碼以及每頁顯示的數據量。對於第二次…

    編程 2025-04-27
  • uniapp ios打包詳解

    一、環境搭建 首先需要安裝Xcode,並在Xcode中登錄自己的Apple ID,開啟自己的開發者賬戶。 接著,需要在uniapp項目中配置簽名證書和描述文件。步驟如下: 在Xco…

    編程 2025-04-25
  • uniapp導航欄字體大小探究

    隨著移動端應用的發展,導航欄越來越成為移動應用中一個重要的組成部分。在如此眾多的移動端開發框架中,uniapp已經成為了很多開發者的首選。在uniapp中,導航欄也是一個非常重要的…

    編程 2025-04-25
  • 全面解析uniapp如何獲取當前位置

    一、uniapp使用定位API實現實時定位 1、uniapp提供的定位API可以獲取當前設備位置,使用方法如下: // 開啟實時定位 uni.startLocationUpdate…

    編程 2025-04-25
  • Echarts 雙 Y 軸詳解

    一、什麼是雙 Y 軸 Echarts 雙 Y 軸是一種常見的圖表類型,它可以在同一張圖表中展示多個指標,每個指標都有自己的 Y 軸。這種圖表類型主要用於展示異構指標之間的關係,可以…

    編程 2025-04-25
  • uniapp面試題解析

    一、uniapp簡介 uniapp是一種基於vue.js框架的開源跨平台開發框架,可以讓開發者使用vue的語法在多個平台上進行一次編譯即可生成iOS、Android、Web和小程序…

    編程 2025-04-25
  • uniapp跳轉到外部鏈接詳解

    一、常規跳轉方式 1、使用a標籤進行跳轉: <a href=”https://www.baidu.com”>跳轉到百度</a> 2、使用window.loc…

    編程 2025-04-24
  • uniapp打包app指南

    一、準備工作 在開始打包app之前,我們需要預先準備好一些工作。首先,確保你已經安裝了相關的軟體:HBuilderX、Android SDK、Xcode(如果你需要在iOS上打包)…

    編程 2025-04-24

發表回復

登錄後才能評論