radar-chart.vue 8.2 KB

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