normal-radar-chart.vue 8.2 KB

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