current-scatter-chart.vue 14 KB

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