lineChart.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // 线图
  2. import * as echarts from '../../ec-canvas/echarts';
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. canvasId: {
  9. type: String,
  10. value: "mychart-dom-bar"
  11. },
  12. id: {
  13. type: String,
  14. value: "mychart-bar"
  15. },
  16. type: {
  17. type: String,
  18. value: "bar"
  19. },
  20. select: {
  21. type: String,
  22. value: ""
  23. },
  24. btnList: {
  25. type: Array,
  26. value: [],
  27. },
  28. color: {
  29. type: String,
  30. value: ""
  31. },
  32. isShow: {
  33. type: Boolean,
  34. value: false,
  35. observer: function (n) {
  36. const select = this.data.select === '6m' || this.data.select === '24h';
  37. if (!select) return
  38. console.log("需要更新");
  39. if (n) this.triggerEvent("lineChenge", this.data.select);
  40. }
  41. },
  42. list: {
  43. type: Array,
  44. value: [],
  45. observer: function (n, o) {
  46. if (!n || !n.length) return;
  47. if (this.data.select !== '24h' && this.data.select !== '6m' && this.setInterval) clearInterval(this.setInterval)
  48. if (this.data.select !== '24h' && this.data.select !== '6m' && this.setTimeout) clearInterval(this.setTimeout)
  49. let xData = [], yData = [], color = this.data.color || "24,144,255";
  50. yData = {
  51. type: this.data.type,
  52. smooth: 0.6,
  53. symbolSize: 1,
  54. itemStyle: {
  55. color: "rgba(" + color + ", 1)"
  56. },
  57. lineStyle: {
  58. width: 1
  59. },
  60. areaStyle: {
  61. color: new echarts.graphic.LinearGradient(
  62. 0, 0, 0, 1,
  63. [
  64. { offset: 0, color: 'rgba(' + color + ', .8)' },
  65. { offset: 1, color: 'rgba(' + color + ', 0)' },
  66. ]
  67. )
  68. },
  69. data: []
  70. }
  71. n.map((v, i) => {
  72. yData.data.push(v[this.data.yType]);
  73. xData.push(v[this.data.xType])
  74. })
  75. if (this.chart) {
  76. const option = this.chart.getOption();
  77. option.series = yData;
  78. option.xAxis[0].data = xData;
  79. option.series = yData;
  80. this.chart.setOption(option);
  81. this.setData({
  82. xData,
  83. yData,
  84. show: true
  85. })
  86. } else {
  87. this.setData({
  88. ec: {
  89. onInit: this.initChart.bind(this)
  90. },
  91. xData,
  92. yData,
  93. show: true
  94. })
  95. }
  96. }
  97. },
  98. yType: {
  99. type: String,
  100. value: ""
  101. },
  102. xType: {
  103. type: String,
  104. value: ""
  105. }
  106. },
  107. chart: undefined,
  108. setInterval: undefined,
  109. setTimeout: undefined,
  110. ready() {
  111. wx.setStorageSync('nextTime', 0)
  112. },
  113. // 销毁组件
  114. detached: function () {
  115. this.chart && this.chart.dispose && this.chart.dispose();
  116. this.setInterval && clearInterval(this.setInterval);
  117. this.setTimeout && clearInterval(this.setTimeout);
  118. },
  119. /**
  120. * 组件的初始数据
  121. */
  122. data: {
  123. show: false,
  124. ec: {
  125. onInit: undefined
  126. },
  127. xData: [],
  128. yData: [],
  129. },
  130. /**
  131. * 组件的方法列表
  132. */
  133. methods: {
  134. initChart: function (canvas, width, height, dpr) {
  135. this.chart && this.chart.dispose && this.chart.dispose();
  136. this.chart = echarts.init(canvas, null, {
  137. width: width,
  138. height: height,
  139. devicePixelRatio: dpr // 像素
  140. });
  141. canvas.setChart(this.chart);
  142. var option = {
  143. animation: false,
  144. tooltip: {
  145. show: true,
  146. textStyle: {
  147. color: "#fff"
  148. },
  149. backgroundColor: 'rgba(0,0,0,.5)',
  150. trigger: 'axis',
  151. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  152. type: 'line', // 默认为直线,可选为:'line' | 'shadow'
  153. shadowStyle: {
  154. color: "rgba(150,150,150,0.2)"
  155. },
  156. },
  157. formatter: "{b}\n{c}",
  158. },
  159. color: ['#1890ff'],
  160. grid: {
  161. top: "10px",
  162. left: '3%',
  163. right: '4%',
  164. bottom: '3%',
  165. containLabel: true
  166. },
  167. xAxis: {
  168. interval: 0,
  169. type: 'category',
  170. boundaryGap: false,
  171. data: this.data.xData,
  172. splitLine: { show: false },
  173. axisTick: {
  174. show: false
  175. },
  176. axisLine: {
  177. show: false,
  178. lineStyle: {
  179. color: '#bfcbd9',
  180. }
  181. }
  182. },
  183. yAxis: {
  184. type: 'value',
  185. splitLine: { show: true, lineStyle: { type: "dotted", color: '#4e5358' } },
  186. axisLine: {
  187. show: false,
  188. lineStyle: {
  189. color: '#bfcbd9',
  190. }
  191. },
  192. axisLabel: {
  193. formatter: function (val, index) {
  194. let out = val;
  195. if (val >= 1000 && val < 10000) {
  196. out = (val / 1000).toFixed(2) - 0 + "千"
  197. } else if (val >= 10000 && val < 100000000) {
  198. out = (val / 10000).toFixed(0) - 0 + "万"
  199. } else if (val >= 100000000) {
  200. out = (val / 100000000).toFixed(2) - 0 + "亿"
  201. }
  202. return out
  203. }
  204. }
  205. },
  206. series: this.data.yData
  207. };
  208. this.chart.setOption(option);
  209. return this.chart;
  210. },
  211. changeselect: function (e) {
  212. let type = e.currentTarget.dataset.type;
  213. if (type === this.data.select) return;
  214. this.setInterval && clearInterval(this.setInterval);
  215. this.setTimeout && clearInterval(this.setTimeout);
  216. if (type === '24h' || type === '6m') this.countFun(type);
  217. this.triggerEvent("lineChenge", type)
  218. },
  219. countFun(type) {
  220. let time = new Date();
  221. let min = time.getMinutes();
  222. let remainder = min % 10;
  223. if (remainder === 1) {
  224. this.setInterval = setInterval(() => {
  225. console.log("定时")
  226. this.triggerEvent("lineChenge", type);
  227. }, 600000)
  228. } else {
  229. this.setTimeout = setTimeout(() => {
  230. clearTimeout(this.setTimeout);
  231. this.triggerEvent("lineChenge", type);
  232. this.setInterval = setInterval(() => {
  233. console.log("定时")
  234. this.triggerEvent("lineChenge", type);
  235. }, 600000)
  236. }, (11 - remainder) * 1000 * 60);
  237. }
  238. }
  239. }
  240. })