radar-chart.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. green:{
  66. areaStyle: {
  67. color: "rgba(165,228,175, 0.9)",
  68. },
  69. lineStyle: {
  70. color: "rgba(255,255,255, 0.85)",
  71. },
  72. itemStyle: {
  73. color: "rgba(165,228,175, 0.5)",
  74. borderColor: "rgba(255,255,255, 0.5)",
  75. borderWidth: 0.5,
  76. },
  77. },
  78. blue:{
  79. areaStyle: {
  80. color: 'rgba(75,85,174, 0.9)',
  81. },
  82. lineStyle: {
  83. color: "rgba(255,255,255, 0.85)",
  84. },
  85. itemStyle: {
  86. color:'rgba(75,85,174, 0.9)',
  87. borderColor: "rgba(255,255,255, 0.5)",
  88. borderWidth: 0.5,
  89. },
  90. }
  91. };
  92. },
  93. computed: {},
  94. methods: {
  95. initChart() {
  96. let themeName = '';
  97. let theme = this.$store.state.themeName;
  98. if(theme == 'dark' || theme == 'light'){
  99. themeName = theme;
  100. }else{
  101. themeName = theme.split(' ')[1];
  102. }
  103. let chart = echarts.init(this.$el);
  104. let maxValue = -1;
  105. if(themeName === "dark"){
  106. this.lineStyles = [this.green,this.blue];
  107. }else{
  108. this.lineStyles = [this.blue,this.green];
  109. }
  110. if (this.value.data)
  111. this.value.data.forEach((item, index) => {
  112. item.value.forEach((value) => {
  113. if (value > maxValue) {
  114. maxValue = value;
  115. }
  116. });
  117. item.areaStyle = this.lineStyles[index % this.lineStyles.length].areaStyle;
  118. item.lineStyle = this.lineStyles[index % this.lineStyles.length].lineStyle;
  119. item.itemStyle = this.lineStyles[index % this.lineStyles.length].itemStyle;
  120. });
  121. maxValue *= 1.5;
  122. let indicator = [];
  123. if (this.value.indicator)
  124. this.value.indicator.forEach((item) => {
  125. indicator.push({ name: item, max: maxValue });
  126. });
  127. let baseSize = 65;
  128. let option = {
  129. grid: {
  130. left: 0,
  131. right: 0,
  132. bottom: 0,
  133. top: 0,
  134. },
  135. tooltip: {
  136. trigger: "item",
  137. backgroundColor: this.$store.state.themeName === "dark" ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
  138. borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  139. textStyle: {
  140. color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
  141. fontSize: util.vh(16),
  142. },
  143. position: function(pos, params, dom, rect, size) {
  144. // 鼠标在左侧时 tooltip 显示到右侧,鼠标在右侧时 tooltip 显示到左侧。
  145. var obj = { top: 60 };
  146. obj[["left", "right"][+(pos[0] < size.viewSize[0] / 2)]] = 5;
  147. return obj;
  148. },
  149. extraCssText: "position: absolute; margin-top:-20%;",
  150. // extraCssText: "max-width:140px;position:sticky;top:0;left:0"
  151. },
  152. radar: [
  153. // 最低层 90
  154. {
  155. radius: baseSize + "%",
  156. center: ["50%", "50%"],
  157. splitNumber: 1,
  158. nameGap: "4",
  159. name: {
  160. textStyle: {
  161. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  162. fontSize: 12,
  163. padding: [0, 16],
  164. },
  165. },
  166. axisLine: {
  167. lineStyle: {
  168. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  169. },
  170. },
  171. splitLine: {
  172. lineStyle: {
  173. width: 1,
  174. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  175. },
  176. },
  177. splitArea: {
  178. areaStyle: {
  179. color: "transparent",
  180. },
  181. },
  182. indicator: indicator,
  183. },
  184. // 次外层 80 - 90
  185. {
  186. radius: ["55%", "65%"],
  187. center: ["50%", "50%"],
  188. startAngle: 90,
  189. splitNumber: 2,
  190. name: {
  191. show: false,
  192. },
  193. axisLine: {
  194. lineStyle: {
  195. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  196. shadowBlur: 1,
  197. shadowColor: "#fff",
  198. shadowOffsetX: 0.5,
  199. shadowOffsetY: 1,
  200. },
  201. },
  202. splitLine: {
  203. lineStyle: {
  204. width: 1,
  205. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  206. shadowColor: "#fff",
  207. shadowBlur: 0,
  208. shadowOffsetX: 0.5,
  209. shadowOffsetY: 0.5,
  210. },
  211. },
  212. splitArea: {
  213. areaStyle: {
  214. color: "transparent",
  215. },
  216. },
  217. indicator: indicator,
  218. },
  219. // 渐变层 40 - 80
  220. {
  221. radius: ["30%", "55%"],
  222. center: ["50%", "50%"],
  223. splitNumber: 1,
  224. name: {
  225. show: false,
  226. },
  227. axisLine: {
  228. lineStyle: {
  229. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  230. },
  231. },
  232. splitLine: {
  233. lineStyle: {
  234. width: 1,
  235. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  236. },
  237. },
  238. splitArea: {
  239. areaStyle: {
  240. shadowBlur: 4,
  241. color: {
  242. type: "radial",
  243. x: 0.5,
  244. y: 0.5,
  245. r: 0.5,
  246. colorStops: [
  247. {
  248. offset: 0.5,
  249. color: "transparent", // 0% 处的颜色
  250. },
  251. {
  252. offset: 1,
  253. color: this.$store.state.themeName === "dark" ? partten.getColor("green") + 60 : partten.getColor("deepblue") + 60, // 100% 处的颜色
  254. },
  255. ],
  256. global: false, // 缺省为 false
  257. },
  258. },
  259. },
  260. indicator: indicator,
  261. },
  262. // 渐变层 0 - 40
  263. {
  264. radius: ["0%", "30%"],
  265. center: ["50%", "50%"],
  266. splitNumber: 1,
  267. name: {
  268. show: false,
  269. },
  270. axisLine: {
  271. lineStyle: {
  272. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  273. },
  274. },
  275. splitLine: {
  276. lineStyle: {
  277. width: 1,
  278. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  279. },
  280. },
  281. splitArea: {
  282. areaStyle: {
  283. shadowBlur: 4,
  284. color: {
  285. type: "radial",
  286. x: 0.5,
  287. y: 0.5,
  288. r: 0.5,
  289. colorStops: [
  290. {
  291. offset: 0.5,
  292. color: "transparent", // 0% 处的颜色
  293. },
  294. {
  295. offset: 1,
  296. color: this.$store.state.themeName === "dark" ? partten.getColor("green") + 60 : partten.getColor("deepblue") + 60, // 100% 处的颜色
  297. },
  298. ],
  299. global: false, // 缺省为 false
  300. },
  301. },
  302. },
  303. indicator: indicator,
  304. },
  305. // 内层 0 - 50
  306. {
  307. radius: "50%",
  308. center: ["50%", "50%"],
  309. splitNumber: 1,
  310. name: {
  311. show: false,
  312. },
  313. axisLine: {
  314. lineStyle: {
  315. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  316. },
  317. },
  318. splitLine: {
  319. lineStyle: {
  320. width: 1,
  321. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  322. },
  323. },
  324. splitArea: {
  325. areaStyle: {
  326. shadowBlur: 4,
  327. color: "transparent",
  328. },
  329. },
  330. indicator: indicator,
  331. },
  332. // 内层 0 - 45
  333. {
  334. radius: "35%",
  335. center: ["50%", "50%"],
  336. splitNumber: 1,
  337. name: {
  338. show: false,
  339. },
  340. axisLine: {
  341. lineStyle: {
  342. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") + 40 : "#000" + 40,
  343. },
  344. },
  345. splitLine: {
  346. lineStyle: {
  347. width: 1,
  348. color: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
  349. },
  350. },
  351. splitArea: {
  352. areaStyle: {
  353. shadowBlur: 4,
  354. color: "transparent",
  355. },
  356. },
  357. indicator: indicator,
  358. },
  359. ],
  360. series: [
  361. {
  362. name: this.title,
  363. type: "radar",
  364. data: this.value.data,
  365. },
  366. ],
  367. };
  368. chart.setOption(option);
  369. },
  370. },
  371. created() {
  372. this.id = "pie-chart-" + util.newGUID();
  373. },
  374. mounted() {
  375. this.$nextTick(() => {
  376. this.$el.style.width = this.width;
  377. this.$el.style.height = this.height;
  378. this.initChart();
  379. });
  380. },
  381. updated() {
  382. this.$nextTick(() => {
  383. this.initChart();
  384. });
  385. },
  386. watch: {
  387. "$store.state.themeName"() {
  388. this.initChart();
  389. },
  390. },
  391. };
  392. </script>
  393. <style lang="less" scoped>
  394. .chart {
  395. width: 100%;
  396. height: 100%;
  397. display: block;
  398. margin: auto;
  399. position: relative;
  400. }
  401. </style>