multi-arrow-line-chart.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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: "multi-arrow-line-chart",
  10. componentName: "multi-arrow-line-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "13.889vh",
  19. },
  20. // 数据
  21. list: {
  22. type: Array,
  23. default: () => [
  24. {
  25. title: "功率",
  26. yAxisIndex: 0,
  27. value: [
  28. {
  29. text: "0m/s",
  30. value: 1,
  31. },
  32. {
  33. text: "2m/s",
  34. value: 2,
  35. },
  36. {
  37. text: "3m/s",
  38. value: 16,
  39. },
  40. {
  41. text: "4m/s",
  42. value: 3,
  43. },
  44. {
  45. text: "5m/s",
  46. value: 3,
  47. },
  48. {
  49. text: "6m/s",
  50. value: 31,
  51. },
  52. {
  53. text: "7m/s",
  54. value: 33,
  55. },
  56. {
  57. text: "8m/s",
  58. value: 43,
  59. },
  60. {
  61. text: "9m/s",
  62. value: 3,
  63. },
  64. {
  65. text: "10m/s",
  66. value: 3,
  67. },
  68. {
  69. text: "11m/s",
  70. value: 31,
  71. },
  72. {
  73. text: "12m/s",
  74. value: 3,
  75. },
  76. {
  77. text: "13m/s",
  78. value: 3,
  79. },
  80. ],
  81. },
  82. ],
  83. },
  84. // 单位
  85. units: {
  86. type: Array,
  87. default: () => ["()"],
  88. },
  89. showLegend: {
  90. type: Boolean,
  91. default: true,
  92. },
  93. isSingle: {
  94. type: Boolean,
  95. default: false,
  96. },
  97. },
  98. data() {
  99. return {
  100. id: "",
  101. chart: null,
  102. color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b"],
  103. };
  104. },
  105. computed: {
  106. legend() {
  107. return this.list.map((t) => {
  108. return t.title;
  109. });
  110. },
  111. xdata() {
  112. return this.list[0].value.map((t) => {
  113. return t.text;
  114. });
  115. },
  116. yAxis() {
  117. let result = [];
  118. this.units.forEach((value, index) => {
  119. result.push({
  120. type: "value",
  121. name: value,
  122. axisLabel: {
  123. formatter: "{value}",
  124. fontSize: util.vh(14),
  125. },
  126. //分格线
  127. splitLine: {
  128. lineStyle: {
  129. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000" + 55,
  130. type: "dashed",
  131. },
  132. },
  133. });
  134. });
  135. return result;
  136. },
  137. series() {
  138. let result = [];
  139. let isSingle = this.isSingle;
  140. this.list.forEach((value, index) => {
  141. var data = value.value.map((t) => {
  142. return t.value;
  143. });
  144. var arrowArr = [];
  145. data.forEach((dvalue, dindex) => {
  146. var item = dvalue;
  147. arrowArr.push({
  148. symbol: "arrow",
  149. symbolSize: 12,
  150. symbolRotate: -90,
  151. value: item,
  152. });
  153. });
  154. result.push({
  155. name: value.title,
  156. type: "line",
  157. smooth: true,
  158. zlevel: index,
  159. lineStyle: {
  160. normal: {
  161. color: this.color[index],
  162. width: 1,
  163. },
  164. },
  165. yAxisIndex: value.yAxisIndex,
  166. data: arrowArr,
  167. });
  168. });
  169. return result;
  170. },
  171. },
  172. methods: {
  173. initChart() {
  174. const chart = echarts.init(this.$el);
  175. let option = {
  176. color: this.color,
  177. tooltip: {
  178. trigger: "axis",
  179. backgroundColor: this.$store.state.themeName === "dark" ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
  180. borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  181. textStyle: {
  182. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  183. fontSize: util.vh(16),
  184. },
  185. },
  186. legend: {
  187. show: this.showLegend,
  188. data: this.legend,
  189. right: 56,
  190. icon: "circle",
  191. itemWidth: 6,
  192. inactiveColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  193. textStyle: {
  194. color: this.$store.state.themeName === "dark" ? partten.getColor("grayl") : "#000",
  195. fontSize: 12,
  196. },
  197. },
  198. grid: {
  199. top: util.vh(32),
  200. left: util.vh(40),
  201. right: util.vh(40),
  202. bottom: util.vh(24),
  203. },
  204. xAxis: [
  205. {
  206. type: "category",
  207. boundaryGap: false,
  208. axisLabel: {
  209. formatter: "{value}",
  210. fontSize: util.vh(14),
  211. textStyle: {
  212. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  213. },
  214. },
  215. data: this.xdata,
  216. },
  217. ],
  218. yAxis: this.yAxis,
  219. series: this.series,
  220. };
  221. chart.clear();
  222. chart.setOption(option);
  223. this.resize = function() {
  224. chart.resize();
  225. };
  226. window.addEventListener("resize", this.resize);
  227. },
  228. },
  229. created() {
  230. this.id = "pie-chart-" + util.newGUID();
  231. },
  232. mounted() {
  233. this.$nextTick(() => {
  234. this.$el.style.width = this.width;
  235. this.$el.style.height = this.height;
  236. this.initChart();
  237. });
  238. },
  239. updated() {
  240. this.$nextTick(() => {
  241. this.initChart();
  242. });
  243. },
  244. unmounted() {
  245. window.removeEventListener("resize", this.resize);
  246. },
  247. };
  248. </script>
  249. <style lang="less">
  250. .chart {
  251. width: 100%;
  252. height: 100%;
  253. display: inline-block;
  254. }
  255. </style>