normal-radar-chart.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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: String,
  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. },
  45. data() {
  46. return {
  47. id: "",
  48. chart: null,
  49. lineStyles: [
  50. {
  51. areaStyle: {
  52. color: "rgba(75,85,174, 0.9)",
  53. },
  54. lineStyle: {
  55. color: "rgba(255,255,255, 0.85)",
  56. },
  57. itemStyle: {
  58. color: "rgba(75,85,174, 0.5)",
  59. borderColor: "rgba(255,255,255, 0.5)",
  60. borderWidth: 0.5,
  61. },
  62. },
  63. ],
  64. };
  65. },
  66. computed: {
  67. series() {
  68. let result = [];
  69. this.value.forEach((value, index) => {
  70. result.push({
  71. name: this.title,
  72. type: "radar",
  73. data: value.data,
  74. });
  75. });
  76. return result;
  77. },
  78. },
  79. methods: {
  80. initChart() {
  81. let chart = echarts.init(this.$el);
  82. let maxValue = -1;
  83. this.value[0].data.forEach((item, index) => {
  84. item.value.forEach((value) => {
  85. if (value > maxValue) {
  86. maxValue = value;
  87. }
  88. });
  89. item.areaStyle = this.lineStyles[index % this.lineStyles.length].areaStyle;
  90. item.lineStyle = this.lineStyles[index % this.lineStyles.length].lineStyle;
  91. item.itemStyle = this.lineStyles[index % this.lineStyles.length].itemStyle;
  92. });
  93. maxValue *= 1.5;
  94. let indicator = [];
  95. this.value[0].indicator.forEach((item) => {
  96. indicator.push({ name: item, max: maxValue });
  97. });
  98. let option = {
  99. grid: {
  100. left: 150,
  101. right: 150,
  102. bottom: 150,
  103. top: 150,
  104. },
  105. tooltip: {
  106. trigger: "item",
  107. },
  108. radar: [
  109. // 最低层 80
  110. {
  111. radius: "70%",
  112. center: ["50%", "50%"],
  113. splitNumber: 1,
  114. nameGap: "16",
  115. name: {
  116. textStyle: {
  117. color: partten.getColor("gray") + 99,
  118. fontSize: 12,
  119. },
  120. },
  121. axisLine: {
  122. lineStyle: {
  123. color: partten.getColor("gray") + 40,
  124. },
  125. },
  126. splitLine: {
  127. lineStyle: {
  128. width: 1,
  129. color: partten.getColor("gray") + 40,
  130. },
  131. },
  132. splitArea: {
  133. areaStyle: {
  134. color: "transparent",
  135. },
  136. },
  137. indicator: indicator,
  138. },
  139. // 次外层 70 - 80
  140. {
  141. radius: ["60%", "70%"],
  142. center: ["50%", "50%"],
  143. startAngle: 90,
  144. splitNumber: 2,
  145. name: {
  146. show: false,
  147. },
  148. axisLine: {
  149. lineStyle: {
  150. color: partten.getColor("gray") + 40,
  151. shadowBlur: 1,
  152. shadowColor: "#fff",
  153. shadowOffsetX: 0.5,
  154. shadowOffsetY: 1,
  155. },
  156. },
  157. splitLine: {
  158. lineStyle: {
  159. width: 1,
  160. color: partten.getColor("gray") + 40,
  161. shadowColor: "#fff",
  162. shadowBlur: 0,
  163. shadowOffsetX: 0.5,
  164. shadowOffsetY: 0.5,
  165. },
  166. },
  167. splitArea: {
  168. areaStyle: {
  169. color: "transparent",
  170. },
  171. },
  172. indicator: indicator,
  173. },
  174. // 渐变层 40 - 70
  175. {
  176. radius: ["35%", "60%"],
  177. center: ["50%", "50%"],
  178. splitNumber: 1,
  179. name: {
  180. show: false,
  181. },
  182. axisLine: {
  183. lineStyle: {
  184. color: partten.getColor("gray") + 40,
  185. },
  186. },
  187. splitLine: {
  188. lineStyle: {
  189. width: 1,
  190. color: partten.getColor("gray"),
  191. },
  192. },
  193. splitArea: {
  194. areaStyle: {
  195. shadowBlur: 4,
  196. color: {
  197. type: "radial",
  198. x: 0.5,
  199. y: 0.5,
  200. r: 0.5,
  201. colorStops: [
  202. {
  203. offset: 0.5,
  204. color: "transparent", // 0% 处的颜色
  205. },
  206. {
  207. offset: 1,
  208. color: partten.getColor("green") + 60, // 100% 处的颜色
  209. },
  210. ],
  211. global: false, // 缺省为 false
  212. },
  213. },
  214. },
  215. indicator: indicator,
  216. },
  217. // 渐变层 0 - 40
  218. {
  219. radius: ["0%", "35%"],
  220. center: ["50%", "50%"],
  221. splitNumber: 1,
  222. name: {
  223. show: false,
  224. },
  225. axisLine: {
  226. lineStyle: {
  227. color: partten.getColor("gray") + 40,
  228. },
  229. },
  230. splitLine: {
  231. lineStyle: {
  232. width: 1,
  233. color: partten.getColor("gray"),
  234. },
  235. },
  236. splitArea: {
  237. areaStyle: {
  238. shadowBlur: 4,
  239. color: {
  240. type: "radial",
  241. x: 0.5,
  242. y: 0.5,
  243. r: 0.5,
  244. colorStops: [
  245. {
  246. offset: 0.5,
  247. color: "transparent", // 0% 处的颜色
  248. },
  249. {
  250. offset: 1,
  251. color: partten.getColor("green") + 60, // 100% 处的颜色
  252. },
  253. ],
  254. global: false, // 缺省为 false
  255. },
  256. },
  257. },
  258. indicator: indicator,
  259. },
  260. // 内层 0 - 50
  261. {
  262. radius: "35%",
  263. center: ["50%", "50%"],
  264. splitNumber: 1,
  265. name: {
  266. show: false,
  267. },
  268. axisLine: {
  269. lineStyle: {
  270. color: partten.getColor("gray") + 40,
  271. },
  272. },
  273. splitLine: {
  274. lineStyle: {
  275. width: 1,
  276. color: partten.getColor("gray"),
  277. },
  278. },
  279. splitArea: {
  280. areaStyle: {
  281. shadowBlur: 4,
  282. color: "transparent",
  283. },
  284. },
  285. indicator: indicator,
  286. },
  287. // 内层 0 - 45
  288. {
  289. radius: "35%",
  290. center: ["50%", "50%"],
  291. splitNumber: 1,
  292. name: {
  293. show: false,
  294. },
  295. axisLine: {
  296. lineStyle: {
  297. color: partten.getColor("gray") + 40,
  298. },
  299. },
  300. splitLine: {
  301. lineStyle: {
  302. width: 1,
  303. color: partten.getColor("gray"),
  304. },
  305. },
  306. splitArea: {
  307. areaStyle: {
  308. shadowBlur: 4,
  309. color: "transparent",
  310. },
  311. },
  312. indicator: indicator,
  313. },
  314. ],
  315. series: this.series,
  316. };
  317. chart.setOption(option);
  318. },
  319. },
  320. created() {
  321. this.id = "pie-chart-" + util.newGUID();
  322. },
  323. mounted() {
  324. this.$nextTick(() => {
  325. this.$el.style.width = this.width;
  326. this.$el.style.height = this.height;
  327. this.initChart();
  328. });
  329. },
  330. updated() {
  331. this.$nextTick(() => {
  332. this.initChart();
  333. });
  334. },
  335. };
  336. </script>
  337. <style lang="less" scoped>
  338. .chart {
  339. width: 100%;
  340. height: 100%;
  341. display: block;
  342. margin: auto;
  343. }
  344. </style>