Chart.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. btnList: {
  21. type: Array,
  22. value: [],
  23. observer: function (n, o) {
  24. if (!n || n === o) return
  25. this.setData({
  26. select: n[0].type
  27. })
  28. }
  29. },
  30. list: {
  31. type: Array,
  32. value: [],
  33. observer: function (n, o) {
  34. if (!n || !n.length) return
  35. let xData = [], yData = [];
  36. let type = this.data.select || this.data.yType;
  37. n.map((v, i) => {
  38. yData[i] = {
  39. type: this.data.type,
  40. barMaxWidth: 30,
  41. label: {
  42. show: true,
  43. shadowColor: 'transparent',
  44. position: 'top',
  45. color: "#fff",
  46. formatter: function (d) {
  47. let v = d.value;
  48. if (isNaN(v)) {
  49. return 0
  50. } else if (v >= 10000 && v < 100000) {
  51. return (v / 10000).toFixed(2) - 0 + '万'
  52. } else if (v >= 100000 && v < 100000000) {
  53. return (v / 10000).toFixed(0) - 0 + '万'
  54. } else if (v >= 100000000) {
  55. return (v / 100000000).toFixed(2) - 0 + '亿'
  56. }
  57. return v
  58. }
  59. },
  60. data: []
  61. }
  62. if (v.length) {
  63. v = v.sort((a, b) => {
  64. return b[type] - a[type]
  65. })
  66. let sLi = JSON.parse(JSON.stringify(v));
  67. if (v.length > 10) sLi = sLi.splice(0, 10);
  68. sLi.map(val => {
  69. i === 0 && xData.push(val[this.data.xType]);
  70. yData[i].data.push(val[type]);
  71. })
  72. }
  73. })
  74. if (this.chart) {
  75. const option = this.chart.getOption();
  76. option.series = yData;
  77. option.xAxis[0].data = xData;
  78. option.series = yData;
  79. this.chart.clear();
  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. // 销毁组件
  109. detached: function () {
  110. this.chart && this.chart.dispose && this.chart.dispose();
  111. },
  112. /**
  113. * 组件的初始数据
  114. */
  115. data: {
  116. show: false,
  117. ec: {
  118. onInit: undefined
  119. },
  120. xData: [],
  121. yData: [],
  122. select: ""
  123. },
  124. /**
  125. * 组件的方法列表
  126. */
  127. methods: {
  128. initChart: function (canvas, width, height, dpr) {
  129. this.chart && this.chart.dispose && this.chart.dispose();
  130. this.chart = echarts.init(canvas, null, {
  131. width: width,
  132. height: height,
  133. devicePixelRatio: dpr // 像素
  134. });
  135. canvas.setChart(this.chart);
  136. var option = {
  137. animation: false,
  138. tooltip: {
  139. show: false,
  140. textStyle: {
  141. color: "#fff"
  142. },
  143. trigger: 'axis',
  144. backgroundColor: 'rgba(0,0,0,.5)',
  145. axisPointer: { // 坐标轴指示器,坐标轴触发有效
  146. type: 'shadow', // 默认为直线,可选为:'line' | 'shadow'
  147. shadowStyle: {
  148. color: "rgba(150,150,150,0.2)"
  149. },
  150. },
  151. formatter: "{b}\n{c}",
  152. },
  153. color: ['#1890ff'],
  154. grid: {
  155. top: "20px",
  156. left: '3%',
  157. right: '4%',
  158. bottom: '3%',
  159. containLabel: true
  160. },
  161. xAxis: {
  162. type: 'category',
  163. splitLine: { show: false },
  164. data: this.data.xData,
  165. axisTick: {
  166. show: false
  167. },
  168. axisLine: {
  169. show: false,
  170. lineStyle: {
  171. color: "#bfcbd9"
  172. }
  173. },
  174. axisLabel: {
  175. interval: 0,
  176. formatter: function (v, index) {
  177. const li = v.split("");
  178. v = li.map((nameString, i) => {
  179. i !== 0 && (i + 1) % 2 === 0 && (nameString = nameString + "\n");
  180. return nameString
  181. }).join("");
  182. return v
  183. }
  184. }
  185. },
  186. yAxis: {
  187. type: 'value',
  188. splitLine: { show: true, lineStyle: { type: "dotted", color: '#4e5358' } },
  189. axisLine: {
  190. show: false,
  191. lineStyle: {
  192. type: "dotted",
  193. color: "#bfcbd9",
  194. }
  195. },
  196. axisLabel: {
  197. formatter: function (val, index) {
  198. let out = val;
  199. if (val >= 1000 && val < 10000) {
  200. out = (val / 1000).toFixed(2) - 0 + "千"
  201. } else if (val >= 10000 && val < 100000) {
  202. out = (val / 10000).toFixed(2) - 0 + "万"
  203. } else if (val >= 100000 && val < 100000000) {
  204. return (val / 10000).toFixed(0) - 0 + '万'
  205. } else if (val >= 100000000) {
  206. out = (val / 100000000).toFixed(2) - 0 + "亿"
  207. }
  208. return out
  209. }
  210. }
  211. },
  212. series: this.data.yData
  213. };
  214. // this.chart.on("finished", () => {
  215. // console.log("绘图结束");
  216. // })
  217. this.chart.setOption(option);
  218. return this.chart;
  219. },
  220. changeselect: function (e) {
  221. let type = e.currentTarget.dataset.type;
  222. if (type === this.data.select) return;
  223. let yData = [], xData = [];
  224. this.data.list.map((v, i) => {
  225. yData[i] = {
  226. type: this.data.type,
  227. data: []
  228. }
  229. if (v.length) {
  230. v = v.sort((a, b) => {
  231. return b[type] - a[type]
  232. })
  233. let sLi = JSON.parse(JSON.stringify(v));
  234. if (v.length > 10) sLi = sLi.splice(0, 10);
  235. sLi.map(val => {
  236. yData[i].data.push(val[type]);
  237. i === 0 && xData.push(val[this.data.xType]);
  238. })
  239. }
  240. })
  241. this.setData({
  242. select: type,
  243. yData,
  244. xData
  245. }, () => {
  246. let option = this.chart.getOption();
  247. option.series = yData;
  248. option.xAxis[0].data = xData;
  249. this.chart.setOption(option);
  250. })
  251. },
  252. }
  253. })