table-line-chart.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "normal-line-chart",
  10. componentName: "normal-line-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "280px",
  15. },
  16. height: {
  17. type: String,
  18. default: "24px",
  19. },
  20. // 数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. text: "1",
  26. value: 3,
  27. },
  28. {
  29. text: "2",
  30. value: 2,
  31. },
  32. {
  33. text: "3",
  34. value: 1,
  35. },
  36. ],
  37. },
  38. },
  39. data() {
  40. return {
  41. id: "",
  42. chart: null,
  43. };
  44. },
  45. computed: {
  46. xdata() {
  47. return this.list.map((t,index) => {
  48. return t.text || index;
  49. });
  50. },
  51. values() {
  52. return this.list.map((t) => {
  53. return t.value || t;
  54. });
  55. },
  56. },
  57. methods: {
  58. hexToRgba(hex, opacity) {
  59. let rgbaColor = "";
  60. let reg = /^#[\da-f]{6}$/i;
  61. if (reg.test(hex)) {
  62. rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt("0x" + hex.slice(3, 5))},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
  63. }
  64. return rgbaColor;
  65. },
  66. initChart() {
  67. let chart = echarts.init(this.$el);
  68. let color = [partten.getColor("green")];
  69. let option = {
  70. color: color,
  71. grid: {
  72. top: 4,
  73. bottom: 0,
  74. left: 8,
  75. right: 8,
  76. },
  77. xAxis: [
  78. {
  79. type: "category",
  80. boundaryGap: false,
  81. axisLine: {
  82. show: true,
  83. lineStyle: {
  84. color: partten.getColor("gray"),
  85. },
  86. },
  87. data: this.xdata,
  88. },
  89. ],
  90. yAxis: [
  91. {
  92. type: "value",
  93. // 分割线
  94. splitLine: {
  95. show: false,
  96. },
  97. axisLine: {
  98. show: false,
  99. },
  100. axisTick: {
  101. show: false,
  102. },
  103. },
  104. ],
  105. series: [
  106. {
  107. type: "line",
  108. lineStyle: {
  109. normal: {
  110. color: color[0],
  111. },
  112. },
  113. animation: false,
  114. symbol: "circle", //数据交叉点样式
  115. areaStyle: {
  116. normal: {
  117. color: new echarts.graphic.LinearGradient(
  118. 0,
  119. 0,
  120. 0,
  121. 1,
  122. [
  123. {
  124. offset: 0,
  125. color: this.hexToRgba(color[0], 0.3),
  126. },
  127. {
  128. offset: 1,
  129. color: this.hexToRgba(color[0], 0.1),
  130. },
  131. ],
  132. false
  133. ),
  134. shadowColor: this.hexToRgba(color[0], 0.1),
  135. shadowBlur: 10,
  136. },
  137. },
  138. data: this.values,
  139. },
  140. ],
  141. };
  142. chart.clear();
  143. chart.setOption(option);
  144. this.resize = function() {
  145. chart.resize();
  146. };
  147. window.addEventListener("resize", this.resize);
  148. },
  149. },
  150. created() {
  151. this.id = "pie-chart-" + util.newGUID();
  152. },
  153. mounted() {
  154. this.$nextTick(() => {
  155. setTimeout(() => {
  156. this.$el.style.width = this.width;
  157. this.$el.style.height = this.height;
  158. this.initChart();
  159. }, 1000);
  160. });
  161. },
  162. updated() {
  163. this.$nextTick(() => {
  164. this.initChart();
  165. });
  166. },
  167. unmounted() {
  168. window.removeEventListener("resize", this.resize);
  169. },
  170. };
  171. </script>
  172. <style lang="less">
  173. .chart {
  174. width: 100%;
  175. height: 100%;
  176. display: inline-block;
  177. }
  178. </style>