当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 微信小程序实现折线图的示例代码

微信小程序实现折线图的示例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论
插件地址: 微信小程序折线图效果: 首先需要引入一个折线图的插件: xxx.xml 的代码: <canvas style="width:

插件地址:

微信小程序折线图效果:

首先需要引入一个折线图的插件:

xxx.xml 的代码:

<canvas
 style="width: 400px; height: 500px;"
 canvas-id="yueele"
 binderror="canvasiderrorcallback"
></canvas>

注意 canvas-id="yueele" 要与 js中的   canvasid: 'yueele' 一样。才能显示图。

然后在xxx.js中配置:

1.在顶部写入:

2.写一个独立的方法(或者直接写在onload里面):

我这里写的方法名是  getmothelectro  ,我后面试在onload里面调用了的。

getmothelectro:function(){
  var windowwidth = 320;
  try {
   var res = wx.getsysteminfosync();
   windowwidth = res.windowwidth;
  } catch (e) {
   console.error('getsysteminfosync failed!');
  }
  yuelinechart = new wxcharts({ //当月用电折线图配置
   canvasid: 'yueele',
   type: 'line',
   categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'], //categories x轴
   animation: true,
   // background: '#f5f5f5',
   series: [{
    name: '总用电量',
    //data: yuesimulationdata.data,
    data: [1, 6, 9, 1, 0, 2, 9, 2, 8, 6, 0, 9, 8, 0, 3, 7, 3, 9, 3, 8, 9, 5, 4, 1, 5, 8, 2, 4, 9, 8, 7],
    format: function (val, name) {
     return val.tofixed(2) + 'kwh';
    }
   }, {
    name: '电池供电量',
    data: [0, 6, 2, 2, 7, 6, 2, 5, 8, 1, 8, 4, 0, 9, 7, 2, 5, 2, 8, 2, 5, 2, 9, 4, 4, 9, 8, 5, 5, 5, 6],
    format: function (val, name) {
     return val.tofixed(2) + 'kwh';
    }
   },
   {
    name: '光伏供电量',
    data: [6, 4, 9, 7, 1, 4, 5, 1, 1, 8, 8, 6, 6, 2, 2, 2, 0, 5, 5, 8, 8, 8, 8, 9, 0, 4, 6, 9, 2, 1, 6],
    format: function (val, name) {
     return val.tofixed(2) + 'kwh';
    }
   },
   {
    name: '市电供电量',
    data: [0, 4, 3, 3, 1, 7, 7, 7, 9, 9, 3, 3, 0, 0, 5, 6, 0, 4, 1, 2, 0, 1, 3, 9, 2, 5, 1, 8, 3, 4, 2],
    format: function (val, name) {
     return val.tofixed(2) + 'kwh';
    }
   }],
   xaxis: {
    disablegrid: true
   },
   yaxis: {
    title: '当月用电(kwh)',
    format: function (val) {
     return val.tofixed(2);
    },
    max: 20,
    min: 0
   },
   width: windowwidth,
   height: 200,
   datalabel: false,
   datapointshape: true,
   extra: {
    linestyle: 'curve'
   }
  });
 }

3.然后在onload里面在调用一次:

4.在增加一个触摸显示功能:

yuetouchhandler: function (e) { //当月用电触摸显示
  console.log(daylinechart.getcurrentdataindex(e));
  yuelinechart.showtooltip(e, { //showtooltip图表中展示数据详细内容
   background: '#7cb5ec',
   format: function (item, category) {
    return category + '日 ' + item.name + ':' + item.data
   }
  });
 },

这样就ok了!

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网