raderswithhart.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div
  3. :id="chartId"
  4. :style="{ height: props.height, width: props.width }"
  5. ></div>
  6. </template>
  7. <script setup>
  8. import util from "@tools/util";
  9. import chartTheme from "./chartTheme.json";
  10. import {
  11. ref,
  12. toRaw,
  13. computed,
  14. onMounted,
  15. watch,
  16. nextTick,
  17. defineProps,
  18. onUpdated,
  19. } from "vue";
  20. import * as echarts from "echarts";
  21. const chartId = "chart-" + util.newGUID(); //chartId
  22. const chartIns = ref(null); //chart 实例
  23. const props = defineProps({
  24. xAxis: {
  25. type: Object,
  26. required: true,
  27. default: () => ({}),
  28. },
  29. series: {
  30. type: Array,
  31. required: true,
  32. default: () => [],
  33. },
  34. lenged: {
  35. type: Array,
  36. required: true,
  37. default: () => [],
  38. },
  39. height: {
  40. type: String,
  41. required: false,
  42. default: "500px",
  43. },
  44. width: {
  45. type: String,
  46. required: false,
  47. default: "500px",
  48. },
  49. title: {
  50. type: String,
  51. required: false,
  52. },
  53. subtext: {
  54. type: String,
  55. required: false,
  56. },
  57. isRadar: {
  58. //是否显示雷达图
  59. type: Boolean,
  60. required: false,
  61. default: false,
  62. },
  63. });
  64. /**定义option */
  65. const option = computed({
  66. get() {
  67. return {
  68. // title: {
  69. // text: props.title || "",
  70. // subtext: props.subtext || "",
  71. // top: 10,
  72. // left: 30,
  73. // },
  74. angleAxis: props.xAxis || {},
  75. radiusAxis: {},
  76. radar: props.isRadar
  77. ? [
  78. //雷达图设定区域
  79. {
  80. indicator:
  81. props.xAxis.map((o) => {
  82. return {
  83. name: o,
  84. };
  85. }) || [],
  86. center: ["60%", "50%"],
  87. radius: "70%",
  88. splitLine: {
  89. show: false,
  90. },
  91. splitArea: {
  92. show: false,
  93. },
  94. },
  95. ]
  96. : {},
  97. polar: {
  98. radius: "70%",
  99. center: ["60%", "50%"],
  100. },
  101. tooltip: {
  102. formatter: (params) => {
  103. return params.componentSubType === "radar"
  104. ? `${params.name}`
  105. : `${params.seriesName}<br/>${
  106. // params.value > 1 ? "频次:" + params.value : ""
  107. params.value > 1
  108. ? props.title + ":" + params.value + props.subtext
  109. : ""
  110. }`;
  111. },
  112. confine: true,
  113. },
  114. series: props.series.length ? props.series : [],
  115. legend: {
  116. show: true,
  117. orient: "vertical",
  118. left: 30,
  119. // itemWidth: 16,
  120. // itemHeight: 10,
  121. textStyle: {
  122. // fontSize: util.vh(10)
  123. },
  124. top: "middle",
  125. data: props.lenged,
  126. },
  127. };
  128. },
  129. set(val) {},
  130. });
  131. watch(
  132. () => option,
  133. (newVal, oldVal) => {
  134. if (chartIns.value) {
  135. const echartIns = toRaw(chartIns.value);
  136. echartIns.clear();
  137. echartIns.setOption(newVal.value);
  138. }
  139. },
  140. { deep: true }
  141. );
  142. onMounted(() => {
  143. nextTick(() => {
  144. echarts.registerTheme("chartTheme", chartTheme);
  145. const echartIns = echarts.init(
  146. document.getElementById(chartId),
  147. "chartTheme"
  148. );
  149. chartIns.value = echartIns;
  150. echartIns.setOption(option.value);
  151. window.addEventListener("resize", () => {
  152. echartIns.resize();
  153. });
  154. });
  155. });
  156. </script>