current-scatter-chart.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. left: 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. "#3D54BE",
  127. "rgb(255,0,0)",
  128. "#a1a1a1",
  129. "#0098d9",
  130. "#FF8700",
  131. "#005eaa",
  132. "#cda819",
  133. "#32a487",
  134. ],
  135. toolbox: {
  136. show: true,
  137. x: "right",
  138. position: [10, 10],
  139. // backgroundColor:'rgba(0,0,0,0.4)',
  140. borderColor: "#ccc",
  141. textStyle: {
  142. fontSize: 16,
  143. color: "#ccc",
  144. },
  145. iconStyle: {
  146. borderColor: "#ccc",
  147. },
  148. emphasis: {
  149. iconStyle: {
  150. borderColor: "#ccc",
  151. },
  152. },
  153. },
  154. tooltip: {
  155. trigger: "item",
  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: "#fff",
  164. },
  165. formatter(params) {
  166. return params.value.x
  167. ? `${params.seriesName}<br />光照:${params.value.x} W/m²<br />功率:${params.value.y} kW<br />温度:${params.value.s} ℃`
  168. : `${params.name}`;
  169. },
  170. },
  171. brush: {
  172. seriesIndex: [1, 2],
  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: 16,
  203. color: that.theme === "dark" ? "#fff" : "#000",
  204. },
  205. //图例-每一条数据的名字
  206. legend: {
  207. show: that.showLegend,
  208. data: that.seriesData.map((i) => i.name),
  209. right: "120",
  210. top: "5",
  211. // icon: "circle",
  212. itemWidth: 6,
  213. inactiveColor:
  214. that.theme === "dark" ? partten.getColor("gray") : "#838383",
  215. textStyle: {
  216. color:
  217. that.theme === "dark" ? partten.getColor("grayl") : "#838383",
  218. fontSize: 12,
  219. },
  220. },
  221. visualMap: [
  222. {
  223. type: "continuous",
  224. min: -40,
  225. max: 60,
  226. dimension: 2,
  227. orient: "vertical",
  228. right: 4,
  229. top: 50,
  230. itemHeight: 600,
  231. text: ["60", "-40"],
  232. textStyle: {
  233. color: "#ccc",
  234. },
  235. calculable: false,
  236. range: [-40, 60],
  237. inRange: {
  238. color: [
  239. "#000",
  240. "rgb(75,11,106)",
  241. "rgb(133,33,106)",
  242. "rgb(176,49,92)",
  243. "rgb(210,70,69)",
  244. "rgb(235,100,42)",
  245. "rgb(247,126,21)",
  246. "rgb(252,183,28)",
  247. "rgb(249,252,156)",
  248. ],
  249. },
  250. outOfRange: {
  251. color: ["#eee", "#eee"],
  252. },
  253. },
  254. ],
  255. grid: {
  256. top: 48,
  257. left: 40,
  258. right: 50,
  259. bottom: 24,
  260. containLabel: true,
  261. },
  262. //x轴
  263. xAxis: [
  264. {
  265. name: "光照",
  266. nameTextStyle: {
  267. color: "#838383",
  268. },
  269. type: "value",
  270. boundaryGap: false,
  271. data: that.xAxisData || [],
  272. min: 0,
  273. max: "dataMax",
  274. // interval: 1,
  275. axisLabel: {
  276. formatter: "{value}",
  277. },
  278. splitLine: {
  279. show: false,
  280. },
  281. textStyle: {
  282. color: that.theme === "dark" ? partten.getColor("gray") : "#000",
  283. },
  284. },
  285. ],
  286. //y轴没有显式设置,根据值自动生成y轴
  287. yAxis: [
  288. {
  289. splitLine: { show: false },
  290. position: "left",
  291. min: 0,
  292. name: "kW",
  293. nameTextStyle: {
  294. color: "#838383",
  295. },
  296. },
  297. {
  298. splitLine: { show: false },
  299. position: "right",
  300. min: 0,
  301. },
  302. ],
  303. animation: true,
  304. dataset: that.dataSet.length ? JSON.parse(that.dataSet) : [],
  305. //数据-data是最终要显示的数据
  306. series: that.seriesData,
  307. };
  308. that.resize = function () {
  309. myChart.resize();
  310. };
  311. window.addEventListener("resize", that.resize);
  312. myChart.setOption(option);
  313. if (that.brushSelected) {
  314. myChart.dispatchAction({
  315. type: "takeGlobalCursor",
  316. // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  317. key: "brush",
  318. brushOption: {
  319. seriesIndex: [2, 3],
  320. yAxisIndex: 0,
  321. transformable: true,
  322. throttleType: "debounce",
  323. throttleDelay: 1000,
  324. removeOnClick: true,
  325. brushType: "polygon",
  326. brushMode: "multiple",
  327. brushStyle: {
  328. borderWidth: 1,
  329. color: "rgba(255,36,36,0.2)",
  330. borderColor: "#ff2424",
  331. },
  332. },
  333. });
  334. }
  335. myChart.off("brushSelected");
  336. myChart.on("brushSelected", (params) => {
  337. console.log(params);
  338. that.$emit("getSelected", params.batch || []);
  339. });
  340. myChart.off("click");
  341. myChart.on("click", (params) => {
  342. if (params.componentType === "markArea") {
  343. myChart.dispatchAction({
  344. type: "brush",
  345. areas: [
  346. {
  347. xAxisIndex: 0,
  348. brushType: "lineX",
  349. coordRange: [params.data.coord[0][0], params.data.coord[1][0]],
  350. },
  351. ],
  352. });
  353. }
  354. });
  355. },
  356. },
  357. created() {
  358. this.id = "chart-" + util.newGUID();
  359. },
  360. mounted() {
  361. // this.$nextTick(() => {
  362. this.$el.style.width = this.width;
  363. this.$el.style.height = this.height;
  364. this.initChart();
  365. if (this.chart) {
  366. this.chart.resize();
  367. }
  368. // });
  369. },
  370. updated() {
  371. // console.log('update')
  372. let myChart = echarts.init(document.getElementById(this.id));
  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>
  384. .chart {
  385. width: 100%;
  386. height: 100%;
  387. display: inline-block;
  388. }
  389. </style>