小程序开发中遇到的问题(6)-小程序中怎么使用echarts

2018-07-03 16:49:00

一. 首先请看官方文档,毕竟是权威的

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

二. 如果看完后,还是感觉晕乎乎的,那么请按照我下面的步骤进行

1. 首先就你的当前页中添加

    "usingComponents": {
        "ec-canvas": "../../../ec-canvas/ec-canvas"
    }

2. wxml中添加

    
      
    

3. wxss 中添加样式。 这里,我添加了一个固定的高度

ec-canvas {
  width: 100%;
  height: 400rpx;
}

4. js 中添加代码

    import * as echarts from '../../../ec-canvas/echarts';

 

let chart = null; function initChart(canvas, width, height) { chart = echarts.init(canvas, null, { width: width, height: height }); canvas.setChart(chart); var option = { color: ['#37a2da', '#32c5e9', '#67e0e3'], tooltip: { trigger: 'axis', axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, legend: { data: ['热度', '正面', '负面'] }, 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: ['汽车之家', '今日头条', '百度贴吧', '一点资讯', '微信', '微博', '知乎'], 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: '负面', 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: { ec: { onInit: initChart } }, onReady() { setTimeout(function () { // 获取 chart 实例的方式 console.log(chart) }, 2000); } });
如图: 注意, 路径是关键。因为前期我路径写错了,一直提示
VM9731:1 thirdScriptError 
 sdk uncaught third Error 
 module "pages/ec-canvas/echarts.js" is not defined 
 Error: module "pages/ec-canvas/echarts.js" is not defined

Page is not constructed because it is not found.

错误。 经过排查,最后发现是引入js 的路径不对。 除了js 中的路径,还需要注意json 文件中的路径。 最后结果如图

关于

联系方式 :

mail: hey_cool@163.com ,
QQ:583459700

备案许可证编号:蜀ICP备16005545号-1 © COPYRIGHT 2015-2024 zhmzjl.com | by: KAPO