国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Taro+Vue3 小程序引入echarts表

這篇具有很好參考價(jià)值的文章主要介紹了Taro+Vue3 小程序引入echarts表。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

背景:根據(jù)需求在一個(gè)報(bào)告界面需要展示不同的echarts表來使數(shù)據(jù)更友好的顯示。

效果如下:Taro+Vue3 小程序引入echarts表,小程序,Echarts,echarts,taro,小程序

一.taro支持echarts

官方說明:Taro 文檔支持引用小程序端第三方組件庫

物料文檔:Taro 物料市場 | 讓每一個(gè)輪子產(chǎn)生價(jià)值

二.引入echarts-for-weixin插件

github地址: https://github.com/ecomfe/echarts-for-weixin

只引入ec-canvas文件夾下的wx-canvas.js

三.自定義 下載echarts

地址: https://echarts.apache.org/zh/builder.html

可自行選擇版本,筆者測試了5.3.3和5.4.1都支持

下載后得到echarts.min.js

請根據(jù)需要自行選擇需要的圖表打包下載,我只選了默認(rèn)的餅圖、柱圖、線圖;
注意點(diǎn):折線圖中如果需要顯示面積圖的平均線,則要下載對應(yīng)的標(biāo)線組件。

Taro+Vue3 小程序引入echarts表,小程序,Echarts,echarts,taro,小程序

經(jīng)過三、四步驟之后會得到兩個(gè)文件,目錄結(jié)構(gòu)如下:

Taro+Vue3 小程序引入echarts表,小程序,Echarts,echarts,taro,小程序

四.封裝組件ec-canvas.vue

<template>
  <canvas
    type="2d"
    class="ec-canvas"
    :class="canvasId" // 多個(gè)echarts時(shí)將canvasId作為唯一標(biāo)識,動態(tài)獲取canvasId用于多個(gè)                                                                           echarts可同時(shí)顯示
    :canvas-id="canvasId"
    @touchStart="touchStart"
    @touchMove="touchMove"
    @touchEnd="touchEnd"
  ></canvas>
</template>

<script lang="js">
import Taro from "@tarojs/taro";
import WxCanvas from "./wx-canvas";
import * as echarts from "./echarts-5.4.1.min";

export default {
  name: "EcCanvas",
  props: {
    canvasId: {
      type: String,
      default: ""
    },
    ec: {
      type: Object,
      default: null
    }
  },
  mounted() {
    echarts.registerPreprocessor(option => {
      if (option && option.series) {
        if (option.series.length > 0) {
          option.series.forEach(series => {
            series.progressive = 0;
          });
        } else if (typeof option.series === "object") {
          option.series.progressive = 0;
        }
      }
    });
    if (!this.ec) {
      console.warn(
        '組件需綁定 ec 變量,例:<ec-canvas id="mychart-dom-bar" ' +
        'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>'
      );
      return;
    }
    if (!this.ec.lazyLoad) {
      this.init();
    }
  },
  methods: {
    init(callback) {
      this.initByNewWay(callback);
    },
    initByNewWay(callback) {
      const query = Taro.createSelectorQuery();
      query
        .select(`.${this.canvasId}`) // 根據(jù)canvasId動態(tài)獲取不同的echarts圖表
        .fields({
          node: true,
          size: true
        })
        .exec(res => {
          if (!res || res.length == 0 || res[0] == null || res[0].node == null) {
            console.error('未獲取到canvas的dom節(jié)點(diǎn),請確認(rèn)在頁面渲染完成后或節(jié)點(diǎn),taro中頁面渲染完成的生命周期是useReady');
            return
          }
          const canvasNode = res[0].node;
          // this.canvasNode = canvasNode;

          const canvasDpr = Taro.getSystemInfoSync().pixelRatio;
          const canvasWidth = res[0].width;
          const canvasHeight = res[0].height;

          const ctx = canvasNode.getContext("2d");

          const canvas = new WxCanvas(ctx, this.canvasId, true, canvasNode);
          echarts.setCanvasCreator(() => {
            return canvas;
          });

          if (typeof callback === "function") {
            this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr);
          } else if (typeof this.ec.onInit === "function") {

            this.chart = this.ec.onInit(
              canvas,
              canvasWidth,
              canvasHeight,
              canvasDpr
            );
          } else {

            this.triggerEvent('init', {
              canvas: canvas,
              width: canvasWidth,
              height: canvasHeight,
              dpr: canvasDpr
            })
          }
        });
    },
    canvasToTempFilePath(opt) {
      const query = Taro.createSelectorQuery().in(this);
      query
        .select(".ec-canvas")
        .fields({
          node: true,
          size: true
        })
        .exec(res => {
          const canvasNode = res[0].node;
          opt.canvas = canvasNode;
          Taro.canvasToTempFilePath(opt);
        });
    },
    touchStart(e) {
      if (this.chart && e.touches.length > 0) {
        var touch = e.touches[0];
        var handler = this.chart.getZr().handler;
        handler.dispatch("mousedown", {
          zrX: touch.x,
          zrY: touch.y
        });
        handler.dispatch("mousemove", {
          zrX: touch.x,
          zrY: touch.y
        });
        handler.processGesture(this.wrapTouch(e), "start");
      }
    },
    touchMove(e) {
      if (this.chart && e.touches.length > 0) {
        var touch = e.touches[0];
        var handler = this.chart.getZr().handler;
        handler.dispatch("mousemove", {
          zrX: touch.x,
          zrY: touch.y
        });
        handler.processGesture(this.wrapTouch(e), "change");
      }
    },
    touchEnd(e) {
      if (this.chart) {
        const touch = e.changedTouches ? e.changedTouches[0] : {};
        var handler = this.chart.getZr().handler;
        handler.dispatch("mouseup", {
          zrX: touch.x,
          zrY: touch.y
        });
        handler.dispatch("click", {
          zrX: touch.x,
          zrY: touch.y
        });
        handler.processGesture(this.wrapTouch(e), "end");
      }
    },
    wrapTouch(event) {
      for (let i = 0; i < event.touches.length; ++i) {
        const touch = event.touches[i];
        touch.offsetX = touch.x;
        touch.offsetY = touch.y;
      }
      return event;
    }
  }
};
</script>

