normal-radar-chart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <div class="chart" :id="id"></div>
  3. </template>
  4. <script>
  5. import util from "@/helper/util.js";
  6. import partten from "@/helper/partten.js";
  7. import * as echarts from "echarts";
  8. export default {
  9. name: "radar-chart",
  10. componentName: "radar-chart",
  11. props: {
  12. // 宽度 默认9.722vh
  13. width: {
  14. type: String,
  15. default: "100%",
  16. },
  17. // 高度 默认9.722vh
  18. height: {
  19. type: String,
  20. default: "7.4074vh",
  21. },
  22. // 标题
  23. title: {
  24. type: Array,
  25. default: () => ["-", "-"],
  26. },
  27. // 值
  28. value: {
  29. type: Array,
  30. default: () => {
  31. return [
  32. {
  33. indicator: ["N0", "N1", "N2", "N3", "N4", "N5"],
  34. data: [
  35. {
  36. value: [44200, 14200, 20000, 35000, 50000, 38000],
  37. name: "风机一号",
  38. },
  39. ],
  40. },
  41. ];
  42. },
  43. },
  44. showLegend: {
  45. type: Boolean,
  46. default: true,
  47. },
  48. showTool: {
  49. type: Boolean,
  50. default: true,
  51. }
  52. },
  53. data() {
  54. return {
  55. id: "",
  56. chart: null,
  57. lineStyles: [],
  58. orange: {
  59. areaStyle: {
  60. color: "rgba(255,119,0, 0.6)",
  61. },
  62. lineStyle: {
  63. color: "#C92F00",
  64. },
  65. itemStyle: {
  66. color: "#C92F00",
  67. borderColor: "#C92F00",
  68. borderWidth: 2,
  69. },
  70. },
  71. blue: {
  72. areaStyle: {
  73. color: 'rgba(16,148,255, 0.6)',
  74. },
  75. lineStyle: {
  76. color: "#FFFFFF",
  77. },
  78. itemStyle: {
  79. color: 'rgba(28,153,255, 0.9)',
  80. borderColor: "rgba(255,255,255, 0.5)",
  81. borderWidth: 0.5,
  82. },
  83. },
  84. red: {
  85. areaStyle: {
  86. color: 'rgba(255, 0, 0, 0.6)',
  87. },
  88. lineStyle: {
  89. color: "#FFFFFF",
  90. },
  91. itemStyle: {
  92. color: 'rgba(255, 0, 0, 0.9)',
  93. borderColor: "rgba(255,255,255, 0.5)",
  94. borderWidth: 0.5,
  95. },
  96. },
  97. green: {
  98. areaStyle: {
  99. color: 'rgba(0, 255, 0, 0.6)',
  100. },
  101. lineStyle: {
  102. color: "#FFFFFF",
  103. },
  104. itemStyle: {
  105. color: 'rgba(0, 255, 0, 0.9)',
  106. borderColor: "rgba(255,255,255, 0.5)",
  107. borderWidth: 0.5,
  108. },
  109. },
  110. white: {
  111. areaStyle: {
  112. color: 'rgba(153, 153, 153, 0.6)',
  113. },
  114. lineStyle: {
  115. color: "#FFFFFF",
  116. },
  117. itemStyle: {
  118. color: 'rgba(153, 153, 153, 0.9)',
  119. borderColor: "rgba(255,255,255, 0.5)",
  120. borderWidth: 0.5,
  121. },
  122. },
  123. };
  124. },
  125. methods: {
  126. renderValue() {
  127. let result = [];
  128. this.value.forEach((value, index) => {
  129. result.push({
  130. name: this.title[index],
  131. type: "radar",
  132. data: value.data,
  133. });
  134. });
  135. return result;
  136. },
  137. initChart() {
  138. let chart = echarts.init(this.$el);
  139. let maxValue = -1;
  140. this.lineStyles = [this.blue, this.orange,this.red,this.green,this.white];
  141. if (this.value.length > 0)
  142. this.value[0].data.forEach((item, index) => {
  143. item.value.forEach((value) => {
  144. if (value > maxValue) {
  145. maxValue = value;
  146. }
  147. });
  148. item.areaStyle =
  149. this.lineStyles[index % this.lineStyles.length].areaStyle;
  150. item.lineStyle =
  151. this.lineStyles[index % this.lineStyles.length].lineStyle;
  152. item.itemStyle =
  153. this.lineStyles[index % this.lineStyles.length].itemStyle;
  154. });
  155. if (this.value[1] && this.value[1].data && this.value[1].data.length> 0) {
  156. this.value[1].data.forEach((item, index) => {
  157. item.value.forEach((value) => {
  158. if (value > maxValue) {
  159. maxValue = value;
  160. }
  161. });
  162. item.areaStyle =
  163. this.lineStyles[1].areaStyle;
  164. item.lineStyle =
  165. this.lineStyles[1].lineStyle;
  166. item.itemStyle =
  167. this.lineStyles[1].itemStyle;
  168. });
  169. }
  170. if (this.value[2] && this.value[2].data && this.value[2].data.length> 0) {
  171. this.value[2].data.forEach((item, index) => {
  172. item.value.forEach((value) => {
  173. if (value > maxValue) {
  174. maxValue = value;
  175. }
  176. });
  177. item.areaStyle =
  178. this.lineStyles[2].areaStyle;
  179. item.lineStyle =
  180. this.lineStyles[2].lineStyle;
  181. item.itemStyle =
  182. this.lineStyles[2].itemStyle;
  183. });
  184. }
  185. if (this.value[3] && this.value[3].data && this.value[3].data.length> 0) {
  186. this.value[3].data.forEach((item, index) => {
  187. item.value.forEach((value) => {
  188. if (value > maxValue) {
  189. maxValue = value;
  190. }
  191. });
  192. item.areaStyle =
  193. this.lineStyles[3].areaStyle;
  194. item.lineStyle =
  195. this.lineStyles[3].lineStyle;
  196. item.itemStyle =
  197. this.lineStyles[3].itemStyle;
  198. });
  199. }
  200. if (this.value[4] && this.value[4].data && this.value[4].data.length> 0) {
  201. this.value[4].data.forEach((item, index) => {
  202. item.value.forEach((value) => {
  203. if (value > maxValue) {
  204. maxValue = value;
  205. }
  206. });
  207. item.areaStyle =
  208. this.lineStyles[4].areaStyle;
  209. item.lineStyle =
  210. this.lineStyles[4].lineStyle;
  211. item.itemStyle =
  212. this.lineStyles[4].itemStyle;
  213. });
  214. }
  215. maxValue *= 1.5;
  216. let indicator = [];
  217. if (this.value.length > 0)
  218. this.value[0].indicator.forEach((item) => {
  219. indicator.push({ name: item, max: maxValue });
  220. });
  221. let option = {
  222. grid: {
  223. left: 150,
  224. right: 150,
  225. bottom: 150,
  226. top: 150,
  227. },
  228. tooltip: {
  229. show: this.showTool,
  230. trigger: "item",
  231. backgroundColor: "rgba(0,0,0,0.4)",
  232. borderColor: "#606769",
  233. textStyle: {
  234. color: "#f8f8ff",
  235. fontSize: util.vh(16),
  236. },
  237. },
  238. legend: {
  239. show: false,
  240. bottom: 16,
  241. inactiveColor: "#606769",
  242. textStyle: {
  243. fontSize: 12,
  244. color: "#f8f8ff",
  245. },
  246. },
  247. radar: [
  248. // 最低层 80
  249. {
  250. radius: "70%",
  251. center: ["50%", "50%"],
  252. splitNumber: 1,
  253. nameGap: "16",
  254. name: {
  255. textStyle: {
  256. color: "#f8f8ff",
  257. fontSize: 12,
  258. },
  259. },
  260. axisLine: {
  261. lineStyle: {
  262. color: "#606769",
  263. },
  264. },
  265. splitLine: {
  266. lineStyle: {
  267. width: 1,
  268. color: "#606769",
  269. },
  270. },
  271. splitArea: {
  272. areaStyle: {
  273. color: "transparent",
  274. },
  275. },
  276. itemStyle: {
  277. color: "#C92F00",
  278. borderColor: "#C92F00",
  279. borderWidth: 0.5,
  280. },
  281. indicator: indicator,
  282. },
  283. // 次外层 70 - 80
  284. {
  285. radius: ["60%", "70%"],
  286. center: ["50%", "50%"],
  287. startAngle: 90,
  288. splitNumber: 2,
  289. name: {
  290. show: false,
  291. },
  292. axisLine: {
  293. lineStyle: {
  294. color: "#606769",
  295. shadowBlur: 1,
  296. shadowColor: "#fff",
  297. shadowOffsetX: 0.5,
  298. shadowOffsetY: 1,
  299. },
  300. },
  301. splitLine: {
  302. lineStyle: {
  303. width: 1,
  304. color: "#606769",
  305. shadowColor: "#fff",
  306. shadowBlur: 0,
  307. shadowOffsetX: 0.5,
  308. shadowOffsetY: 0.5,
  309. },
  310. },
  311. splitArea: {
  312. areaStyle: {
  313. color: "transparent",
  314. },
  315. },
  316. indicator: indicator,
  317. },
  318. // 渐变层 40 - 70
  319. {
  320. radius: ["35%", "60%"],
  321. center: ["50%", "50%"],
  322. splitNumber: 1,
  323. name: {
  324. show: false,
  325. },
  326. axisLine: {
  327. lineStyle: {
  328. color: "#606769",
  329. },
  330. },
  331. splitLine: {
  332. lineStyle: {
  333. width: 1,
  334. color: "#606769",
  335. },
  336. },
  337. splitArea: {
  338. areaStyle: {
  339. shadowBlur: 4,
  340. color: {
  341. type: "radial",
  342. x: 0.5,
  343. y: 0.5,
  344. r: 0.5,
  345. colorStops: [
  346. {
  347. offset: 0.5,
  348. color: "transparent", // 0% 处的颜色
  349. },
  350. {
  351. offset: 1,
  352. color: "#0E235A", // 100% 处的颜色
  353. },
  354. ],
  355. global: false, // 缺省为 false
  356. },
  357. },
  358. },
  359. indicator: indicator,
  360. },
  361. // 渐变层 0 - 40
  362. {
  363. radius: ["0%", "35%"],
  364. center: ["50%", "50%"],
  365. splitNumber: 1,
  366. name: {
  367. show: false,
  368. },
  369. axisLine: {
  370. lineStyle: {
  371. color: "#606769",
  372. },
  373. },
  374. splitLine: {
  375. lineStyle: {
  376. width: 1,
  377. color: "#606769",
  378. },
  379. },
  380. splitArea: {
  381. areaStyle: {
  382. shadowBlur: 4,
  383. color: {
  384. type: "radial",
  385. x: 0.5,
  386. y: 0.5,
  387. r: 0.5,
  388. colorStops: [
  389. {
  390. offset: 0.5,
  391. color: "transparent", // 0% 处的颜色
  392. },
  393. {
  394. offset: 1,
  395. color: "#0F245E", // 100% 处的颜色
  396. },
  397. ],
  398. global: false, // 缺省为 false
  399. },
  400. },
  401. },
  402. indicator: indicator,
  403. },
  404. // 内层 0 - 50
  405. {
  406. radius: "35%",
  407. center: ["50%", "50%"],
  408. splitNumber: 1,
  409. name: {
  410. show: false,
  411. },
  412. axisLine: {
  413. lineStyle: {
  414. color: "#606769",
  415. },
  416. },
  417. splitLine: {
  418. lineStyle: {
  419. width: 1,
  420. color: "#606769",
  421. },
  422. },
  423. splitArea: {
  424. areaStyle: {
  425. shadowBlur: 4,
  426. color: "transparent",
  427. },
  428. },
  429. indicator: indicator,
  430. },
  431. // 内层 0 - 45
  432. {
  433. radius: "35%",
  434. center: ["50%", "50%"],
  435. splitNumber: 1,
  436. name: {
  437. show: false,
  438. },
  439. axisLine: {
  440. lineStyle: {
  441. color: "#606769",
  442. },
  443. },
  444. splitLine: {
  445. lineStyle: {
  446. width: 1,
  447. color: "#606769",
  448. },
  449. },
  450. splitArea: {
  451. areaStyle: {
  452. shadowBlur: 4,
  453. color: "transparent",
  454. },
  455. },
  456. indicator: indicator,
  457. },
  458. ],
  459. series: this.renderValue(),
  460. };
  461. chart.setOption(option);
  462. },
  463. },
  464. created() {
  465. this.id = "pie-chart-" + util.newGUID();
  466. },
  467. mounted() {
  468. this.$nextTick(() => {
  469. this.$el.style.width = this.width;
  470. this.$el.style.height = this.height;
  471. this.initChart();
  472. });
  473. },
  474. updated() {
  475. this.$nextTick(() => {
  476. this.initChart();
  477. });
  478. },
  479. };
  480. </script>
  481. <style lang="less" scoped>
  482. .chart {
  483. width: 100%;
  484. height: 100%;
  485. display: block;
  486. margin: auto;
  487. }
  488. </style>