当前位置:   article > 正文

微信小程序Canvas绘制曲线图饼图柱状图雷达图蛛网图实现(附源码)_小程序 曲线图

小程序 曲线图

小程序绘制曲线图

在这里插入图片描述

  1. <view class="container">
  2. <canvas canvas-id="lineCanvas" disable-scroll="true" class="canvas" bindtouchstart="touchHandler"></canvas>
  3. <button type="primary" bindtap="updateData">更新数据</button>
  4. </view>
  1. var wxCharts = require('../../../utils/wxcharts.js');
  2. var app = getApp();
  3. var lineChart = null;
  4. Page({
  5. data: {
  6. },
  7. touchHandler: function (e) {
  8. console.log(lineChart.getCurrentDataIndex(e));
  9. lineChart.showToolTip(e, {
  10. // background: '#7cb5ec'
  11. });
  12. },
  13. createSimulationData: function () {
  14. var categories = [];
  15. var data = [];
  16. for (var i = 0; i < 10; i++) {
  17. categories.push('2016-' + (i + 1));
  18. data.push(Math.random()*(20-10)+10);
  19. }
  20. // data[4] = null;
  21. return {
  22. categories: categories,
  23. data: data
  24. }
  25. },
  26. updateData: function () {
  27. var simulationData = this.createSimulationData();
  28. var series = [{
  29. name: '成交量1',
  30. data: simulationData.data,
  31. format: function (val, name) {
  32. return val.toFixed(2) + '万';
  33. }
  34. }];
  35. lineChart.updateData({
  36. categories: simulationData.categories,
  37. series: series
  38. });
  39. },
  40. onLoad: function (e) {
  41. var windowWidth = 320;
  42. try {
  43. var res = wx.getSystemInfoSync();
  44. windowWidth = res.windowWidth;
  45. } catch (e) {
  46. console.error('getSystemInfoSync failed!');
  47. }
  48. var simulationData = this.createSimulationData();
  49. lineChart = new wxCharts({
  50. canvasId: 'lineCanvas',
  51. type: 'line',
  52. categories: simulationData.categories,
  53. animation: true,
  54. background: '#f5f5f5',
  55. series: [{
  56. name: '成交量1',
  57. data: simulationData.data,
  58. format: function (val, name) {
  59. return val.toFixed(2) + '万';
  60. }
  61. }, {
  62. name: '成交量2',
  63. data: [11, 24, 29, 15, null, 21, 32, 23, 45, 21],
  64. format: function (val, name) {
  65. return val.toFixed(2) + '万';
  66. }
  67. }],
  68. xAxis: {
  69. disableGrid: true
  70. },
  71. yAxis: {
  72. title: '成交金额 (万元)',
  73. format: function (val) {
  74. return val.toFixed(2);
  75. },
  76. min: 0
  77. },
  78. width: windowWidth,
  79. height: 200,
  80. dataLabel: false,
  81. dataPointShape: true,
  82. extra: {
  83. lineStyle: 'curve'
  84. }
  85. });
  86. }
  87. });

小程序绘制柱状图