<style>
.ec-canvas {
  width: 100%;
  min-height: 400rpx;
  flex: 1;
}
</style>

?五.封裝e-chart.vue組件

<template>
  <ec-canvas ref="ecCanvasRef" :canvas-id="canvasId" :ec="ec"></ec-canvas>
</template>

<script lang="js">
/**
 * 自定義下載的echarts.min.js 文件  要使用需使用js,ts需要聲明文件
 * 此組件不能使用setup語法糖,會報(bào)錯(cuò)的.
 */
import Taro, { useLoad } from "@tarojs/taro";
import * as echarts from './ec-canvas/echarts-5.4.1.min'
import EcCanvas from './ec-canvas/ec-canvas.vue'
import { ref, reactive } from "vue";
export default {
    components: {
        EcCanvas
    },
    props: {
        canvasId: {
            type: String,
            default: ''
        }
    },
    setup(props, { expose }) {
        const ec = reactive({
            lazyLoad: true
        })
        const ecCanvasRef = ref(null);

        const refresh = (options) => {
            if (!ecCanvasRef.value) {
                console.error('ecCanvas未獲取到dom');
                return
            }
            ecCanvasRef.value?.init((canvas, width, height, canvasDpr) => {
                const chart = echarts.init(canvas, null, {
                    width: width,
                    height: height,
                    devicePixelRatio: canvasDpr
                })
                canvas.setChart(chart);
                chart.setOption(options);
                return chart;
            })
        }

        expose({
            refresh,
        })
        return {// 返回值會暴露給模板和其他的選項(xiàng)式 API 鉤子
            ec, ecCanvasRef
        }
    },
}
</script>

六.使用封裝的組件

此時(shí)封裝的目錄結(jié)構(gòu)如下:

Taro+Vue3 小程序引入echarts表,小程序,Echarts,echarts,taro,小程序Taro+Vue3 小程序引入echarts表,小程序,Echarts,echarts,taro,小程序

子組件:

<template>
  <view class="cards bg-white mx-sm mt-mm">
    <view class="text-center pt-xs pb-mm">
      <text class="text-md text-dark ml-sm">借貸分析</text>
    </view>
   
    <cover-view class="container-echarts">
      <e-chart ref="barChat" canvas-id="bar-canvas" />
    </cover-view>
  </view>
</template>
<script lang="ts">
export default { name: 'loanAnalysis' };
</script>

<script lang="ts" setup>
import Taro, { useReady } from '@tarojs/taro';
import { ref } from 'vue';
import EChart from '../components/echarts/e-chart.vue';
import { getLoanView } from '@/service/report';
import { MoneyNoUnitText, FormatThousand } from '@/units/currency';

const components = { 'e-chart': EChart };

const props = defineProps({
  params: { type: Object, required: true }
});

