radar-chart.vue 9.2 KB

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