current-scatter-chart.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@tools/util";
  6. import partten from "@/helper/partten";
  7. import * as echarts from "echarts";
  8. import chartTheme from "./current-scatter-chart.json";
  9. export default {
  10. name: "currentScatterChart",
  11. props: {
  12. // 图表宽度
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. // 图表高度
  18. height: {
  19. type: String,
  20. default: "350px",
  21. },
  22. // 图表主标题
  23. chartTitle: {
  24. type: String,
  25. default: "自定义图表组件",
  26. },
  27. // X 轴配置项
  28. xAxisData: {
  29. type: Array,
  30. default: () => {
  31. return [];
  32. },
  33. },
  34. // Y 轴配置项
  35. yAxisData: {
  36. type: Array,
  37. default: () => {
  38. return [];
  39. },
  40. },
  41. dataSet: {
  42. type: String,
  43. default: "",
  44. },
  45. // 图表核心数据
  46. seriesData: {
  47. type: Array,
  48. default: () => {
  49. return [];
  50. },
  51. },
  52. // 是否显示图表图例
  53. showLegend: {
  54. type: Boolean,
  55. default: true,
  56. },
  57. // 是否默认采用笔刷模式
  58. brushSelected: {
  59. type: Boolean,
  60. default: false,
  61. },
  62. },
  63. data() {
  64. return {
  65. id: "",
  66. chart: null,
  67. color: [
  68. "#2999a0", "#67b9ff", "#ff6271", "#05b2fa", "#0ef167","#05bb4c", "#4b55ae"
  69. ],
  70. theme: "dark",
  71. };
  72. },
  73. computed: {
  74. collapse() {
  75. return this.$store.state.collapse;
  76. },
  77. },
  78. watch: {
  79. height() {
  80. if (this.chart) {
  81. this.chart.resize();
  82. }
  83. },
  84. collapse(val) {
  85. if (this.chart) {
  86. setTimeout(() => {
  87. this.chart.resize();
  88. }, 300);
  89. }
  90. },
  91. },
  92. methods: {
  93. resize() {},
  94. initChart() {
  95. const that = this;
  96. echarts.registerTheme("chartTheme", chartTheme);
  97. let myChart = echarts.init(
  98. document.getElementById(this.id),
  99. "chartTheme"
  100. );
  101. that.chart = myChart;
  102. //指定图表的配置项和数据
  103. const option = {
  104. //标题
  105. title: {
  106. text: that.chartTitle,
  107. right: 440,
  108. top: 4,
  109. textStyle: {
  110. fontSize: 14,
  111. color: that.theme === "dark" ? partten.getColor("grayl") : "#000",
  112. },
  113. },
  114. // backgroundColor:
  115. // that.theme === "dark"
  116. // ? "rgba(0,0,0,0.4)"
  117. // : "rgba(255,255,255,0.5)",
  118. //工具箱
  119. color: [
  120. // "#3D54BE",
  121. // "rgb(255,0,0)",
  122. // "#a1a1a1",
  123. // "#0098d9",
  124. // "#FF8700",
  125. // "#005eaa",
  126. // "#cda819",
  127. // "#32a487",
  128. "#2999a0", "#67b9ff", "#a1a1a1", "#05b2fa", "#0ef167","#05bb4c", "#4b55ae"
  129. ],
  130. toolbox: {
  131. show: true,
  132. x: "right",
  133. position: [10, 10],
  134. // backgroundColor:'rgba(0,0,0,0.4)',
  135. borderColor: "#ccc",
  136. textStyle: {
  137. fontSize: 16,
  138. color: "#ccc",
  139. },
  140. iconStyle: {
  141. borderColor: "#ccc",
  142. },
  143. emphasis: {
  144. iconStyle: {
  145. borderColor: "#ccc",
  146. },
  147. },
  148. },
  149. tooltip: {
  150. trigger: "item",
  151. axisPointer: {
  152. type: "cross",
  153. },
  154. backgroundColor: "rgba(0,0,0,0.4)",
  155. borderColor: partten.getColor("gray"),
  156. textStyle: {
  157. fontSize: util.vh(16),
  158. color: "#fff",
  159. },
  160. formatter(params) {
  161. return params.value?.x
  162. ? `${params.seriesName}<br />风速:${params.value.x} m/s<br />功率:${params.value.y} kW`
  163. : `${params.name}`;
  164. },
  165. },
  166. brush: {
  167. seriesIndex: [2, 3],
  168. yAxisIndex: 0,
  169. transformable: true,
  170. throttleType: "debounce",
  171. throttleDelay: 1000,
  172. removeOnClick: true,
  173. brushType: "polygon",
  174. brushMode: "multiple",
  175. brushStyle: {
  176. borderWidth: 1,
  177. borderColor: "#ff2424",
  178. },
  179. },
  180. dataZoom: [
  181. {
  182. type: "inside", //图表下方的伸缩条
  183. show: false, //是否显示
  184. realtime: true, //拖动时,是否实时更新系列的视图
  185. start: 0, //伸缩条开始位置(1-100),可以随时更改
  186. end: 100, //伸缩条结束位置(1-100),可以随时更改
  187. },
  188. {
  189. type: "slider", //图表下方的伸缩条
  190. show: false, //是否显示
  191. realtime: true, //拖动时,是否实时更新系列的视图
  192. start: 0, //伸缩条开始位置(1-100),可以随时更改
  193. end: 100, //伸缩条结束位置(1-100),可以随时更改
  194. },
  195. ],
  196. textStyle: {
  197. fontSize: 16,
  198. color: that.theme === "dark" ? "#fff" : "#000",
  199. },
  200. //图例-每一条数据的名字
  201. legend: {
  202. show: that.showLegend,
  203. data: ["拟合功率", "保证功率", "无用点", "有用点", "Cp值"],
  204. right: "120",
  205. top: "5",
  206. // icon: "circle",
  207. itemWidth: 6,
  208. inactiveColor:
  209. that.theme === "dark" ? partten.getColor("gray") : "#000",
  210. textStyle: {
  211. color: that.theme === "dark" ? partten.getColor("grayl") : "#000",
  212. fontSize: 12,
  213. },
  214. },
  215. grid: {
  216. top: 48,
  217. left: 40,
  218. right: 45,
  219. bottom: 24,
  220. containLabel: true,
  221. },
  222. //x轴
  223. xAxis: [
  224. {
  225. name: "m/s",
  226. nameTextStyle: {
  227. color: "#838383",
  228. },
  229. type: "value",
  230. boundaryGap: false,
  231. data: that.xAxisData || [],
  232. min: 0,
  233. max: 25,
  234. interval: 1,
  235. axisLabel: {
  236. formatter: "{value}",
  237. },
  238. splitLine: {
  239. show: false,
  240. },
  241. textStyle: {
  242. color: that.theme === "dark" ? partten.getColor("gray") : "#000",
  243. },
  244. },
  245. ],
  246. //y轴没有显式设置,根据值自动生成y轴
  247. yAxis: [
  248. {
  249. splitLine: { show: false },
  250. position: "left",
  251. min: 0,
  252. name: "kW",
  253. nameTextStyle: {
  254. color: "#838383",
  255. },
  256. },
  257. {
  258. splitLine: { show: false },
  259. position: "right",
  260. min: 0,
  261. },
  262. ],
  263. animation: true,
  264. dataset: that.dataSet.length ? JSON.parse(that.dataSet) : [],
  265. //数据-data是最终要显示的数据
  266. series: that.seriesData,
  267. };
  268. that.resize = function () {
  269. myChart.resize();
  270. };
  271. window.addEventListener("resize", that.resize);
  272. myChart.setOption(option);
  273. if (that.brushSelected) {
  274. myChart.dispatchAction({
  275. type: "takeGlobalCursor",
  276. // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  277. key: "brush",
  278. brushOption: {
  279. seriesIndex: [2, 3],
  280. yAxisIndex: 0,
  281. transformable: true,
  282. throttleType: "debounce",
  283. throttleDelay: 1000,
  284. removeOnClick: true,
  285. brushType: "polygon",
  286. brushMode: "multiple",
  287. brushStyle: {
  288. borderWidth: 1,
  289. color: "rgba(255,36,36,0.2)",
  290. borderColor: "#ff2424",
  291. },
  292. },
  293. });
  294. }
  295. myChart.off("brushSelected");
  296. myChart.on("brushSelected", (params) => {
  297. that.$emit("getSelected", params.batch || []);
  298. });
  299. myChart.off("click");
  300. myChart.on("click", (params) => {
  301. // console.log(params)
  302. if (params.componentType === "markArea") {
  303. myChart.dispatchAction({
  304. type: "brush",
  305. areas: [
  306. {
  307. xAxisIndex: 0,
  308. brushType: "lineX",
  309. coordRange: [params.data.coord[0][0], params.data.coord[1][0]],
  310. },
  311. ],
  312. });
  313. }
  314. });
  315. },
  316. },
  317. created() {
  318. this.id = "chart-" + util.newGUID();
  319. },
  320. mounted() {
  321. // this.$nextTick(() => {
  322. this.$el.style.width = this.width;
  323. this.$el.style.height = this.height;
  324. this.initChart();
  325. if (this.chart) {
  326. this.chart.resize();
  327. }
  328. // });
  329. },
  330. updated() {
  331. // console.log('update')
  332. let myChart = echarts.init(document.getElementById(this.id));
  333. myChart.dispose();
  334. this.$nextTick(() => {
  335. this.initChart();
  336. });
  337. },
  338. unmounted() {
  339. window.removeEventListener("resize", this.resize);
  340. },
  341. };
  342. </script>
  343. <style>
  344. .chart {
  345. width: 100%;
  346. height: 100%;
  347. display: inline-block;
  348. }
  349. </style>