useReady(() => {
  initMultiBarChart();
});
const barChat = ref<any>();
let loanAmount = ref<number>();
let repaymentLoanAmount = ref<number>();
let prototypeOption = {
  grid: { left: 14, right: 18, bottom: 8, top: 40, containLabel: true },
  legend: { show: false },
  color: ['#40a9ff', '#FF5652'],
  xAxis: [
    {
      type: 'category',
      data: [],
      axisLabel: { fontSize: 10, color: 'rgba(0, 0, 0, 0.56)' },
      axisLine: { show: false },
      axisTick: { show: false }
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '萬元',
      nameTextStyle: { padding: [0, 40, 0, 0], color: 'rgba(0,0,0,0.56)' },
      axisLabel: { fontSize: 10, color: 'rgba(0, 0, 0, 0.56)' },
      axisLine: { show: false },
      axisTick: { show: false },
      splitNumber: 2
    }
  ],
  series: [
    {
      name: '貸款、借款流入',
      type: 'bar',
      data: [],
      barMaxWidth: 12,
      barMinHeight: 1,
      stack: 'cr',
      itemStyle: {
        barBorderRadius: [0, 2, 0, 0]
      }
    },
    {
      name: '償還貸款、借款',
      type: 'bar',
      data: [],
      barMaxWidth: 12,
      barMinHeight: 1,
      stack: 'cr',
      itemStyle: {
        barBorderRadius: [0, 0, 0, 2]
      }
    }
  ]
};

const initMultiBarChart = () => {
  getLoanView(props.params).then(res => {
    if (res) {
      loanAmount.value = MoneyNoUnitText(res.loanAmount || 0);
      repaymentLoanAmount.value = MoneyNoUnitText(res.repaymentLoanAmount || 0);

      prototypeOption.xAxis[0].data = res.list.map(
        i => i.dateId.toString().slice(2, 4) + '.' + i.dateId.toString().slice(4)
      );
      prototypeOption.yAxis[0].axisLabel = {
        formatter: (v: any) => (Math.abs(v) > 0 ? FormatThousand(parseInt(v / 10000), 0) : v),
        color: 'rgba(0, 0, 0, 0.56)'
      };
      prototypeOption.series[0].data = res.list.map(i => (i.loanAmount > 0 ? i.loanAmount : null));
      prototypeOption.series[1].data = res.list.map(i =>
        i.repaymentLoanAmount > 0 ? -i.repaymentLoanAmount : null
      );

      Taro.nextTick(() => {
        barChat.value.refresh(prototypeOption);
      });
    }
  });
};
</script>

<style>
.container-echarts {
  height: 400rpx;
  width: 100%;
}
</style>

父組件引用子組件:

import loanAnalysis from './loan-analysis.vue';

 <loan-analysis :params="params"></loan-analysis>



【 問題描述 】在小程序使用echarts,圖表展示出來之后不跟隨頁面滾動,浮在最上方。
【 問題原因 】布局中含有position:fixed或absulote的元素,導(dǎo)致echarts圖表無法滑動。
官方文件說明:canvas為原生組件故有一下的性質(zhì): bash 由于原生組件脫離在 WebView 
渲染流程外,因此在使用時(shí)有以下限制: 組件的層級是最高的,所以頁面中的其他組件無論設(shè)置
 z-index 為多少,都無法蓋在原生組件上。 后插入的原生組件可以覆蓋之前的原生組件。 
原生組件還無法在 scroll-view、swiper、picker-view、movable-view 中使用。 
部分CSS樣式無法應(yīng)用于原生組件,例如: 無法對原生組件設(shè)置 CSS 動畫 無法定義
原生組件為 position: fixed 不能在父級節(jié)點(diǎn)使用 overflow:hidden 來裁剪
原生組件的顯示區(qū)域
【 解決方法 】所有父級元素(有包含ec-canvas的所有view元素)的position不可為
fixed或absulote,將其改為其他,另外兄弟元素可以為position:absulote或fixed,
否在還是會有重疊

原文鏈接:https://blog.csdn.net/samscat/article/details/126182735

?參考博文:Taro3+Vue3使用echarts_taro使用echarts_江渚清沨的博客-CSDN博客文章來源地址http://www.zghlxwxcb.cn/news/detail-648467.html

