一、Echarts(小程序版)
1.1:下载 GitHub 上的 项目
https://github.com/ecomfe/echarts-for-weixin
1.2:但项目下载之后,打开小程序开发工具,可以看到效果如下,适配性还是比较完美的。
1.3.如果是在项目里面引入组件的话,打开从github上面下载的代码,将ec-canvas
文件夹复制黏贴到你的项目里面。
好的,组件已经复制到了我的项目里面,现在我想实现一个折线图,现在开始去组件里面搬运复制黏贴代码了。
wxml(通过组件的方式使用)
<!--index.wxml-->
<view class="container"><ec-canvas id="mychart-dom-line" canvas-id="mychart-line" ec="{{ ec }}"></ec-canvas>
</view>
js(引入插件的js,并进行初始化)
import * as echarts from '../../ec-canvas/echarts';const app = getApp();function initChart(canvas, width, height) {const chart = echarts.init(canvas, null, {width: width,height: height});canvas.setChart(chart);var option = {title: {text: '测试下面legend的红色区域不应被裁剪',left: 'center'},color: ["#37A2DA", "#67E0E3", "#9FE6B8"],legend: {data: ['A', 'B', 'C'],top: 50,left: 'center',backgroundColor: 'red',z: 100},grid: {containLabel: true},tooltip: {show: true,trigger: 'axis'},xAxis: {type: 'category',boundaryGap: false,data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],// show: false},yAxis: {x: 'center',type: 'value',splitLine: {lineStyle: {type: 'dashed'}}// show: false},series: [{name: 'A',type: 'line',smooth: true,data: [18, 36, 65, 30, 78, 40, 33]}, {name: 'B',type: 'line',smooth: true,data: [12, 50, 51, 35, 70, 30, 20]}, {name: 'C',type: 'line',smooth: true,data: [10, 30, 31, 50, 40, 20, 10]}]};chart.setOption(option);return chart;
}Page({onShareAppMessage: function (res) {return {title: 'ECharts 可以在微信小程序中使用啦!',path: '/pages/index/index',success: function () { },fail: function () { }}},data: {ec: {onInit: initChart}},onReady() {}
});
json(注册组件)
{"usingComponents": {"ec-canvas": "../../ec-canvas/ec-canvas"}
}
css(需要写一下css的样式,这一段是必须的,不然无法展示图像)
ec-canvas {width: 100%;height: 100%;
}.container {position: absolute;top: 0;bottom: 0;left: 0;right: 0;display: flex;flex-direction: column;align-items: center;justify-content: space-between;box-sizing: border-box;
}.picker-pos {margin-top: -130rpx;margin-left: 150rpx;color: blueviolet;
}
这个时候 小程序Echarts图表组件算是已经可以运用在项目里面啦
1.4.说明
1.4.1参数详见 Echarts官网,其实这里的参数是Echarts的pc版的参数说明,但小程序版差不多一样;
1.4.2使用之前建议去 https://github.com/ecomfe/echarts-for-weixin 看一下,这里说明了一些注意事项,查看完后再做使用取舍;
1.4.3在弄demo的时候,用手机扫码(git里的那个体验码),感觉效果还不错,把demo弄出来后在手机里查看发现坐标轴上的数字特别小(猜想那个插件的js部分可能不是最新的);
1.4.4看了Echarts官网的pc版,做的体验功能挺不错的,但小程序端还是有待完善的(例如折线图的这个弹出),猜想可能是PC端做了这块的边界判断,但小程序部分没有,希望后续能加上完善这里。
1.4.5 转载地址:https://cloud.tencent.com/developer/article/1450181
1.4.6 所以说,这个使用还是存在一点瑕疵的,是否使用需要自己来衡量评估一下。
二、wxcharts.js和uCharts.js
可以点击参考这篇博客