radar-chart.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. extraCssText: "position: absolute; margin-top:50%;"
  120. // extraCssText: "max-width:140px;position:sticky;top:0;left:0"
  121. },
  122. radar: [
  123. // 最低层 90
  124. {
  125. radius: baseSize + "%",
  126. center: ["50%", "50%"],
  127. splitNumber: 1,
  128. nameGap: "4",
  129. name: {
  130. textStyle: {
  131. color: partten.getColor("gray"),
  132. fontSize: 12,
  133. padding: [0, 16],
  134. },
  135. },
  136. axisLine: {
  137. lineStyle: {
  138. color: partten.getColor("gray") + 40,
  139. },
  140. },
  141. splitLine: {
  142. lineStyle: {
  143. width: 1,
  144. color: partten.getColor("gray") + 40,
  145. },
  146. },
  147. splitArea: {
  148. areaStyle: {
  149. color: "transparent",
  150. },
  151. },
  152. indicator: indicator,
  153. },
  154. // 次外层 80 - 90
  155. {
  156. radius: ["55%", "65%"],
  157. center: ["50%", "50%"],
  158. startAngle: 90,
  159. splitNumber: 2,
  160. name: {
  161. show: false,
  162. },
  163. axisLine: {
  164. lineStyle: {
  165. color: partten.getColor("gray") + 40,
  166. shadowBlur: 1,
  167. shadowColor: "#fff",
  168. shadowOffsetX: 0.5,
  169. shadowOffsetY: 1,
  170. },
  171. },
  172. splitLine: {
  173. lineStyle: {
  174. width: 1,
  175. color: partten.getColor("gray") + 40,
  176. shadowColor: "#fff",
  177. shadowBlur: 0,
  178. shadowOffsetX: 0.5,
  179. shadowOffsetY: 0.5,
  180. },
  181. },
  182. splitArea: {
  183. areaStyle: {
  184. color: "transparent",
  185. },
  186. },
  187. indicator: indicator,
  188. },
  189. // 渐变层 40 - 80
  190. {
  191. radius: ["30%", "55%"],
  192. center: ["50%", "50%"],
  193. splitNumber: 1,
  194. name: {
  195. show: false,
  196. },
  197. axisLine: {
  198. lineStyle: {
  199. color: partten.getColor("gray") + 40,
  200. },
  201. },
  202. splitLine: {
  203. lineStyle: {
  204. width: 1,
  205. color: partten.getColor("gray"),
  206. },
  207. },
  208. splitArea: {
  209. areaStyle: {
  210. shadowBlur: 4,
  211. color: {
  212. type: "radial",
  213. x: 0.5,
  214. y: 0.5,
  215. r: 0.5,
  216. colorStops: [
  217. {
  218. offset: 0.5,
  219. color: "transparent", // 0% 处的颜色
  220. },
  221. {
  222. offset: 1,
  223. color: partten.getColor("green") + 60, // 100% 处的颜色
  224. },
  225. ],
  226. global: false, // 缺省为 false
  227. },
  228. },
  229. },
  230. indicator: indicator,
  231. },
  232. // 渐变层 0 - 40
  233. {
  234. radius: ["0%", "30%"],
  235. center: ["50%", "50%"],
  236. splitNumber: 1,
  237. name: {
  238. show: false,
  239. },
  240. axisLine: {
  241. lineStyle: {
  242. color: partten.getColor("gray") + 40,
  243. },
  244. },
  245. splitLine: {
  246. lineStyle: {
  247. width: 1,
  248. color: partten.getColor("gray"),
  249. },
  250. },
  251. splitArea: {
  252. areaStyle: {
  253. shadowBlur: 4,
  254. color: {
  255. type: "radial",
  256. x: 0.5,
  257. y: 0.5,
  258. r: 0.5,
  259. colorStops: [
  260. {
  261. offset: 0.5,
  262. color: "transparent", // 0% 处的颜色
  263. },
  264. {
  265. offset: 1,
  266. color: partten.getColor("green") + 60, // 100% 处的颜色
  267. },
  268. ],
  269. global: false, // 缺省为 false
  270. },
  271. },
  272. },
  273. indicator: indicator,
  274. },
  275. // 内层 0 - 50
  276. {
  277. radius: "50%",
  278. center: ["50%", "50%"],
  279. splitNumber: 1,
  280. name: {
  281. show: false,
  282. },
  283. axisLine: {
  284. lineStyle: {
  285. color: partten.getColor("gray") + 40,
  286. },
  287. },
  288. splitLine: {
  289. lineStyle: {
  290. width: 1,
  291. color: partten.getColor("gray"),
  292. },
  293. },
  294. splitArea: {
  295. areaStyle: {
  296. shadowBlur: 4,
  297. color: "transparent",
  298. },
  299. },
  300. indicator: indicator,
  301. },
  302. // 内层 0 - 45
  303. {
  304. radius: "35%",
  305. center: ["50%", "50%"],
  306. splitNumber: 1,
  307. name: {
  308. show: false,
  309. },
  310. axisLine: {
  311. lineStyle: {
  312. color: partten.getColor("gray") + 40,
  313. },
  314. },
  315. splitLine: {
  316. lineStyle: {
  317. width: 1,
  318. color: partten.getColor("gray"),
  319. },
  320. },
  321. splitArea: {
  322. areaStyle: {
  323. shadowBlur: 4,
  324. color: "transparent",
  325. },
  326. },
  327. indicator: indicator,
  328. },
  329. ],
  330. series: [
  331. {
  332. name: this.title,
  333. type: "radar",
  334. data: this.value.data,
  335. },
  336. ],
  337. };
  338. chart.setOption(option);
  339. },
  340. },
  341. created() {
  342. this.id = "pie-chart-" + util.newGUID();
  343. },
  344. mounted() {
  345. this.$nextTick(() => {
  346. this.$el.style.width = this.width;
  347. this.$el.style.height = this.height;
  348. this.initChart();
  349. });
  350. },
  351. updated() {
  352. this.$nextTick(() => {
  353. this.initChart();
  354. });
  355. },
  356. };
  357. </script>
  358. <style lang="less" scoped>
  359. .chart {
  360. width: 100%;
  361. height: 100%;
  362. display: block;
  363. margin: auto;
  364. position: relative;
  365. }
  366. </style>