widgetGauge.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <div :style="styleObj">
  3. <v-chart :options="options" autoresize />
  4. </div>
  5. </template>
  6. <script>
  7. import echarts from "echarts";
  8. import { eventBusParams } from "@/utils/screen";
  9. export default {
  10. name: "WidgetGauge",
  11. components: {},
  12. props: {
  13. value: Object,
  14. ispreview: Boolean,
  15. },
  16. data() {
  17. return {
  18. options: {
  19. series: [
  20. {
  21. type: "gauge",
  22. z: 100,
  23. axisLine: {
  24. lineStyle: {
  25. width: 10,
  26. color: [
  27. [
  28. 0.3,
  29. new echarts.graphic.LinearGradient(0, 1, 1, 0, [
  30. {
  31. offset: 0,
  32. color: "rgba(0, 237, 3,0.1)",
  33. },
  34. {
  35. offset: 0.5,
  36. color: "rgba(0, 237, 3,0.6)",
  37. },
  38. {
  39. offset: 1,
  40. color: "rgba(0, 237, 3,1)",
  41. },
  42. ]),
  43. ],
  44. [
  45. 0.7,
  46. new echarts.graphic.LinearGradient(0, 1, 1, 0, [
  47. {
  48. offset: 0,
  49. color: "rgba(255, 184, 0,0.1)",
  50. },
  51. {
  52. offset: 0.5,
  53. color: "rgba(255, 184, 0,0.6)",
  54. },
  55. {
  56. offset: 1,
  57. color: "rgba(255, 184, 0,1)",
  58. },
  59. ]),
  60. ],
  61. [
  62. 1,
  63. new echarts.graphic.LinearGradient(0, 1, 1, 0, [
  64. {
  65. offset: 0,
  66. color: "rgba(175, 36, 74,0.1)",
  67. },
  68. {
  69. offset: 0.5,
  70. color: "rgba(255, 36, 74,0.6)",
  71. },
  72. {
  73. offset: 1,
  74. color: "rgba(255, 36, 74,1)",
  75. },
  76. ]),
  77. ],
  78. ],
  79. },
  80. },
  81. pointer: {
  82. itemStyle: {
  83. color: "auto",
  84. },
  85. },
  86. axisTick: {
  87. show: true,
  88. distance: 0,
  89. length: 10,
  90. lineStyle: {
  91. color: "auto",
  92. width: 2,
  93. },
  94. },
  95. splitLine: {
  96. show: true,
  97. distance: 0,
  98. length: 14,
  99. lineStyle: {
  100. color: "auto",
  101. width: 4,
  102. },
  103. },
  104. axisLabel: {
  105. show: true,
  106. color: "white",
  107. distance: 2,
  108. fontSize: 10,
  109. },
  110. detail: {
  111. valueAnimation: true,
  112. formatter: "{value} %",
  113. color: "white",
  114. fontSize: 18,
  115. },
  116. data: [
  117. {
  118. value: 70,
  119. },
  120. ],
  121. },
  122. ],
  123. },
  124. optionsStyle: {}, // 样式
  125. optionsData: {}, // 数据
  126. optionsCollapse: {}, // 图标属性
  127. optionsSetup: {},
  128. };
  129. },
  130. computed: {
  131. styleObj() {
  132. return {
  133. position: this.ispreview ? "absolute" : "static",
  134. width: this.optionsStyle.width + "px",
  135. height: this.optionsStyle.height + "px",
  136. left: this.optionsStyle.left + "px",
  137. top: this.optionsStyle.top + "px",
  138. background: this.optionsSetup.background,
  139. };
  140. },
  141. },
  142. watch: {
  143. value: {
  144. handler(val) {
  145. this.optionsStyle = val.position; // 位置
  146. this.optionsData = val.data; // 数据
  147. this.optionsCollapse = val.collapse; // 折叠数据
  148. this.optionsSetup = val.setup; // 样式
  149. this.editorOptions();
  150. },
  151. deep: true,
  152. },
  153. },
  154. created() {
  155. this.optionsStyle = this.value.position;
  156. this.optionsData = this.value.data;
  157. this.optionsCollapse = this.value.collapse;
  158. this.optionsSetup = this.value.setup;
  159. this.editorOptions();
  160. eventBusParams(
  161. this.optionsSetup,
  162. this.optionsData,
  163. (dynamicData, optionsSetup) => {
  164. console.log("dynamicData", dynamicData);
  165. this.getEchartData(dynamicData, optionsSetup);
  166. }
  167. );
  168. },
  169. methods: {
  170. editorOptions() {
  171. this.setOptions();
  172. this.setOptionsData();
  173. },
  174. setOptions() {
  175. const optionsSetup = this.optionsSetup;
  176. const series = this.options.series;
  177. if (series[0].type == "gauge") {
  178. const axisLine = {
  179. show: optionsSetup.ringShow,
  180. lineStyle: {
  181. width: optionsSetup.pieWeight,
  182. color: [
  183. [
  184. 0.3,
  185. new echarts.graphic.LinearGradient(0, 1, 1, 0, [
  186. {
  187. offset: 0,
  188. color: optionsSetup.color30p0,
  189. },
  190. {
  191. offset: 0.5,
  192. color: optionsSetup.color30p5,
  193. },
  194. {
  195. offset: 1,
  196. color: optionsSetup.color30p10,
  197. },
  198. ]),
  199. ],
  200. [
  201. 0.7,
  202. new echarts.graphic.LinearGradient(0, 1, 1, 0, [
  203. {
  204. offset: 0,
  205. color: optionsSetup.color70p0,
  206. },
  207. {
  208. offset: 0.5,
  209. color: optionsSetup.color70p5,
  210. },
  211. {
  212. offset: 1,
  213. color: optionsSetup.color70p10,
  214. },
  215. ]),
  216. ],
  217. [
  218. 1,
  219. new echarts.graphic.LinearGradient(0, 1, 1, 0, [
  220. {
  221. offset: 0,
  222. color: optionsSetup.color100p0,
  223. },
  224. {
  225. offset: 0.5,
  226. color: optionsSetup.color100p5,
  227. },
  228. {
  229. offset: 1,
  230. color: optionsSetup.color100p10,
  231. },
  232. ]),
  233. ],
  234. ],
  235. },
  236. };
  237. const axisTick = {
  238. show: optionsSetup.tickShow,
  239. distance: optionsSetup.tickDistance,
  240. length: optionsSetup.tickLength,
  241. lineStyle: {
  242. color: "auto",
  243. width: optionsSetup.tickWidth,
  244. },
  245. };
  246. const splitLine = {
  247. show: optionsSetup.splitShow,
  248. distance: optionsSetup.splitDistance,
  249. length: optionsSetup.splitLength,
  250. lineStyle: {
  251. color: "auto",
  252. width: optionsSetup.splitWidth,
  253. },
  254. };
  255. series[0].axisLine = axisLine;
  256. series[0].axisTick = axisTick;
  257. series[0].splitLine = splitLine;
  258. }
  259. },
  260. setOptionsData() {
  261. const optionsData = this.optionsData; // 数据类型 静态 or 动态
  262. optionsData.dataType == "staticData"
  263. ? this.staticDataFn(optionsData.staticData)
  264. : this.dynamicDataFn(optionsData.dynamicData, optionsData.refreshTime);
  265. },
  266. staticDataFn(val) {
  267. const optionsSetup = this.optionsSetup;
  268. const series = this.options.series;
  269. const num = val[0]["num"];
  270. const data = [
  271. {
  272. value: num,
  273. },
  274. ];
  275. const detail = {
  276. valueAnimation: true,
  277. formatter: "{value} %",
  278. color: optionsSetup.labelColor,
  279. fontSize: optionsSetup.labelFontSize,
  280. fontWeight: optionsSetup.labelFontWeight,
  281. };
  282. series[0].data = data;
  283. series[0].detail = detail;
  284. },
  285. dynamicDataFn(val, refreshTime) {
  286. if (!val) return;
  287. if (this.ispreview) {
  288. this.getEchartData(val);
  289. this.flagInter = setInterval(() => {
  290. this.getEchartData(val);
  291. }, refreshTime);
  292. } else {
  293. this.getEchartData(val);
  294. }
  295. },
  296. getEchartData(val) {
  297. const data = this.queryEchartsData(val);
  298. data.then((res) => {
  299. this.renderingFn(res);
  300. });
  301. },
  302. renderingFn(val) {
  303. const optionsSetup = this.optionsSetup;
  304. const series = this.options.series;
  305. const data = [
  306. {
  307. value: val[0].value,
  308. },
  309. ];
  310. const detail = {
  311. valueAnimation: true,
  312. formatter: "{value} %",
  313. color: optionsSetup.labelColor,
  314. fontSize: optionsSetup.labelFontSize,
  315. fontWeight: optionsSetup.labelFontWeight,
  316. };
  317. series[0].data = data;
  318. series[0].detail = detail;
  319. },
  320. },
  321. };
  322. </script>
  323. <style scoped lang="scss">
  324. .echarts {
  325. width: 100%;
  326. height: 100%;
  327. overflow: hidden;
  328. }
  329. </style>