radar-chart.vue 8.5 KB

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