current-scatter-chart.vue 8.6 KB

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