current-scatter-chart.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. "#3D54BE",
  112. "rgb(255,0,0)",
  113. "#a1a1a1",
  114. "#0098d9",
  115. "#FF8700",
  116. "#005eaa",
  117. "#cda819",
  118. "#32a487"
  119. ],
  120. toolbox: {
  121. show: true,
  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: "item",
  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: "#fff",
  149. },
  150. formatter(params) {
  151. return params.value.length
  152. ? `${params.seriesName}<br />风速:${params.value[0]} m/s<br />功率:${params.value[1]} kW<br />温度:${params.value[2]} ℃`
  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: "120",
  195. top: "5",
  196. // icon: "circle",
  197. itemWidth: 6,
  198. inactiveColor:
  199. that.theme === "dark"
  200. ? partten.getColor("gray")
  201. : "#838383",
  202. textStyle: {
  203. color:
  204. that.theme === "dark"
  205. ? partten.getColor("grayl")
  206. : "#838383",
  207. fontSize: 12,
  208. },
  209. },
  210. visualMap: [{
  211. type: 'continuous',
  212. min: -40,
  213. max: 50,
  214. dimension: 2,
  215. orient: 'vertical',
  216. right: 4,
  217. top: 50,
  218. itemHeight: 600,
  219. text: ['50', '-40'],
  220. calculable: false,
  221. range: [-40,50],
  222. inRange: {
  223. color: ['#000', 'rgb(75,11,106)','rgb(133,33,106)','rgb(176,49,92)','rgb(210,70,69)','rgb(235,100,42)','rgb(247,126,21)','rgb(252,183,28)', 'rgb(249,252,156)']
  224. },
  225. outOfRange: {
  226. color: ['#eee', '#eee']
  227. }
  228. }],
  229. grid: {
  230. top: 48,
  231. left: 40,
  232. right: 40,
  233. bottom: 24,
  234. },
  235. //x轴
  236. xAxis: [
  237. {
  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:
  256. that.theme === "dark"
  257. ? partten.getColor("gray")
  258. : "#000",
  259. },
  260. },
  261. ],
  262. //y轴没有显式设置,根据值自动生成y轴
  263. yAxis: [{
  264. splitLine: { show: false },
  265. position: 'left',
  266. min: 0,
  267. name: 'kW',
  268. nameTextStyle: {
  269. color: '#838383'
  270. }
  271. }, {
  272. splitLine: { show: false },
  273. position: 'right',
  274. min: 0,
  275. }],
  276. animation: true,
  277. dataset: that.dataSet.length? JSON.parse(that.dataSet) : [],
  278. //数据-data是最终要显示的数据
  279. series: that.seriesData,
  280. };
  281. that.resize = function () {
  282. myChart.resize();
  283. };
  284. window.addEventListener("resize", that.resize);
  285. myChart.setOption(option);
  286. if (that.brushSelected) {
  287. myChart.dispatchAction({
  288. type: "takeGlobalCursor",
  289. // 如果想变为“可刷选状态”,必须设置。不设置则会关闭“可刷选状态”。
  290. key: "brush",
  291. brushOption: {
  292. seriesIndex: [2,3],
  293. yAxisIndex: 0,
  294. transformable: true,
  295. throttleType: "debounce",
  296. throttleDelay: 1000,
  297. removeOnClick: true,
  298. brushType: "polygon",
  299. brushMode: "multiple",
  300. brushStyle: {
  301. borderWidth: 1,
  302. color: "rgba(255,36,36,0.2)",
  303. borderColor: "#ff2424",
  304. },
  305. },
  306. });
  307. }
  308. myChart.off("brushSelected");
  309. myChart.on("brushSelected", (params) => {
  310. that.$emit("getSelected", params.batch || []);
  311. });
  312. // myChart.off('click')
  313. // myChart.on('click', params => {
  314. // console.log(params)
  315. // if(params.componentType === 'markArea'){
  316. // myChart.dispatchAction({
  317. // type: 'brush',
  318. // areas: [
  319. // {
  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. },
  330. created() {
  331. this.id = "chart-" + util.newGUID();
  332. },
  333. mounted() {
  334. // this.$nextTick(() => {
  335. this.$el.style.width = this.width;
  336. this.$el.style.height = this.height;
  337. this.initChart();
  338. // });
  339. },
  340. updated() {
  341. console.log('update')
  342. let myChart = echarts.init(document.getElementById(this.id));
  343. myChart.dispose();
  344. this.$nextTick(() => {
  345. this.initChart();
  346. });
  347. },
  348. unmounted() {
  349. window.removeEventListener("resize", this.resize);
  350. },
  351. };
  352. </script>
  353. <style>
  354. .chart {
  355. width: 100%;
  356. height: 100%;
  357. display: inline-block;
  358. }
  359. </style>