radar-chart.vue 9.6 KB

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