lineChart.js 6.1 KB

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