current-scatter-chart.vue 13 KB

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