1
0

current-scatter-chart.vue 8.9 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. "#05bb4c",
  69. "#4b55ae",
  70. "#fa8c16",
  71. "#f8de5b",
  72. "#1a93cf",
  73. "#c531c7",
  74. "#bd3338",
  75. ],
  76. theme: "dark",
  77. };
  78. },
  79. computed: {
  80. collapse() {
  81. return this.$store.state.collapse;
  82. },
  83. },
  84. watch: {
  85. height() {
  86. if (this.chart) {
  87. this.chart.resize();
  88. }
  89. },
  90. collapse(val) {
  91. if (this.chart) {
  92. setTimeout(() => {
  93. this.chart.resize();
  94. }, 300);
  95. }
  96. },
  97. },
  98. methods: {
  99. resize() {},
  100. initChart() {
  101. const that = this;
  102. echarts.registerTheme("chartTheme", chartTheme);
  103. let myChart = echarts.init(
  104. document.getElementById(this.id),
  105. "chartTheme"
  106. );
  107. that.chart = myChart;
  108. //指定图表的配置项和数据
  109. const option = {
  110. //标题
  111. title: {
  112. text: that.chartTitle,
  113. right: 440,
  114. top: 4,
  115. textStyle: {
  116. fontSize: 14,
  117. color: that.theme === "dark" ? partten.getColor("grayl") : "#000",
  118. },
  119. },
  120. // backgroundColor:
  121. // that.theme === "dark"
  122. // ? "rgba(0,0,0,0.4)"
  123. // : "rgba(255,255,255,0.5)",
  124. //工具箱
  125. // color: [
  126. // "#0098d9",
  127. // "rgb(255,0,0)",
  128. // "#0a4468",
  129. // "#a9e3f199",
  130. // "#a9e3f199",
  131. // "#005eaa",
  132. // "#cda819",
  133. // "#32a487"
  134. // ],
  135. toolbox: {
  136. show: false,
  137. x: "right",
  138. position: [10, 10],
  139. // backgroundColor:'rgba(0,0,0,0.4)',
  140. borderColor: partten.getColor("gray"),
  141. textStyle: {
  142. fontSize: util.vh(16),
  143. color: partten.getColor("gray"),
  144. },
  145. iconStyle: {
  146. borderColor: partten.getColor("gray"),
  147. },
  148. emphasis: {
  149. iconStyle: {
  150. borderColor: partten.getColor("gray"),
  151. },
  152. },
  153. },
  154. tooltip: {
  155. trigger: "axis",
  156. axisPointer: {
  157. type: "cross",
  158. },
  159. // backgroundColor: "rgba(0,0,0,0.4)",
  160. // borderColor: partten.getColor("gray"),
  161. // textStyle: {
  162. // fontSize: util.vh(16),
  163. // color: partten.getColor("gray"),
  164. // },
  165. // formatter(params) {
  166. // return params.value?.x
  167. // ? `${params.seriesName}<br />风速:${params.value.x}m/s<br />功率:${params.value.y}kW`
  168. // : `${params.name}`;
  169. // },
  170. },
  171. brush: {
  172. seriesIndex: [2, 3],
  173. yAxisIndex: 0,
  174. transformable: true,
  175. throttleType: "debounce",
  176. throttleDelay: 1000,
  177. removeOnClick: true,
  178. brushType: "polygon",
  179. brushMode: "multiple",
  180. brushStyle: {
  181. borderWidth: 1,
  182. borderColor: "#ff2424",
  183. },
  184. },
  185. dataZoom: [
  186. {
  187. type: "inside", //图表下方的伸缩条
  188. show: false, //是否显示
  189. realtime: true, //拖动时,是否实时更新系列的视图
  190. start: 0, //伸缩条开始位置(1-100),可以随时更改
  191. end: 100, //伸缩条结束位置(1-100),可以随时更改
  192. },
  193. {
  194. type: "slider", //图表下方的伸缩条
  195. show: false, //是否显示
  196. realtime: true, //拖动时,是否实时更新系列的视图
  197. start: 0, //伸缩条开始位置(1-100),可以随时更改
  198. end: 100, //伸缩条结束位置(1-100),可以随时更改
  199. },
  200. ],
  201. textStyle: {
  202. fontSize: util.vh(16),
  203. color: that.theme === "dark" ? "#fff" : "#000",
  204. },
  205. //图例-每一条数据的名字
  206. legend: {
  207. show: that.showLegend,
  208. // data: [ "拟合功率", "保证功率","无用点", "有用点", "Cp值"],
  209. right: 170,
  210. type: "scroll",
  211. top: "5",
  212. // icon: "circle",
  213. itemWidth: 6,
  214. inactiveColor:
  215. that.theme === "dark" ? partten.getColor("gray") : "#000",
  216. textStyle: {
  217. color: that.theme === "dark" ? partten.getColor("grayl") : "#000",
  218. fontSize: 12,
  219. },
  220. },
  221. grid: {
  222. top: 58,
  223. left: 40,
  224. right: 68,
  225. bottom: 24,
  226. },
  227. //x轴
  228. xAxis: [
  229. {
  230. name: "时间",
  231. nameTextStyle: {
  232. color: "#838383",
  233. },
  234. type: "category",
  235. boundaryGap: true,
  236. data: that.xAxisData || [],
  237. axisLabel: {
  238. formatter: "{value}",
  239. },
  240. splitLine: {
  241. show: false,
  242. },
  243. axisLine: {
  244. onZero: false,
  245. },
  246. smooth: true,
  247. textStyle: {
  248. color: that.theme === "dark" ? partten.getColor("gray") : "#000",
  249. },
  250. },
  251. ],
  252. //y轴没有显式设置,根据值自动生成y轴
  253. yAxis: [
  254. {
  255. splitLine: { show: false },
  256. position: "left",
  257. min: 0,
  258. name: "kW",
  259. nameTextStyle: {
  260. color: "#838383",
  261. },
  262. axisLine: {
  263. onZero: false,
  264. },
  265. },
  266. ],
  267. animation: true,
  268. dataset: that.dataSet.length ? JSON.parse(that.dataSet) : [],
  269. //数据-data是最终要显示的数据
  270. series: that.seriesData,
  271. };
  272. that.resize = function () {
  273. myChart.resize();
  274. };
  275. window.addEventListener("resize", that.resize);
  276. myChart.setOption(option);
  277. // if (that.brushSelected) {
  278. // myChart.dispatchAction({
  279. // type: "takeGlobalCursor",
  280. // // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  281. // key: "brush",
  282. // brushOption: {
  283. // seriesIndex: [2,3],
  284. // yAxisIndex: 0,
  285. // transformable: true,
  286. // throttleType: "debounce",
  287. // throttleDelay: 1000,
  288. // removeOnClick: true,
  289. // brushType: "polygon",
  290. // brushMode: "multiple",
  291. // brushStyle: {
  292. // borderWidth: 1,
  293. // color: "rgba(255,36,36,0.2)",
  294. // borderColor: "#ff2424",
  295. // },
  296. // },
  297. // });
  298. // }
  299. myChart.off("brushSelected");
  300. myChart.on("brushSelected", (params) => {
  301. that.$emit("getSelected", params.batch || []);
  302. });
  303. myChart.off("click");
  304. myChart.on("click", (params) => {
  305. // if(params.componentType === 'markArea'){
  306. // myChart.dispatchAction({
  307. // type: 'brush',
  308. // areas: [
  309. // {
  310. // xAxisIndex: 0,
  311. // brushType: 'lineX',
  312. // coordRange: [params.data.coord[0][0], params.data.coord[1][0]]
  313. // },
  314. // ]
  315. // });
  316. // }
  317. });
  318. },
  319. },
  320. created() {
  321. this.id = "chart-" + util.newGUID();
  322. },
  323. mounted() {
  324. // this.$nextTick(() => {
  325. this.$el.style.width = this.width;
  326. this.$el.style.height = this.height;
  327. this.initChart();
  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>