在这里插入图片描述

  1. <view class="container">
  2. <view class="title">
  3. <view wx:if="{{!isMainChartDisplay}}" class="back-btn" bindtap="backToMainChart">返回</view>
  4. {{chartTitle}}
  5. </view>
  6. <canvas canvas-id="columnCanvas" class="canvas" bindtouchstart="touchHandler"></canvas>
  7. <view style="text-align:center">点击数据每一项查看详情</view>
  8. </view>
  1. var wxCharts = require('../../../utils/wxcharts.js');
  2. var app = getApp();
  3. var columnChart = null;
  4. var chartData = {
  5. main: {
  6. title: '总成交量',
  7. data: [15, 20, 45, 37],
  8. categories: ['2012', '2013', '2014', '2015']
  9. },
  10. sub: [{
  11. title: '2012年度成交量',
  12. data: [70, 40, 65, 100, 34, 18],
  13. categories: ['1', '2', '3', '4', '5', '6']
  14. }, {
  15. title: '2013年度成交量',
  16. data: [55, 30, 45, 36, 56, 13],
  17. categories: ['1', '2', '3', '4', '5', '6']
  18. }, {
  19. title: '2014年度成交量',
  20. data: [76, 45, 32, 74, 54, 35],
  21. categories: ['1', '2', '3', '4', '5', '6']
  22. }, {
  23. title: '2015年度成交量',
  24. data: [76, 54, 23, 12, 45, 65],
  25. categories: ['1', '2', '3', '4', '5', '6']
  26. }]
  27. };
  28. Page({
  29. data: {
  30. chartTitle: '总成交量',
  31. isMainChartDisplay: true
  32. },
  33. backToMainChart: function () {
  34. this.setData({
  35. chartTitle: chartData.main.title,
  36. isMainChartDisplay: true
  37. });
  38. columnChart.updateData({
  39. categories: chartData.main.categories,
  40. series: [{
  41. name: '成交量',
  42. data: chartData.main.data,
  43. format: function (val, name) {
  44. return val.toFixed(2) + '万';
  45. }
  46. }]
  47. });
  48. },
  49. touchHandler: function (e) {
  50. var index = columnChart.getCurrentDataIndex(e);
  51. if (index > -1 && index < chartData.sub.length && this.data.isMainChartDisplay) {
  52. this.setData({
  53. chartTitle: chartData.sub[index].title,
  54. isMainChartDisplay: false
  55. });
  56. columnChart.updateData({
  57. categories: chartData.sub[index].categories,
  58. series: [{
  59. name: '成交量',
  60. data: chartData.sub[index].data,
  61. format: function (val, name) {
  62. return val.toFixed(2) + '万';
  63. }
  64. }]
  65. });
  66. }
  67. },
  68. onReady: function (e) {
  69. var windowWidth = 320;
  70. try {
  71. var res = wx.getSystemInfoSync();
  72. windowWidth = res.windowWidth;
  73. } catch (e) {
  74. console.error('getSystemInfoSync failed!');
  75. }
  76. columnChart = new wxCharts({
  77. canvasId: 'columnCanvas',
  78. type: 'column',
  79. animation: true,
  80. categories: chartData.main.categories,
  81. series: [{
  82. name: '成交量',
  83. data: chartData.main.data,
  84. format: function (val, name) {
  85. return val.toFixed(2) + '万';
  86. }
  87. }],
  88. yAxis: {
  89. format: function (val) {
  90. return val + '万';
  91. },
  92. title: 'hello',
  93. min: 0
  94. },
  95. xAxis: {
  96. disableGrid: false,
  97. type: 'calibration'
  98. },
  99. extra: {
  100. column: {
  101. width: 15
  102. }
  103. },
  104. width: windowWidth,
  105. height: 200,
  106. });
  107. }
  108. });

 小程序绘制饼图

 在这里插入图片描述

  1. <view class="container">
  2. <canvas canvas-id="pieCanvas" class="canvas" style="height:300px" bindtouchstart="touchHandler"></canvas>
  3. </view>

  1. var wxCharts = require('../../../utils/wxcharts.js');
  2. var app = getApp();
  3. var pieChart = null;
  4. Page({
  5. data: {
  6. },
  7. touchHandler: function (e) {
  8. console.log(pieChart.getCurrentDataIndex(e));
  9. },
  10. onLoad: function (e) {
  11. var windowWidth = 320;
  12. try {
  13. var res = wx.getSystemInfoSync();
  14. windowWidth = res.windowWidth;
  15. } catch (e) {
  16. console.error('getSystemInfoSync failed!');
  17. }
  18. pieChart = new wxCharts({
  19. animation: true,
  20. canvasId: 'pieCanvas',
  21. type: 'pie',
  22. series: [{
  23. name: '成交量1',
  24. data: 15,
  25. }, {
  26. name: '成交量2',
  27. data: 35,
  28. }, {
  29. name: '成交量3',
  30. data: 78,
  31. }, {
  32. name: '成交量4',
  33. data: 63,
  34. }, {
  35. name: '成交量2',
  36. data: 35,
  37. }, {
  38. name: '成交量3',
  39. data: 78,
  40. }, {
  41. name: '成交量4',
  42. data: 63,
  43. }, {
  44. name: '成交量2',
  45. data: 35,
  46. }, {
  47. name: '成交量3',
  48. data: 78,
  49. }, {
  50. name: '成交量3',
  51. data: 78,
  52. }],
  53. width: windowWidth,
  54. height: 300,
  55. dataLabel: true,
  56. });
  57. }
  58. });

小程序绘制环形图

