current-scatter-chart.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. },
  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: 'light'
  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(document.getElementById(this.id), 'chartTheme');
  104. that.chart = myChart
  105. //指定图表的配置项和数据
  106. const option = {
  107. //标题
  108. title: {
  109. text: that.chartTitle,
  110. right: 440,
  111. top: 4,
  112. textStyle: {
  113. fontSize: 14,
  114. color: that.theme === "dark" ? partten.getColor("grayl") : "#000",
  115. },
  116. },
  117. // backgroundColor:
  118. // that.theme === "dark"
  119. // ? "rgba(0,0,0,0.4)"
  120. // : "rgba(255,255,255,0.5)",
  121. //工具箱
  122. color: [
  123. "#cda819",
  124. "rgb(255,0,0)",
  125. "#0098d9",
  126. "#a9e3f199",
  127. "#a9e3f199",
  128. "#005eaa",
  129. "#0a4468",
  130. "#32a487"
  131. ],
  132. toolbox: {
  133. show: false,
  134. x: "right",
  135. position: [10, 10],
  136. // backgroundColor:'rgba(0,0,0,0.4)',
  137. borderColor: partten.getColor("gray"),
  138. textStyle: {
  139. fontSize: util.vh(16),
  140. color: partten.getColor("gray")
  141. },
  142. iconStyle: {
  143. borderColor:partten.getColor("gray")
  144. },
  145. emphasis: {
  146. iconStyle: {
  147. borderColor:partten.getColor("gray")
  148. },
  149. },
  150. },
  151. tooltip: {
  152. trigger: "axis",
  153. axisPointer: {
  154. type: "cross",
  155. },
  156. // backgroundColor: "rgba(0,0,0,0.4)",
  157. // borderColor: partten.getColor("gray"),
  158. // textStyle: {
  159. // fontSize: util.vh(16),
  160. // color: partten.getColor("gray"),
  161. // },
  162. // formatter(params) {
  163. // return params.value?.x
  164. // ? `${params.seriesName}<br />风速:${params.value.x}m/s<br />功率:${params.value.y}kW`
  165. // : `${params.name}`;
  166. // },
  167. },
  168. brush: {
  169. seriesIndex: [2,3],
  170. yAxisIndex: 0,
  171. transformable: true,
  172. throttleType: "debounce",
  173. throttleDelay: 1000,
  174. removeOnClick: true,
  175. brushType: "polygon",
  176. brushMode: "multiple",
  177. brushStyle: {
  178. borderWidth: 1,
  179. borderColor: "#ff2424",
  180. },
  181. },
  182. dataZoom: [
  183. {
  184. type: "inside", //图表下方的伸缩条
  185. show: true, //是否显示
  186. realtime: true, //拖动时,是否实时更新系列的视图
  187. start: 0, //伸缩条开始位置(1-100),可以随时更改
  188. end: 5, //伸缩条结束位置(1-100),可以随时更改
  189. },
  190. {
  191. type: "slider", //图表下方的伸缩条
  192. show: true, //是否显示
  193. realtime: true, //拖动时,是否实时更新系列的视图
  194. start: 0, //伸缩条开始位置(1-100),可以随时更改
  195. end: 5, //伸缩条结束位置(1-100),可以随时更改
  196. },
  197. ],
  198. textStyle: {
  199. fontSize: util.vh(16),
  200. color: that.theme === "dark" ? "#fff" : "#000",
  201. },
  202. //图例-每一条数据的名字
  203. legend: {
  204. show: that.showLegend,
  205. // data: [ "拟合功率", "保证功率","无用点", "有用点", "Cp值"],
  206. right: 170,
  207. type: 'scroll',
  208. top: "5",
  209. // icon: "circle",
  210. itemWidth: 6,
  211. inactiveColor:
  212. that.theme === "dark"
  213. ? partten.getColor("gray")
  214. : "#000",
  215. textStyle: {
  216. color:
  217. that.theme === "dark"
  218. ? partten.getColor("grayl")
  219. : "#000",
  220. fontSize: 12,
  221. },
  222. },
  223. grid: {
  224. top: 58,
  225. left: 40,
  226. right: 48,
  227. // bottom: 24,
  228. },
  229. //x轴
  230. xAxis: [
  231. {
  232. name: '时间',
  233. nameTextStyle: {
  234. color: '#838383'
  235. },
  236. type: "category",
  237. boundaryGap: true,
  238. data: that.xAxisData || [],
  239. axisLabel: {
  240. formatter: "{value}",
  241. },
  242. splitLine: {
  243. show: false,
  244. },
  245. smooth: true,
  246. textStyle: {
  247. color:
  248. that.theme === "dark"
  249. ? partten.getColor("gray")
  250. : "#000",
  251. },
  252. },
  253. ],
  254. //y轴没有显式设置,根据值自动生成y轴
  255. yAxis: [{
  256. splitLine: { show: false },
  257. position: 'left',
  258. min: 0,
  259. name: '度',
  260. nameTextStyle: {
  261. color: '#838383'
  262. },
  263. }],
  264. animation: true,
  265. dataset: that.dataSet.length? JSON.parse(that.dataSet) : [],
  266. //数据-data是最终要显示的数据
  267. series: that.seriesData,
  268. };
  269. that.resize = function () {
  270. myChart.resize();
  271. };
  272. window.addEventListener("resize", that.resize);
  273. myChart.setOption(option);
  274. // if (that.brushSelected) {
  275. // myChart.dispatchAction({
  276. // type: "takeGlobalCursor",
  277. // // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  278. // key: "brush",
  279. // brushOption: {
  280. // seriesIndex: [2,3],
  281. // yAxisIndex: 0,
  282. // transformable: true,
  283. // throttleType: "debounce",
  284. // throttleDelay: 1000,
  285. // removeOnClick: true,
  286. // brushType: "polygon",
  287. // brushMode: "multiple",
  288. // brushStyle: {
  289. // borderWidth: 1,
  290. // color: "rgba(255,36,36,0.2)",
  291. // borderColor: "#ff2424",
  292. // },
  293. // },
  294. // });
  295. // }
  296. myChart.off("brushSelected");
  297. myChart.on("brushSelected", (params) => {
  298. that.$emit("getSelected", params.batch || []);
  299. });
  300. myChart.off('click')
  301. myChart.on('click', 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. // });
  326. },
  327. updated() {
  328. // console.log('update')
  329. let myChart = echarts.init(document.getElementById(this.id));
  330. myChart.dispose();
  331. this.$nextTick(() => {
  332. this.initChart();
  333. });
  334. },
  335. unmounted() {
  336. window.removeEventListener("resize", this.resize);
  337. },
  338. };
  339. </script>
  340. <style>
  341. .chart {
  342. width: 100%;
  343. height: 100%;
  344. display: inline-block;
  345. }
  346. </style>