index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import * as echarts from '../../ec-canvas/echarts';
  2. import {
  3. getToDayChartOpt
  4. } from './today-chart-opt.js';
  5. import {
  6. getSourceChartOpt
  7. } from './source-chart-opt.js';
  8. //模拟toDay数据
  9. let toDayAata = {
  10. dayTime: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
  11. visitorNum: [17, 6, 12, 7, 9, 12, 19, 8, 14, 15, 17, 8, 18],
  12. clickNum: [7, 19, 5, 15, 6, 11, 17, 14, 12, 16, 9, 11, 14]
  13. }
  14. Page({
  15. data: {
  16. //今日浏览统计chart type
  17. toDayChartType: 'line', //line | bar
  18. //今日浏览统计 chart
  19. todayEc: {
  20. lazyLoad: true,
  21. isLoaded: false
  22. },
  23. //访客来源 chart
  24. sourceEc: {
  25. lazyLoad: true,
  26. isLoaded: false
  27. }
  28. },
  29. //今日浏览统计 chart切换
  30. switchToDayChartType: function() {
  31. if (this.data.toDayChartType === 'line') {
  32. this.setData({
  33. toDayChartType: 'bar',
  34. })
  35. } else {
  36. this.setData({
  37. toDayChartType: 'line',
  38. })
  39. }
  40. if (this.toDayChart) {
  41. this.toDayChart.setOption(getToDayChartOpt(toDayAata,this.data.toDayChartType))
  42. }
  43. },
  44. //init 今日浏览统计 chart
  45. initToDayChart: function() {
  46. this.toDayComponent.init((canvas, width, height) => {
  47. // 获取组件的 canvas、width、height 后的回调函数
  48. // 在这里初始化图表
  49. const chart = echarts.init(canvas, null, {
  50. width: width,
  51. height: height
  52. });
  53. chart.setOption(getToDayChartOpt(toDayAata, this.data.toDayChartType));
  54. // 将图表实例绑定到 this 上,可以在其他成员函数(如 dispose)中访问
  55. this.toDayChart = chart;
  56. this.setData({
  57. isLoaded: true,
  58. isDisposed: false
  59. });
  60. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  61. return chart;
  62. });
  63. },
  64. //init 访客来源 chart
  65. initSourceChart: function () {
  66. this.sourceComponent.init((canvas, width, height) => {
  67. // 获取组件的 canvas、width、height 后的回调函数
  68. // 在这里初始化图表
  69. const chart = echarts.init(canvas, null, {
  70. width: width,
  71. height: height
  72. });
  73. chart.setOption(getSourceChartOpt([100,500,450]));
  74. // 将图表实例绑定到 this 上,可以在其他成员函数(如 dispose)中访问
  75. this.sourceDayChart = chart;
  76. this.setData({
  77. isLoaded: true,
  78. isDisposed: false
  79. });
  80. // 注意这里一定要返回 chart 实例,否则会影响事件处理等
  81. return chart;
  82. });
  83. },
  84. onReady() {
  85. this.toDayComponent = this.selectComponent('#today-chart');
  86. this.sourceComponent = this.selectComponent('#source-chart');
  87. //模拟异步
  88. setTimeout(()=>{
  89. this.initToDayChart();
  90. this.initSourceChart();
  91. },300)
  92. }
  93. });