lineChartCarsim.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="linechart" :id="id" style="width: 100%; height: 100%"></div>
  3. </template>
  4. <script>
  5. import tool from "./common/tool.js";
  6. export default {
  7. name: "lineChartCarsim", // carsim-折线图
  8. mixins: [tool],
  9. props: {
  10. id: {
  11. default: "lineChartCarsim",
  12. type: String,
  13. },
  14. dataList: {
  15. default: function () {
  16. return [];
  17. },
  18. type: Array,
  19. },
  20. columns: {
  21. default: function () {
  22. return [];
  23. },
  24. type: Array,
  25. },
  26. xName: {
  27. default: "",
  28. type: String,
  29. },
  30. yName: {
  31. default: "",
  32. type: String,
  33. },
  34. },
  35. data() {
  36. return {
  37. charts: "",
  38. };
  39. },
  40. watch: {
  41. dataList: {
  42. handler(val) {
  43. if (val.length > 0) {
  44. this.initStateList();
  45. }
  46. },
  47. deep: true,
  48. },
  49. },
  50. methods: {
  51. initStateList() {
  52. let array = this.dataList;
  53. // console.log(array);
  54. for (let index = 0; index < array.length; index++) {
  55. const element = array[index];
  56. let flag = Object.values(element).some((i) => i === "");
  57. if (flag) {
  58. this.charts.clear();
  59. this.charts.setOption(this.option1);
  60. return;
  61. }
  62. }
  63. // if (!this.isSorted(array.map((i) => i.x))) {
  64. // this.$message.error("X轴的坐标应逐渐增大");
  65. // this.charts.clear();
  66. // this.charts.setOption(this.option1);
  67. // return;
  68. // }
  69. let props = [];
  70. this.columns.forEach((i) => {
  71. props.push(i.prop); // ['x', '1.0', '2.0', '3.0', '4.0']
  72. });
  73. // let xData = [];
  74. // let yData = [];
  75. let series = [];
  76. props.forEach((j, index) => {
  77. let a = [];
  78. if (j === "x") return;
  79. array.forEach((i) => {
  80. a.push([i.x, i[j]]);
  81. });
  82. series.push({
  83. data: a,
  84. type: "line",
  85. name: j,
  86. smooth: false,
  87. });
  88. });
  89. /* yData.forEach((i, index) => {
  90. series.push({
  91. name: props[index + 1],
  92. data: i,
  93. type: "line",
  94. smooth: false,
  95. symbol: "circle",
  96. symbolSize: 6,
  97. lineStyle: {
  98. normal: {
  99. width: 1.5,
  100. // color: this.themeColor,
  101. },
  102. },
  103. itemStyle: {
  104. normal: {
  105. // color: this.themeColor,
  106. },
  107. },
  108. });
  109. }); */
  110. let option = {
  111. title: {
  112. text: "暂无数据",
  113. show: series.length === 0,
  114. left: "center",
  115. top: "center",
  116. },
  117. tooltip: {
  118. //提示框,可以在全局也可以在
  119. trigger: "item", //提示框的样式
  120. formatter: "{b}: {c}",
  121. color: "#000", //提示框的背景色
  122. textStyle: {
  123. //提示的字体样式
  124. color: "#ffffff",
  125. },
  126. confine: true,
  127. },
  128. xAxis: {
  129. name: this.xName,
  130. nameLocation: "center",
  131. nameGap: 30,
  132. type: "value",
  133. axisTick: { inside: true },
  134. // boundaryGap: false,
  135. },
  136. yAxis: {
  137. name: this.yName,
  138. type: "value",
  139. axisTick: { inside: true },
  140. scale: true,
  141. },
  142. legend: {},
  143. series,
  144. grid: {
  145. top: "75px",
  146. left: "30px",
  147. right: "30px",
  148. bottom: "10%",
  149. containLabel: true,
  150. },
  151. };
  152. this.charts.setOption(option);
  153. },
  154. },
  155. };
  156. </script>
  157. <style lang='less' scoped>
  158. .linechart {
  159. margin: 0 auto;
  160. overflow: hidden;
  161. }
  162. </style>