normal-radar-chart.vue 9.0 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: 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: "风机一号",
  38. },
  39. ],
  40. },
  41. ];
  42. },
  43. },
  44. showLegend: {
  45. type: Boolean,
  46. default: true,
  47. },
  48. },
  49. data() {
  50. return {
  51. id: "",
  52. chart: null,
  53. lineStyles: [
  54. {
  55. areaStyle: {
  56. color: "rgba(75,85,174, 0.9)",
  57. },
  58. lineStyle: {
  59. color: "rgba(255,255,255, 0.85)",
  60. },
  61. itemStyle: {
  62. color: "rgba(75,85,174, 0.5)",
  63. borderColor: "rgba(255,255,255, 0.5)",
  64. borderWidth: 0.5,
  65. },
  66. },
  67. ],
  68. };
  69. },
  70. computed: {
  71. series() {
  72. let result = [];
  73. this.value.forEach((value, index) => {
  74. result.push({
  75. name: this.title,
  76. type: "radar",
  77. data: value.data,
  78. });
  79. });
  80. return result;
  81. },
  82. },
  83. methods: {
  84. initChart() {
  85. let chart = echarts.init(this.$el);
  86. let maxValue = -1;
  87. if (this.value.length > 0)
  88. this.value[0].data.forEach((item, index) => {
  89. item.value.forEach((value) => {
  90. if (value > maxValue) {
  91. maxValue = value;
  92. }
  93. });
  94. item.areaStyle = this.lineStyles[index % this.lineStyles.length].areaStyle;
  95. item.lineStyle = this.lineStyles[index % this.lineStyles.length].lineStyle;
  96. item.itemStyle = this.lineStyles[index % this.lineStyles.length].itemStyle;
  97. });
  98. maxValue *= 1.5;
  99. let indicator = [];
  100. if (this.value.length > 0)
  101. this.value[0].indicator.forEach((item) => {
  102. indicator.push({ name: item, max: maxValue });
  103. });
  104. let option = {
  105. grid: {
  106. left: 150,
  107. right: 150,
  108. bottom: 150,
  109. top: 150,
  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. legend: {
  121. show: this.showLegend,
  122. bottom: 16,
  123. inactiveColor: partten.getColor("gray"),
  124. textStyle: {
  125. color: partten.getColor("grayl"),
  126. fontSize: 12,
  127. },
  128. },
  129. radar: [
  130. // 最低层 80
  131. {
  132. radius: "70%",
  133. center: ["50%", "50%"],
  134. splitNumber: 1,
  135. nameGap: "16",
  136. name: {
  137. textStyle: {
  138. color: partten.getColor("gray") + 99,
  139. fontSize: 12,
  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. // 次外层 70 - 80
  161. {
  162. radius: ["60%", "70%"],
  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 - 70
  196. {
  197. radius: ["35%", "60%"],
  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%", "35%"],
  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: "35%",
  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: this.series,
  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. }
  365. </style>