normal-radar-chart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. },
  49. data() {
  50. return {
  51. id: "",
  52. chart: null,
  53. lineStyles: [],
  54. green:{
  55. areaStyle: {
  56. color: "rgba(165,228,175, 0.9)",
  57. },
  58. lineStyle: {
  59. color: "rgba(255,255,255, 0.85)",
  60. },
  61. itemStyle: {
  62. color: "rgba(165,228,175, 0.5)",
  63. borderColor: "rgba(255,255,255, 0.5)",
  64. borderWidth: 0.5,
  65. },
  66. },
  67. blue:{
  68. areaStyle: {
  69. color: 'rgba(75,85,174, 0.9)',
  70. },
  71. lineStyle: {
  72. color: "rgba(255,255,255, 0.85)",
  73. },
  74. itemStyle: {
  75. color:'rgba(75,85,174, 0.9)',
  76. borderColor: "rgba(255,255,255, 0.5)",
  77. borderWidth: 0.5,
  78. },
  79. }
  80. };
  81. },
  82. methods: {
  83. renderValue() {
  84. let result = [];
  85. this.value.forEach((value, index) => {
  86. result.push({
  87. name: this.title[index],
  88. type: "radar",
  89. data: value.data,
  90. });
  91. });
  92. return result;
  93. },
  94. initChart() {
  95. let themeName = '';
  96. let theme = this.$store.state.themeName;
  97. if(theme == 'dark' || theme == 'light'){
  98. themeName = theme;
  99. }else{
  100. themeName = theme.split(' ')[1];
  101. }
  102. let chart = echarts.init(this.$el);
  103. let maxValue = -1;
  104. if(themeName === "dark"){
  105. this.lineStyles = [this.green,this.blue];
  106. }else{
  107. this.lineStyles = [this.blue,this.green];
  108. }
  109. if (this.value.length > 0)
  110. this.value[0].data.forEach((item, index) => {
  111. item.value.forEach((value) => {
  112. if (value > maxValue) {
  113. maxValue = value;
  114. }
  115. });
  116. item.areaStyle =
  117. this.lineStyles[index % this.lineStyles.length].areaStyle;
  118. item.lineStyle =
  119. this.lineStyles[index % this.lineStyles.length].lineStyle;
  120. item.itemStyle =
  121. this.lineStyles[index % this.lineStyles.length].itemStyle;
  122. });
  123. maxValue *= 1.5;
  124. let indicator = [];
  125. if (this.value.length > 0)
  126. this.value[0].indicator.forEach((item) => {
  127. indicator.push({ name: item, max: maxValue });
  128. });
  129. let option = {
  130. grid: {
  131. left: 150,
  132. right: 150,
  133. bottom: 150,
  134. top: 150,
  135. },
  136. tooltip: {
  137. trigger: "item",
  138. backgroundColor:
  139. themeName === "dark"
  140. ? "rgba(0,0,0,0.4)"
  141. : "rgba(255,255,255,0.5)",
  142. borderColor:
  143. themeName === "dark"
  144. ? partten.getColor("gray")
  145. : "#000",
  146. textStyle: {
  147. color: themeName === "dark" ? "#fff" : "#000",
  148. fontSize: util.vh(16),
  149. },
  150. },
  151. legend: {
  152. show: this.showLegend,
  153. bottom: 16,
  154. inactiveColor:
  155. themeName === "dark"
  156. ? partten.getColor("gray")
  157. : "#000",
  158. textStyle: {
  159. fontSize: 12,
  160. color:
  161. themeName === "dark"
  162. ? partten.getColor("grayl")
  163. : "#000",
  164. },
  165. },
  166. radar: [
  167. // 最低层 80
  168. {
  169. radius: "70%",
  170. center: ["50%", "50%"],
  171. splitNumber: 1,
  172. nameGap: "16",
  173. name: {
  174. textStyle: {
  175. color: themeName === "dark"
  176. ? partten.getColor("gray") + 99
  177. : "#000",
  178. fontSize: 12,
  179. },
  180. },
  181. axisLine: {
  182. lineStyle: {
  183. color: themeName === "dark"
  184. ? partten.getColor("gray") + 40
  185. : "#000" + 40,
  186. },
  187. },
  188. splitLine: {
  189. lineStyle: {
  190. width: 1,
  191. color: themeName === "dark"
  192. ? partten.getColor("gray") + 40
  193. : "#000" + 40,
  194. },
  195. },
  196. splitArea: {
  197. areaStyle: {
  198. color: "transparent",
  199. },
  200. },
  201. indicator: indicator,
  202. },
  203. // 次外层 70 - 80
  204. {
  205. radius: ["60%", "70%"],
  206. center: ["50%", "50%"],
  207. startAngle: 90,
  208. splitNumber: 2,
  209. name: {
  210. show: false,
  211. },
  212. axisLine: {
  213. lineStyle: {
  214. color: themeName === "dark"
  215. ? partten.getColor("gray") + 40
  216. : "#000" + 40,
  217. shadowBlur: 1,
  218. shadowColor: "#fff",
  219. shadowOffsetX: 0.5,
  220. shadowOffsetY: 1,
  221. },
  222. },
  223. splitLine: {
  224. lineStyle: {
  225. width: 1,
  226. color: themeName === "dark"
  227. ? partten.getColor("gray") + 40
  228. : "#000" + 40,
  229. shadowColor: "#fff",
  230. shadowBlur: 0,
  231. shadowOffsetX: 0.5,
  232. shadowOffsetY: 0.5,
  233. },
  234. },
  235. splitArea: {
  236. areaStyle: {
  237. color: "transparent",
  238. },
  239. },
  240. indicator: indicator,
  241. },
  242. // 渐变层 40 - 70
  243. {
  244. radius: ["35%", "60%"],
  245. center: ["50%", "50%"],
  246. splitNumber: 1,
  247. name: {
  248. show: false,
  249. },
  250. axisLine: {
  251. lineStyle: {
  252. color: themeName === "dark"
  253. ? partten.getColor("gray") + 40
  254. : "#000" + 40,
  255. },
  256. },
  257. splitLine: {
  258. lineStyle: {
  259. width: 1,
  260. color: themeName === "dark"
  261. ? partten.getColor("gray")
  262. : "#000",
  263. },
  264. },
  265. splitArea: {
  266. areaStyle: {
  267. shadowBlur: 4,
  268. color: {
  269. type: "radial",
  270. x: 0.5,
  271. y: 0.5,
  272. r: 0.5,
  273. colorStops: [
  274. {
  275. offset: 0.5,
  276. color: "transparent", // 0% 处的颜色
  277. },
  278. {
  279. offset: 1,
  280. color: themeName === "dark"
  281. ? partten.getColor("green") + 60
  282. : partten.getColor("deepblue") + 60, // 100% 处的颜色
  283. },
  284. ],
  285. global: false, // 缺省为 false
  286. },
  287. },
  288. },
  289. indicator: indicator,
  290. },
  291. // 渐变层 0 - 40
  292. {
  293. radius: ["0%", "35%"],
  294. center: ["50%", "50%"],
  295. splitNumber: 1,
  296. name: {
  297. show: false,
  298. },
  299. axisLine: {
  300. lineStyle: {
  301. color: themeName === "dark"
  302. ? partten.getColor("gray") + 40
  303. : "#000" + 40,
  304. },
  305. },
  306. splitLine: {
  307. lineStyle: {
  308. width: 1,
  309. color: themeName === "dark"
  310. ? partten.getColor("gray")
  311. : "#000",
  312. },
  313. },
  314. splitArea: {
  315. areaStyle: {
  316. shadowBlur: 4,
  317. color: {
  318. type: "radial",
  319. x: 0.5,
  320. y: 0.5,
  321. r: 0.5,
  322. colorStops: [
  323. {
  324. offset: 0.5,
  325. color: "transparent", // 0% 处的颜色
  326. },
  327. {
  328. offset: 1,
  329. color: themeName === "dark"
  330. ? partten.getColor("green") + 60
  331. : partten.getColor("deepblue") + 60, // 100% 处的颜色
  332. },
  333. ],
  334. global: false, // 缺省为 false
  335. },
  336. },
  337. },
  338. indicator: indicator,
  339. },
  340. // 内层 0 - 50
  341. {
  342. radius: "35%",
  343. center: ["50%", "50%"],
  344. splitNumber: 1,
  345. name: {
  346. show: false,
  347. },
  348. axisLine: {
  349. lineStyle: {
  350. color: themeName === "dark"
  351. ? partten.getColor("gray") + 40
  352. : "#000" + 40,
  353. },
  354. },
  355. splitLine: {
  356. lineStyle: {
  357. width: 1,
  358. color: themeName === "dark"
  359. ? partten.getColor("gray")
  360. : "#000",
  361. },
  362. },
  363. splitArea: {
  364. areaStyle: {
  365. shadowBlur: 4,
  366. color: "transparent",
  367. },
  368. },
  369. indicator: indicator,
  370. },
  371. // 内层 0 - 45
  372. {
  373. radius: "35%",
  374. center: ["50%", "50%"],
  375. splitNumber: 1,
  376. name: {
  377. show: false,
  378. },
  379. axisLine: {
  380. lineStyle: {
  381. color: themeName === "dark"
  382. ? partten.getColor("gray") + 40
  383. : "#000" + 40,
  384. },
  385. },
  386. splitLine: {
  387. lineStyle: {
  388. width: 1,
  389. color: themeName === "dark"
  390. ? partten.getColor("gray")
  391. : "#000",
  392. },
  393. },
  394. splitArea: {
  395. areaStyle: {
  396. shadowBlur: 4,
  397. color: "transparent",
  398. },
  399. },
  400. indicator: indicator,
  401. },
  402. ],
  403. series: this.renderValue(),
  404. };
  405. chart.setOption(option);
  406. },
  407. },
  408. created() {
  409. this.id = "pie-chart-" + util.newGUID();
  410. },
  411. mounted() {
  412. this.$nextTick(() => {
  413. this.$el.style.width = this.width;
  414. this.$el.style.height = this.height;
  415. this.initChart();
  416. });
  417. },
  418. updated() {
  419. this.$nextTick(() => {
  420. this.initChart();
  421. });
  422. },
  423. watch: {
  424. "$store.state.themeName"() {
  425. this.initChart();
  426. },
  427. },
  428. };
  429. </script>
  430. <style lang="less" scoped>
  431. .chart {
  432. width: 100%;
  433. height: 100%;
  434. display: block;
  435. margin: auto;
  436. }
  437. </style>