Chart.js 6.9 KB

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