本篇內(nèi)容主要講解“微信小程序中怎么安裝和引用ECharts”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“微信小程序中怎么安裝和引用ECharts”吧!
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),吐魯番網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:吐魯番等地區(qū)。吐魯番做網(wǎng)站價(jià)格咨詢:18982081108
Apache ECharts 官方提供了在微信小程序中使用Echarts 的代碼實(shí)例和 ec-canvas
組件,但是未發(fā)布 npm
包。
此項(xiàng)目在官方代碼之上修改支持 ec-canvas
組件傳入 echarts
可支持 npm
引入 echarts
或本地自定義構(gòu)建后的 echarts
,更符合 Web
開發(fā)體驗(yàn)。
并且發(fā)布 npm
包,支持小程序通過 npm 安裝使用。并支持 Taro
按需引入 echarts
減小打包體積。【相關(guān)學(xué)習(xí)推薦:小程序開發(fā)教程】
npm install echarts-for-weixin
參考代碼 tools/demo
1、首先在頁面的 json 文件加入 usingComponents 配置字段
{ "usingComponents": { "ec-canvas": "echarts-for-weixin" } }
2、項(xiàng)目根目錄創(chuàng)建 package.json
并執(zhí)行 npm install 安裝依賴
{ "dependencies": { "echarts": "^5.1.2", "echarts-for-weixin": "^1.0.0" } }
3、小程序開發(fā)者工具中構(gòu)建 npm
點(diǎn)擊開發(fā)者工具中的菜單欄:工具 --> 構(gòu)建 npm
4、在頁面中引入 echarts
,可以從 npm
引入 echarts
,也可以引入本地自定義構(gòu)建的 echarts
以減小體積
import * as echarts from 'echarts' // 從 npm 引入 echarts import * as echarts from './echarts' // 或者從本地引入自定義構(gòu)建的 echarts
5、然后可以在對(duì)應(yīng)頁面的 wxml 中直接使用該組件
<view class="container"> <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" echarts="{{ echarts }}" ec="{{ ec }}"></ec-canvas> </view>
6、ec-canvas
的具體用法和如何初始化圖表請(qǐng)參考 Echarts 官方小程序示例
import * as echarts from 'echarts' let chart = null; function initChart(canvas, width, height, dpr) { chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr // new }); canvas.setChart(chart); var option = { tooltip: { trigger: 'axis', axisPointer: { // 坐標(biāo)軸指示器,坐標(biāo)軸觸發(fā)有效 type: 'shadow' // 默認(rèn)為直線,可選為:'line' | 'shadow' }, confine: true }, legend: { data: ['熱度', '正面', '負(fù)面'] }, grid: { left: 20, right: 20, bottom: 15, top: 40, containLabel: true }, xAxis: [ { type: 'value', axisLine: { lineStyle: { color: '#999' } }, axisLabel: { color: '#666' } } ], yAxis: [ { type: 'category', axisTick: { show: false }, data: ['汽車之家', '今日頭條', '百度貼吧', '一點(diǎn)資訊', '微信', '微博', '知乎'], axisLine: { lineStyle: { color: '#999' } }, axisLabel: { color: '#666' } } ], series: [ { name: '熱度', type: 'bar', label: { normal: { show: true, position: 'inside' } }, data: [300, 270, 340, 344, 300, 320, 310], itemStyle: { // emphasis: { // color: '#37a2da' // } } }, { name: '正面', type: 'bar', stack: '總量', label: { normal: { show: true } }, data: [120, 102, 141, 174, 190, 250, 220], itemStyle: { // emphasis: { // color: '#32c5e9' // } } }, { name: '負(fù)面', type: 'bar', stack: '總量', label: { normal: { show: true, position: 'left' } }, data: [-20, -32, -21, -34, -90, -130, -110], itemStyle: { // emphasis: { // color: '#67e0e3' // } } } ] }; chart.setOption(option); return chart; } Page({ data: { echarts, ec: { onInit: initChart } }, onReady() { setTimeout(function () { // 獲取 chart 實(shí)例的方式 console.log(chart) }, 2000); } })
參考代碼 examples/taro
安裝依賴
npm install echarts-for-weixin
在項(xiàng)目根目錄中新建文件 project.package.json
或者自定義命名,此文件是小程序的 package.json
,并在下一步中添加小程序自定義構(gòu)建 npm 方式。這么做的目的是為了能夠共享項(xiàng)目 node_modules
project.package.json
{ "dependencies": { "echarts": "^5.1.2", "echarts-for-weixin": "^1.0.2" } }
在 project.config
的 setting
中添加小程序自定義構(gòu)建 npm 方式,參考 自定義 node_modules 和 miniprogram_npm 位置的構(gòu)建 npm 方式
{ "setting": { "packNpmManually": true, "packNpmRelationList": [ { "packageJsonPath": "../project.package.json", "miniprogramNpmDistDir": "./" } ] } }
執(zhí)行 Taro
的開發(fā)或者打包命令進(jìn)行項(xiàng)目開發(fā)
npm run dev:weapp
小程序開發(fā)者工具中構(gòu)建 npm。注意:小程序開發(fā)工具打開的項(xiàng)目目錄是 dist
文件夾
點(diǎn)擊開發(fā)者工具中的菜單欄:工具 --> 構(gòu)建 npm
在全局的 app.config.js
中添加或者在單個(gè)需要使用到 echarts
的頁面配置中添加引用組件
{ "usingComponents": { "ec-canvas": "echarts-for-weixin" } }
在頁面中引入 echarts
,可以從 npm
引入 echarts
,也可以引入本地自定義構(gòu)建的 echarts
以減小體積
import * as echarts from 'echarts' // 從 npm 引入 echarts import * as echarts from './echarts' // 或者從本地引入自定義構(gòu)建的 echarts
將引入的 echarts
傳給組件
<ec-canvas id='mychart-dom-area' canvas-id='mychart-area' echarts={echarts} // 將引入的 echarts 傳給組件 ec={this.state.ec} />
ec-canvas
的具體用法和如何初始化圖表請(qǐng)參考 Echarts 官方小程序示例
function initChart(canvas, width, height) { const chart = echarts.init(canvas, null, { width: width, height: height }) canvas.setChart(chart) const model = { yCates: ['Saturday', 'Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday', 'Sunday'], xCates: ['1', '2', '3', '4', '5'], data: [ // [yCateIndex, xCateIndex, value] [0, 0, 5], [0, 1, 7], [0, 2, 3], [0, 3, 5], [0, 4, 2], [1, 0, 1], [1, 1, 2], [1, 2, 4], [1, 3, 8], [1, 4, 2], [2, 0, 2], [2, 1, 3], [2, 2, 8], [2, 3, 6], [2, 4, 7], [3, 0, 3], [3, 1, 7], [3, 2, 5], [3, 3, 1], [3, 4, 6], [4, 0, 3], [4, 1, 2], [4, 2, 7], [4, 3, 8], [4, 4, 9], [5, 0, 2], [5, 1, 2], [5, 2, 3], [5, 3, 4], [5, 4, 7], [6, 0, 6], [6, 1, 5], [6, 2, 3], [6, 3, 1], [6, 4, 2] ] } const data = model.data.map(function (item) { return [item[1], item[0], item[2] || '-'] }) const option = { tooltip: { position: 'top' }, animation: false, grid: { bottom: 60, top: 10, left: 80 }, xAxis: { type: 'category', data: model.xCates }, yAxis: { type: 'category', data: model.yCates }, visualMap: { min: 1, max: 10, show: false, calculable: true, orient: 'horizontal', left: 'center', bottom: 10, inRange: { color: ['#37A2DA', '#32C5E9', '#67E0E3', '#91F2DE', '#FFDB5C', '#FF9F7F'], } }, series: [{ name: 'Punch Card', type: 'heatmap', data: data, label: { normal: { show: true } }, itemStyle: { emphasis: { shadowBlur: 10, shadowColor: 'rgba(0, 0, 0, 0.5)' } } }] } chart.setOption(option) return chart }export default class Echarts extends React.Component { state = { ec: { onInit: initChart } } render () { return ( <View className='echarts'> <ec-canvas id='mychart-dom-area' canvas-id='mychart-area' echarts={echarts} ec={this.state.ec} /> </View> ) } }
參考代碼 examples/taro-manual-load
注意:小程序開發(fā)者工具打開的項(xiàng)目目錄是打包后的 dist
目錄
1、安裝依賴
npm install echarts-for-weixin
2、在項(xiàng)目根目錄中新建文件 project.package.json
或者自定義命名,此文件是小程序的 package.json
,并在下一步中添加小程序自定義構(gòu)建 npm 方式。并配置 config/index.js
中的 copy
選項(xiàng),將 project.package.json
復(fù)制到 dist
目錄下并且重命名為 package.json
。并且復(fù)制 node_modules/echarts-for-weixin
至 dist/node_modules/echarts-for-weixin
避免在小程序開發(fā)者工具中打開的項(xiàng)目重新安裝依賴
project.package.json
{ "dependencies": { "echarts-for-weixin": "^1.0.2" } }
config/index.js
{ copy: { patterns: [ { from: 'project.package.json', to: 'dist/package.json' }, // 指定需要 copy 的文件 { from: 'node_modules/echarts-for-weixin/', to: 'dist/node_modules/echarts-for-weixin/' } ], options: {} } }
3、在 project.config
的 setting
中添加小程序自定義構(gòu)建 npm 方式,參考 自定義 node_modules 和 miniprogram_npm 位置的構(gòu)建 npm 方式
{ "setting": { "packNpmManually": true, "packNpmRelationList": [ { "packageJsonPath": "./package.json", "miniprogramNpmDistDir": "./" } ] } }
4、執(zhí)行 Taro
的開發(fā)或者打包命令進(jìn)行項(xiàng)目開發(fā)
npm run dev:weapp
5、小程序開發(fā)者工具中構(gòu)建 npm。注意:小程序開發(fā)工具打開的項(xiàng)目目錄是 dist
文件夾
點(diǎn)擊開發(fā)者工具中的菜單欄:工具 --> 構(gòu)建 npm
1、在全局的 app.config.js
中添加或者在單個(gè)需要使用到 echarts
的頁面配置中添加引用組件
{ "usingComponents": { "ec-canvas": "echarts-for-weixin" } }
2、在頁面中引入 echarts/core
和需要的組件,Taro 打包會(huì)只打包引入的組件,這樣達(dá)到按需引入的目的
// Import the echarts core module, which provides the necessary interfaces for using echarts. import * as echarts from 'echarts/core'; // Import charts, all with Chart suffix import { // LineChart, BarChart, // PieChart, // ScatterChart, // RadarChart, // MapChart, // TreeChart, // TreemapChart, // GraphChart, // GaugeChart, // FunnelChart, // ParallelChart, // SankeyChart, // BoxplotChart, // CandlestickChart, // EffectScatterChart, // LinesChart, // HeatmapChart, // PictorialBarChart, // ThemeRiverChart, // SunburstChart, // CustomChart, } from 'echarts/charts'; // import components, all suffixed with Component import { // GridSimpleComponent, GridComponent, // PolarComponent, // RadarComponent, // GeoComponent, // SingleAxisComponent, // ParallelComponent, // CalendarComponent, // GraphicComponent, // ToolboxComponent, TooltipComponent, // AxisPointerComponent, // BrushComponent, TitleComponent, // TimelineComponent, // MarkPointComponent, // MarkLineComponent, // MarkAreaComponent, // LegendComponent, // LegendScrollComponent, // LegendPlainComponent, // DataZoomComponent, // DataZoomInsideComponent, // DataZoomSliderComponent, // VisualMapComponent, // VisualMapContinuousComponent, // VisualMapPiecewiseComponent, // AriaComponent, // TransformComponent, DatasetComponent, } from 'echarts/components'; // Import renderer, note that introducing the CanvasRenderer or SVGRenderer is a required step import { CanvasRenderer, // SVGRenderer, } from 'echarts/renderers'; // Register the required components echarts.use( [ TitleComponent, TooltipComponent, GridComponent, BarChart, CanvasRenderer, HeatmapChart, VisualMapComponent, VisualMapContinuousComponent, VisualMapPiecewiseComponent, ] );
3、將引入的 echarts
傳給組件
<ec-canvas id='mychart-dom-area' canvas-id='mychart-area' echarts={echarts} // 將引入的 echarts 傳給組件 ec={this.state.ec} />
4、ec-canvas
的具體用法和如何初始化圖表請(qǐng)參考 Echarts 官方小程序示例
function initChart(canvas, width, height) { const chart = echarts.init(canvas, null, { width: width, height: height }) canvas.setChart(chart) const model = { yCates: ['Saturday', 'Friday', 'Thursday', 'Wednesday', 'Tuesday', 'Monday', 'Sunday'], xCates: ['1', '2', '3', '4', '5'], data: [ // [yCateIndex, xCateIndex, value] [0, 0, 5], [0, 1, 7], [0, 2, 3], [0, 3, 5], [0, 4, 2], [1, 0, 1], [1, 1, 2], [1, 2, 4], [1, 3, 8], [1, 4, 2], [2, 0, 2], [2, 1, 3], [2, 2, 8], [2, 3, 6], [2, 4, 7], [3, 0, 3], [3, 1, 7], [3, 2, 5], [3, 3, 1], [3, 4, 6], [4, 0, 3], [4, 1, 2], [4, 2, 7], [4, 3, 8], [4, 4, 9], [5, 0, 2], [5, 1, 2], [5, 2, 3], [5, 3, 4], [5, 4, 7], [6, 0, 6], [6, 1, 5], [6, 2, 3], [6, 3, 1], [6, 4, 2] ] } const data = model.data.map(function (item) { return [item[1], item[0], item[2] || '-'] }) const option = { tooltip: { position: 'top' }, animation: false, grid: { bottom: 60, top: 10, left: 80 }, xAxis: { type: 'category', data: model.xCates }, yAxis: { type: 'category', data: model.yCates }, visualMap: { min: 1, max: 10, show: false, calculable: true, orient: 'horizontal', left: 'center', bottom: 10, inRange: { color: ['#37A2DA', '#32C5E9', '#67E0E3', '#91F2DE', '#FFDB5C', '#FF9F7F'], } }, series: [{ name: 'Punch Card', type: 'heatmap', data: data, label: { normal: { show: true } }, itemStyle: { emphasis: { shadowBlur: 10, shadowColor: 'rgba(0, 0, 0, 0.5)' } } }] } chart.setOption(option) return chart } export default class Echarts extends React.Component { state = { ec: { onInit: initChart } } render () { return ( <View className='echarts'> <ec-canvas id='mychart-dom-area' canvas-id='mychart-area' echarts={echarts} ec={this.state.ec} /> </View> ) } }
5、可以查看小程序開發(fā)者工具中的依賴分析,確定文件大小以及是否正確按需引入
到此,相信大家對(duì)“微信小程序中怎么安裝和引用ECharts”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
網(wǎng)頁名稱:微信小程序中怎么安裝和引用ECharts
本文來源:http://vcdvsql.cn/article24/iijcje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、定制開發(fā)、域名注冊、、面包屑導(dǎo)航、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)