到了這里,關(guān)于Taro+Vue3 小程序引入echarts表的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • vue3+taro+Nutui 開發(fā)小程序(一)

    vue3+taro+Nutui 開發(fā)小程序(一)

    前言:最近在調(diào)研開發(fā)小程序,發(fā)現(xiàn)現(xiàn)在taro框架逐漸成熟,能完美地使用vue3來進(jìn)行開發(fā),調(diào)研中發(fā)現(xiàn)京東的Nutui也不錯(cuò)所以準(zhǔn)備寫一個(gè)由0到1的vue3+taro+Nutui的小程序。 這篇我們首先搭建一個(gè)框架: vscode插件準(zhǔn)備環(huán)節(jié): 目前我用到的插件如下: Eslint? ?用來vue格式化代碼的

    2024年02月08日
    瀏覽(23)
  • vue2與vue3—引入echarts以及使用

    npm install echarts --save???? ? main.js中? ?vue組件中 引入方法一:?通過getCurrentInstance main.js文件中: vue組件中: 引入方法二: 組件中直接引入

    2024年02月16日
    瀏覽(45)
  • Taro+Vue3開發(fā)微信小程序的分享好友功能

    Taro+Vue3開發(fā)微信小程序的分享好友功能

    1、背景:使用taro框架和vue3框架開發(fā)小程序,實(shí)現(xiàn)一個(gè)把pdf文件直接分享給微信好友。 一開始看taro文檔理解有偏差,導(dǎo)致分享出去的文件是個(gè)app的小程序連接。如下圖 ?實(shí)現(xiàn)代碼如下: 后續(xù)仔細(xì)查看taro文檔, Taro 文檔,發(fā)現(xiàn)這種寫法是個(gè)頁面組件,轉(zhuǎn)發(fā)出去的是個(gè)小程序,

    2024年02月12日
    瀏覽(28)
  • Vue3 手把手按需引入 Echarts

    Vue3 手把手按需引入 Echarts

    背景:新項(xiàng)目采用 Vue3 作為前端項(xiàng)目框架,避免不了要使用 echarts ,但是在使用的時(shí)候,出現(xiàn)了與 Vue2 使用不一樣的地方,所以特別記下來,希望給到有需要的同學(xué)一些幫助。 在你自己需要的目錄下創(chuàng)建引入 eachrts 配置的文件,我是在 src/utils 目錄下創(chuàng)建的 echarts.ts 文件(

    2024年02月02日
    瀏覽(22)
  • Vue3中按需引入ECharts(一看就會)

    Vue3中按需引入ECharts(一看就會)

    使用背景:比如做一個(gè)工程化項(xiàng)目,且只使用到柱狀圖和折線圖,如果把所有的echarts組件都引入到項(xiàng)目中的話,會影響用戶打開頁面的速度和項(xiàng)目的性能。所以對于做一個(gè)高逼格的程序工程師,我們需要按需引入。 ? 1:廢話不多說 老步驟安裝 ? 2:自己新建一個(gè) js 文件(名

    2024年02月16日
    瀏覽(20)
  • taro+vue3 搭建一套框架,適用于微信小程序和H5

    安裝 Taro??梢栽诮K端輸入以下命令進(jìn)行安裝: 創(chuàng)建項(xiàng)目。使用以下命令創(chuàng)建 Taro+Vue3 項(xiàng)目: 其中,myApp 是項(xiàng)目名稱。 進(jìn)入項(xiàng)目并啟動。使用以下命令進(jìn)入項(xiàng)目并啟動: 注意,需要先進(jìn)入對應(yīng)的目錄再啟動。 編寫代碼。在 src 目錄下編寫代碼,可以像使用 Vue 開發(fā) Web 應(yīng)用程

    2024年02月10日
    瀏覽(76)
  • vue3 setup+Taro3 調(diào)用原生小程序自定義年月日時(shí)分多列選擇器,NutUI改造

    vue3 setup+Taro3 調(diào)用原生小程序自定義年月日時(shí)分多列選擇器,NutUI改造

    NutUI 有日期時(shí)間選擇器,但是滑動效果太差,卡頓明顯。換成 原生小程序 很順暢 上代碼: 若需要自定義年開始時(shí)間,見 initColumn 方法 如作為組件,通過父級傳遞,可使用:

    2024年02月13日
    瀏覽(20)
  • Taro + vue3 實(shí)現(xiàn)自定義返回欄

    Taro + vue3 實(shí)現(xiàn)自定義返回欄

    算是一個(gè)簡單的返回頁面? 主要是這里 Taro +vue3 的頁面結(jié)構(gòu)還有一個(gè)?

    2024年04月29日
    瀏覽(25)
  • Taro+vue3 實(shí)現(xiàn)滑動列表 時(shí)切換電影

    Taro+vue3 實(shí)現(xiàn)滑動列表 時(shí)切換電影

    以上代碼是滑動的組件?

    2024年01月16日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包