echarts小程序组件下载地址:https://echarts.apache.org/zh/tutorial.html#%E5%9C%A8%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%B8%AD%E4%BD%BF%E7%94%A8%20ECharts

以上是echarts官方给的小程序引入的示例;
但是在kbone中需要修改一下:
打开build/miniprogram.config.js
module.exports = {
origin: 'https://test.miniprogram.com',
entry: '/',
router: {
home: [
'/(home|index)?',
],
other: [
'/other',
],
},
redirect: {
notFound: 'home',
accessDenied: 'home',
},
generate: {
autoBuildNpm: 'npm',
app : 'default', //app 输出配置,支持的值 default默认值,会输出 app.js、app.json、app.wxss 和 project.config.json 等文件
//分包
subpackages : {
package1 : ['other'],
// package2 : ['other']
},
//分包预加载
preloadRule: {
// 进入 home 页面时,会预下载名为 package1 的分包
home: {
network: 'all',
packages: ['package1'],
},
},
//引入第三方小程序组件
wxCustomComponent: {
root: path.resolve(__dirname, '../src/components'),
usingComponents: {
'ec-canvas': {
path : 'ec-canvas/ec-canvas',
props : ['ec', 'init'],
events : []
},
"van-button": {
path : '@vant/weapp/button/index',
props : ['type'],
events : []
},
"van-calendar": {
path : '@vant/weapp/calendar/index',
props : ['show'],
events : ['confirm']
},
},
},
},
.......
.......
}
添加wxCustomComponent,看↑↑↑代码,尤其需要注意的是kbone中引入第三方组件的时候传参是需要设置的不设置不认!所以props,或者events自行添加
这里用到了vantUI的对应2个ui组件,一个button一个calendar日历选择,也是示例用的;
设置完这一步之后,看页面组件具体实现
<template>
<div>
<div v-if="!isMiniprogram" id="mychart-dom" style="height:300px"></div>
<ec-canvas v-if="isMiniprogram" id="mychart-dom-line" canvas-id="mychart-line" :ec="ec"></ec-canvas>
<van-button type="default" @click="showCalendar=true">打开日历</van-button>
<van-button type="primary">主要按钮</van-button>
<van-button type="info">信息按钮</van-button>
<van-button type="warning">警告按钮</van-button>
<van-button type="danger" @click="$navjumper.go('/other')">跳转other页</van-button>
<van-calendar v-if="!isMiniprogram" v-model="showCalendar" @confirm="onConfirm" />
<van-calendar v-if="isMiniprogram" :show="showCalendar" @confirm="onConfirm" />
</div>
</template>
<script>
export default {
name: '',
components: {},
data (){
return {
//echart对象
isWeb : false,
chartObj : null,
ec: {
onInit: this.initChart
},
//日历
showCalendar : false,
}
},
methods: {
onConfirm(res){
this.showCalendar = false;
var date = process.env.isMiniprogram ? new Date(res.detail) : res;
var str = `${date.getMonth() + 1}/${date.getDate()}`;
console.log(str)
},
initChart(canvas, width, height, dpr , echarts){
var vue = this;
vue.chartObj = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr // new
});
canvas.setChart(vue.chartObj);
return vue.chartObj;
},
setEchartOption(){
var vue = this;
if(!vue.chartObj){
setTimeout(function(){
vue.setEchartOption();
},200);
return;
}
var option = {
grid:{
x:10,
y:30,
x2:0,
y2:-20,
borderWidth:1
},
color: ["#fe6228", "#28c4fe"],
legend: {
//对应series
data: ['test'],
top: 20,
left: 'center',
z: 100
},
grid: {
containLabel: true
},
tooltip: {
show: true,
trigger: 'axis',
triggerOn: 'click',
formatter: function (params) {
var data = params[0];
var html = `${data.seriesName}:${data.value}\n检查日期:${data.axisValue}`;
if(vue.xAxisDataTmp){
var time = vue.xAxisDataTmp[data.dataIndex]
html = `${data.seriesName}:${data.value}\n检查日期:${time}`;
}
return html;
}
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ['12-10', '12-11', '12-12'],
// show: false
},
yAxis: {
x: 'center',
type: 'value',
splitLine: {
lineStyle: {
type: 'dashed'
}
}
// show: false
},
series: [
{
name: 'test',
type: 'line',
smooth: true,
data: [2.0, 3.0, 4.0]
},
// {
// name: 'mg',
// type: 'line',
// smooth: true,
// data: [12, 50, 51]
// }
]
};
vue.chartObj.setOption(option);
},
},
created() {
this.isMiniprogram = process.env.isMiniprogram
},
mounted(){
//web端
if(!this.isMiniprogram){
this.chartObj = this.$echarts.init(document.getElementById('mychart-dom'));
// this.$Toast('提示内容');
}
this.setEchartOption()
}
}
</script>
<style lang="less">
</style>
具体效果:

pc网站打开:

在kbone中有一个坑,也就是回调之后,需要创建canvas对象再初始化echarts;但是kbone中和小程序中canvas对象并不一致,所以得略修改下echarts组件的代码,打开组件目录,找到ec-canvas.js中initByOldWay、initByNewWay方法改为:
initByOldWay(callback) {
// 1.9.91 <= version < 2.9.0:原来的方式初始化
ctx = wx.createCanvasContext(this.data.canvasId, this);
const canvas = new WxCanvas(ctx, this.data.canvasId, false);
echarts.setCanvasCreator(() => {
return canvas;
});
// const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
const canvasDpr = 1
var query = wx.createSelectorQuery().in(this);
query.select('.ec-canvas').boundingClientRect(res => {
if (typeof callback === 'function') {
this.chart = callback(canvas, res.width, res.height, canvasDpr);
}
else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr , echarts);
}
else {
this.triggerEvent('init', {
canvas: canvas,
width: res.width,
height: res.height,
canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
});
}
}).exec();
},
initByNewWay(callback) {
// version >= 2.9.0:使用新的方式初始化
const query = wx.createSelectorQuery().in(this)
query
.select('.ec-canvas')
.fields({ node: true, size: true })
.exec(res => {
const canvasNode = res[0].node
this.canvasNode = canvasNode
const canvasDpr = wx.getSystemInfoSync().pixelRatio
const canvasWidth = res[0].width
const canvasHeight = res[0].height
const ctx = canvasNode.getContext('2d')
const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
echarts.setCanvasCreator(() => {
return canvas
})
if (typeof callback === 'function') {
this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
} else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr , echarts)
} else {
this.triggerEvent('init', {
canvas: canvas,
width: canvasWidth,
height: canvasHeight,
dpr: canvasDpr
})
}
})
},
组件改完之后,对应的实现回调处理的方法对应改为:
initChart(canvas, width, height, dpr , echarts){
var vue = this;
vue.chartObj = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr // new
});
canvas.setChart(vue.chartObj);
return vue.chartObj;
},
这样echart初始化就完成了;
938




被折叠的 条评论
为什么被折叠?



