Chart.js 7.0 KB

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