在这里插入图片描述

  1. <view class="container">
  2. <canvas canvas-id="ringCanvas" class="canvas" bindtouchstart="touchHandler"></canvas>
  3. <view>调用stopAnimation终止动画,并监听渲染完成事件</view>
  4. <button type="primary" bindtap="updateData" style="margin-top:30rpx">更新title</button>
  5. </view>

  1. var wxCharts = require('../../../utils/wxcharts.js');
  2. var app = getApp();
  3. var ringChart = null;
  4. Page({
  5. data: {
  6. },
  7. touchHandler: function (e) {
  8. console.log(ringChart.getCurrentDataIndex(e));
  9. },
  10. updateData: function () {
  11. ringChart.updateData({
  12. title: {
  13. name: '80%'
  14. },
  15. subtitle: {
  16. color: '#ff0000'
  17. }
  18. });
  19. },
  20. onReady: function (e) {
  21. var windowWidth = 320;
  22. try {
  23. var res = wx.getSystemInfoSync();
  24. windowWidth = res.windowWidth;
  25. } catch (e) {
  26. console.error('getSystemInfoSync failed!');
  27. }
  28. ringChart = new wxCharts({
  29. animation: true,
  30. canvasId: 'ringCanvas',
  31. type: 'ring',
  32. extra: {
  33. ringWidth: 25
  34. },
  35. title: {
  36. name: '70%',
  37. color: '#7cb5ec',
  38. fontSize: 25
  39. },
  40. subtitle: {
  41. name: '收益率',
  42. color: '#666666',
  43. fontSize: 15
  44. },
  45. series: [{
  46. name: '成交量1',
  47. data: 15,
  48. stroke: false
  49. }, {
  50. name: '成交量2',
  51. data: 35,
  52. stroke: false
  53. }, {
  54. name: '成交量3',
  55. data: 78,
  56. stroke: false
  57. }, {
  58. name: '成交量4',
  59. data: 63,
  60. stroke: false
  61. }],
  62. disablePieStroke: true,
  63. width: windowWidth,
  64. height: 200,
  65. dataLabel: false,
  66. legend: false,
  67. padding: 0
  68. });
  69. ringChart.addEventListener('renderComplete', () => {
  70. console.log('renderComplete');
  71. });
  72. setTimeout(() => {
  73. ringChart.stopAnimation();
  74. }, 500);
  75. }
  76. });

小程序绘制曲线图选区

在这里插入图片描述

  1. <view class="container">
  2. <canvas canvas-id="areaCanvas" class="canvas" bindtouchstart="touchHandler"></canvas>
  3. </view>

  1. var wxCharts = require('../../../utils/wxcharts.js');
  2. var app = getApp();
  3. var areaChart = null;
  4. Page({
  5. data: {
  6. },
  7. touchHandler: function (e) {
  8. console.log(areaChart.getCurrentDataIndex(e));
  9. areaChart.showToolTip(e);
  10. },
  11. onLoad: function (e) {
  12. var windowWidth = 320;
  13. try {
  14. var res = wx.getSystemInfoSync();
  15. windowWidth = res.windowWidth;
  16. } catch (e) {
  17. console.error('getSystemInfoSync failed!');
  18. }
  19. areaChart = new wxCharts({
  20. canvasId: 'areaCanvas',
  21. type: 'area',
  22. categories: ['1', '2', '3', '4', '5', '6'],
  23. animation: true,
  24. series: [{
  25. name: '成交量1',
  26. data: [32, 45, null, 56, 33, 34],
  27. format: function (val) {
  28. return val.toFixed(2) + '万';
  29. }
  30. }],
  31. yAxis: {
  32. title: '成交金额 (万元)',
  33. format: function (val) {
  34. return val.toFixed(2);
  35. },
  36. min: 0,
  37. fontColor: '#8085e9',
  38. gridColor: '#8085e9',
  39. titleFontColor: '#f7a35c'
  40. },
  41. xAxis: {
  42. fontColor: '#7cb5ec',
  43. gridColor: '#7cb5ec'
  44. },
  45. extra: {
  46. legendTextColor: '#cb2431'
  47. },
  48. width: windowWidth,
  49. height: 200
  50. });
  51. }
  52. });

小程序绘制蛛网图/雷达图

在这里插入图片描述

  1. <view class="container">
  2. <canvas canvas-id="radarCanvas" class="canvas" bindtouchstart="touchHandler"></canvas>
  3. </view>

  1. var wxCharts = require('../../../utils/wxcharts.js');
  2. var app = getApp();
  3. var radarChart = null;
  4. Page({
  5. data: {
  6. },
  7. touchHandler: function (e) {
  8. console.log(radarChart.getCurrentDataIndex(e));
  9. },
  10. onReady: function (e) {
  11. var windowWidth = 320;
  12. try {
  13. var res = wx.getSystemInfoSync();
  14. windowWidth = res.windowWidth;
  15. } catch (e) {
  16. console.error('getSystemInfoSync failed!');
  17. }
  18. radarChart = new wxCharts({
  19. canvasId: 'radarCanvas',
  20. type: 'radar',
  21. categories: ['1', '2', '3', '4', '5', '6'],
  22. series: [{
  23. name: '成交量1',
  24. data: [90, 110, 125, 95, 87, 122]
  25. }],
  26. width: windowWidth,
  27. height: 200,
  28. extra: {
  29. radar: {
  30. max: 150
  31. }
  32. }
  33. });
  34. }
  35. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/526518
推荐阅读
相关标签
  

闽ICP备14008679号