area-line-chart.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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: "multiple-bar-chart",
  10. componentName: "multiple-bar-chart",
  11. props: {
  12. width: {
  13. type: String,
  14. default: "100%",
  15. },
  16. height: {
  17. type: String,
  18. default: "800px",
  19. },
  20. lineData: {
  21. type: Array,
  22. default: () => [],
  23. },
  24. areaData: {
  25. type: Array,
  26. default: () => [],
  27. },
  28. // 单位
  29. units: {
  30. type: Array,
  31. default: () => ["健康趋势", "风机健康状态数量"],
  32. },
  33. // 显示 legend
  34. showLegend: {
  35. type: Boolean,
  36. default: true,
  37. },
  38. // 颜色
  39. color: {
  40. type: Array,
  41. default: () => ["#1c99ff", "#3d54be", "#f8de5b"],
  42. },
  43. },
  44. data() {
  45. return {
  46. id: "",
  47. chart: null,
  48. };
  49. },
  50. computed: {
  51. legend() {
  52. let data = [];
  53. this.lineData.forEach((value, index) => {
  54. data.push(value.text);
  55. });
  56. return data;
  57. },
  58. xAxisData() {
  59. let data = [];
  60. if (this.lineData.length > 0)
  61. this.lineData[0].value.forEach((value, index) => {
  62. data.push(value.text);
  63. });
  64. return data;
  65. },
  66. areaChartData() {
  67. let data = [];
  68. if (this.areaData && this.areaData.length) {
  69. for (var i = 0; i < this.areaData.length; i++) {
  70. let item = this.areaData[i];
  71. var color = item.color;
  72. data.push({
  73. name: item.name,
  74. value: [item.start, item.end, item.end - item.start],
  75. itemStyle: {
  76. normal: {
  77. color: color,
  78. },
  79. },
  80. exData: item,
  81. });
  82. }
  83. }
  84. return data;
  85. },
  86. areaMax() {
  87. let max = 0;
  88. this.areaData.forEach((value) => {
  89. if (max < value.end) max = value.end;
  90. });
  91. return max;
  92. },
  93. },
  94. methods: {
  95. renderItem(params, api) {
  96. var start = api.coord([api.value(0)]);
  97. var end = api.coord([api.value(1)]);
  98. var height = api.size([0, 1])[1];
  99. var rectShape = echarts.graphic.clipRectByRect(
  100. {
  101. x: start[0],
  102. y: start[1] - height / 2,
  103. width: end[0] - start[0],
  104. height: height,
  105. },
  106. {
  107. x: params.coordSys.x,
  108. y: params.coordSys.y,
  109. width: params.coordSys.width,
  110. height: params.coordSys.height,
  111. }
  112. );
  113. return (
  114. rectShape && {
  115. type: "rect",
  116. transition: ["shape"],
  117. shape: rectShape,
  118. style: api.style(),
  119. }
  120. );
  121. },
  122. initChart() {
  123. let that = this;
  124. let chart = echarts.init(this.$el);
  125. let option = {
  126. color: this.color,
  127. grid: {
  128. left: 40,
  129. right: 40,
  130. bottom: 40,
  131. top: 32,
  132. containLabel: true,
  133. },
  134. tooltip: {
  135. show: true,
  136. trigger: "axis",
  137. axisPointer: {
  138. type: "cross",
  139. },
  140. backgroundColor:
  141. this.$store.state.themeName === "dark"
  142. ? "rgba(0,0,0,0.4)"
  143. : "rgba(255,255,255,0.5)",
  144. borderColor:
  145. this.$store.state.themeName === "dark"
  146. ? partten.getColor("gray")
  147. : "#000",
  148. textStyle: {
  149. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  150. fontSize: 14,
  151. },
  152. formatter: function (params) {
  153. var htmlStr = `<div style='margin-bottom:5px'>${params[0].axisValue}</div>`;
  154. for (var i = 0; i < params.length; i++) {
  155. htmlStr += `<div style='margin-bottom:2px'>`;
  156. var param = params[i];
  157. var seriesName = param.seriesName; //图例名称
  158. var value = param.value; //y轴值
  159. var mark = param.marker; //点
  160. var unit = `<span style='font-size:12px'>${
  161. seriesName === "风速" ? "m/s" : "MW"
  162. }</span>`;
  163. htmlStr += mark; //一个点
  164. htmlStr += `${seriesName} : ${
  165. value != null ? value + unit : "--"
  166. }`; //圆点后面显示的文本
  167. htmlStr += "</div>";
  168. }
  169. return htmlStr;
  170. },
  171. },
  172. legend: {
  173. show: this.showLegend,
  174. data: this.legend,
  175. right: 120,
  176. icon: "ract",
  177. itemWidth: 8,
  178. itemHeight: 8,
  179. inactiveColor:
  180. this.$store.state.themeName === "dark"
  181. ? partten.getColor("gray")
  182. : "#000",
  183. textStyle: {
  184. color:
  185. this.$store.state.themeName === "dark"
  186. ? partten.getColor("grayl")
  187. : "#000",
  188. fontSize: 12,
  189. },
  190. },
  191. xAxis: [
  192. {
  193. type: "category",
  194. axisLabel: {
  195. color:
  196. this.$store.state.themeName === "dark"
  197. ? partten.getColor("gray")
  198. : "#000",
  199. },
  200. axisLine: {
  201. show: false,
  202. },
  203. axisTick: {
  204. show: false,
  205. },
  206. data: this.xAxisData,
  207. },
  208. {
  209. show: false,
  210. min: 0,
  211. max: this.areaMax,
  212. axisLabel: {
  213. show: false,
  214. formatter: function (val) {
  215. return Math.max(0, val - 0) + " ms";
  216. },
  217. },
  218. },
  219. ],
  220. yAxis: [
  221. {
  222. type: "value",
  223. name: this.units[0],
  224. axisLabel: {
  225. formatter: "{value} ",
  226. color:
  227. this.$store.state.themeName === "dark"
  228. ? partten.getColor("gray")
  229. : "#000",
  230. },
  231. axisLine: {
  232. type: "dashed",
  233. lineStyle: {
  234. color:
  235. this.$store.state.themeName === "dark"
  236. ? partten.getColor("gray")
  237. : "#000",
  238. },
  239. width: 5,
  240. },
  241. axisTick: {
  242. show: false,
  243. },
  244. splitLine: {
  245. lineStyle: {
  246. type: "dashed",
  247. dashOffset: 10,
  248. color:
  249. this.$store.state.themeName === "dark"
  250. ? partten.getColor("gray")
  251. : "#000" + 80,
  252. },
  253. },
  254. },
  255. {
  256. data: [
  257. this.areaData && this.areaData.length
  258. ? this.areaData[0].name
  259. : "",
  260. ],
  261. axisLabel: { show: false },
  262. },
  263. ],
  264. series: [],
  265. };
  266. // line data
  267. if (this.lineData.length > 0) {
  268. this.lineData.forEach((value, index) => {
  269. option.series.push({
  270. name: value.text,
  271. type: "line",
  272. data: value.value,
  273. smooth: true, //平滑展示
  274. yAxisIndex: 0,
  275. });
  276. });
  277. }
  278. // 区域
  279. if (this.areaData && this.areaData.length > 0) {
  280. option.series.push({
  281. type: "custom",
  282. renderItem: this.renderItem,
  283. yAxisIndex: 1,
  284. xAxisIndex: 1,
  285. itemStyle: {
  286. opacity: 0.2,
  287. },
  288. tooltip: {
  289. show: false,
  290. formatter: function (params) {
  291. return params.marker + params.name + ": " + params.value[2] + "s";
  292. },
  293. },
  294. encode: {
  295. x: [1, 2],
  296. y: 0,
  297. },
  298. data: this.areaChartData,
  299. });
  300. }
  301. chart.setOption(option);
  302. return chart;
  303. },
  304. },
  305. emits: {
  306. areaClick: null,
  307. },
  308. created() {
  309. this.id = "pie-chart-" + util.newGUID();
  310. },
  311. mounted() {
  312. this.$nextTick(() => {
  313. this.$el.style.width = this.width;
  314. this.$el.style.height = this.height;
  315. let that = this;
  316. let chart = this.initChart();
  317. chart.on("click", function (e, p) {
  318. console.log(e);
  319. if (e.seriesType == "custom") {
  320. that.$emit("areaClick", { data: e.data.exData });
  321. }
  322. });
  323. });
  324. },
  325. updated() {
  326. this.$nextTick(() => {
  327. this.initChart();
  328. });
  329. },
  330. watch: {
  331. "$store.state.themeName"() {
  332. this.initChart();
  333. },
  334. },
  335. };
  336. </script>
  337. <style lang="less">
  338. .chart {
  339. width: 100%;
  340. height: 100%;
  341. display: inline-block;
  342. }
  343. </style>