微信小程序的wx-charts插件

微信小程序的wx-charts插件

还有就是可以使用一些小程序的插件,比如wx-charts.

先来看一下网上对这个插件的评价:

微信小程序的wx-charts插件

目前在github上有微信小程序的wx-charts插件1804颗星,使用的比较广泛。

github地址:https://github.com/xiaolin3303/wx-charts.git;

支持图标类型

  • 饼图 pie
  • 圆环图 ring
  • 线图 line
  • 柱状图 column
  • 区域图 area
  • 雷达图 radar

使用方法

直接引入编译好的dist里面的js文件(二选一)

微信小程序的wx-charts插件

微信小程序的wx-charts插件

 

 然后在需要使用的页面的js当中使用require引入即可:

?

1

let Charts = require('./../../utils/wxcharts.js');

  .wxml中定义

<canvas canvas-id="canvas1"></canvas>

<canvas canvas-id="canvas2"></canvas>

<canvas canvas-id="canvas3"></canvas>

<canvas canvas-id="canvas4"></canvas>

<canvas canvas-id="canvas5"></canvas>

<canvas canvas-id="canvas6"></canvas>

  canvas-id与js当中的new Charts选项当中的canvasId必须要一致才行;

参数说明:

?

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

opts                         Object

opts.canvasId                String required                    微信小程序canvas-id

opts.width                   Number required                canvas宽度,单位为px

opts.height                  Number required                canvas高度,单位为px

opts.title                   Object           (only for ring chart)

opts.title.name              String           标题内容

opts.title.fontSize          Number            标题字体大小(可选,单位为px)

opts.title.color             String           标题颜色(可选)

opts.subtitle                Object         (only for ring chart)

opts.subtitle.name           String           副标题内容

opts.subtitle.fontSize       Number          副标题字体大小(可选,单位为px)

opts.subtitle.color          String          副标题颜色(可选)

opts.animation               Boolean default true         是否动画展示

opts.legend                  Boolen default true       是否显示图表下方各类别的标识

opts.type                    String required 图表类型,可选值为pie, line, column, area,radar,ring

opts.categories              Array required       (饼图、圆环图不需要) 数据类别分类

opts.dataLabel               Boolean default true     是否在图表中显示数据内容值

opts.dataPointShape          Boolean default true   是否在图表中显示数据点图形标识

opts.xAxis                   Object       X轴配置

opts.xAxis.disableGrid       Boolean default false      不绘制X轴网格(适用于折线图,柱形图,区域图)

opts.yAxis                   Object    Y轴配置

opts.yAxis.format            Function           自定义Y轴文案显示

opts.yAxis.min               Number        Y轴起始值

opts.yAxis.max               Number           Y轴终止值

opts.yAxis.title             String       Y轴title

?

1

opts.yAxis.titleFontColor             String       Y轴title的文字的颜色

?

1

opts.yAxis.fontColor             String       Y轴文字的颜色

?

1

opts.yAxis.gridColor             String       Y轴格子的颜色

?

1

2

opts.yAxis.disabled          Boolean default false        不绘制Y轴

opts.series                  Array required        数据列表

  数据列表series的参数

?

1

2

3

4

5

dataItem                      Object

dataItem.data                 Array required (饼图、圆环图为Number) 数据

dataItem.color                String 例如#7cb5ec 不传入则使用系统默认配色方案

dataItem.name                 String 数据名称

dateItem.format               Function 自定义显示数据内容

  高清显示

设置canvas的尺寸为2倍大小,然后缩小到50%,建议都进行这样的设置,图表本身绘制时是按照高清显示配置的,不然整体效果会偏大(一般以iPhone6为标准进行设计)

?

1

2

3

4

5

6

/* 例如设计图尺寸为320 x 300 */

.canvas {

    width: 640px;

    height: 600px;

    transform: scale(0.5)

}

  例子:

  pie(饼图)

?

1

2

3

4

5

6

7

8

new Charts({

     canvasId: 'canvas1',

     type: 'pie',

     series: [{ name: '一班', data: 50 }, { name: '二班', data: 30 }, { name: '三班', data: 20 }, { name: '四班', data: 18 }, { name: '五班', data: 8 }],

     width: 640,

     height: 400,

     dataLabel: true,

   });

  微信小程序的wx-charts插件

 

 线图(circle)

?

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

new Charts({

canvasId: 'canvas2',

dataPointShape: "circle",

type: 'line',

extra: {

lineStyle: 'curve' //线条的形状(弧形)

},

categories: ['2012', '2013', '2014', '2015', '2016', '2017'],

series: [{

name: '成交量1',

data: [0.15, null, 0.45, 0.37, 0.4, 0.8],//设置某一个值为null会出现断层

format: function (val) {

return val.toFixed(2) + '万';

}

}, {

name: '成交量2',

data: [0.30, 0.37, 0.65, 0.78, 0.69, 0.94],

format: function (val) {

return val.toFixed(2) + '万';

}

}],

yAxis: {

title: '成交金额 (万元)',

format: function (val) {

return val.toFixed(2);

},

fontColor: "red",

titleFontColor: "red",

min: 0,<br>gridColor:"red"

?

1

2

3

4

5

},

width: 840,

height: 600,

dataLabel: true

});