scatterSingleChart.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <script setup name="scatterSingleChart">
  2. import util from "@tools/util";
  3. import chartTheme from "./../rateAnalysis.json";
  4. import {
  5. ref,
  6. toRaw,
  7. computed,
  8. onMounted,
  9. watch,
  10. nextTick,
  11. defineProps,
  12. } from "vue";
  13. import { useStore } from "vuex";
  14. import * as echarts from "echarts";
  15. const chartId = "chart-" + util.newGUID(); //chartId
  16. const chartIns = ref(null); //chart 实例
  17. const props = defineProps({
  18. xAxis: {
  19. type: Array,
  20. required: true,
  21. default: () => [],
  22. },
  23. yAxis: {
  24. type: Array,
  25. required: true,
  26. default: () => [],
  27. },
  28. series: {
  29. type: Array,
  30. required: false,
  31. default: () => [],
  32. },
  33. height: {
  34. type: String,
  35. required: false,
  36. default: "500px",
  37. },
  38. title: {
  39. type: String,
  40. required: false,
  41. default: "",
  42. },
  43. subtext: {
  44. type: String,
  45. required: false,
  46. default: "",
  47. },
  48. width: {
  49. type: String,
  50. required: false,
  51. default: "500px",
  52. },
  53. });
  54. /**定义option */
  55. const option = computed({
  56. get() {
  57. return {
  58. tooltip: {
  59. position: "top",
  60. formatter: function (params) {
  61. if (params.componentType === "markLine") {
  62. return params.name;
  63. } else {
  64. return (
  65. "偏航:" +
  66. params.value[0] +
  67. "度<br/ >风速:" +
  68. params.value[1] +
  69. "m/s"
  70. );
  71. }
  72. },
  73. },
  74. title: {
  75. text: props.title || "",
  76. subtext: props.subtext || "",
  77. top: 6,
  78. left: "5%",
  79. textStyle: {
  80. color: "#b2bcbf",
  81. },
  82. subtextStyle: {
  83. color: "#b2bcbf",
  84. },
  85. },
  86. grid: {
  87. top: 80,
  88. left: 40,
  89. right: 40,
  90. bottom: 40,
  91. },
  92. xAxis: props.xAxis || [],
  93. // {
  94. // type: 'category',
  95. // data: props.xAxis || [],
  96. // boundaryGap: false,
  97. // splitLine: {
  98. // show: true
  99. // },
  100. // axisLine: {
  101. // show: false
  102. // }
  103. // },
  104. yAxis: props.yAxis || [],
  105. // {
  106. // type: 'category',
  107. // data: props.yAxis,
  108. // axisLine: {
  109. // show: false
  110. // }
  111. // },
  112. series: props.series || [],
  113. // [
  114. // {
  115. // name: 'Punch Card',
  116. // type: 'scatter',
  117. // symbolSize: function (val) {
  118. // return val[2] * 2;
  119. // },
  120. // data: props.data,
  121. // animationDelay: function (idx) {
  122. // return idx * 5;
  123. // }
  124. // }
  125. // ]
  126. };
  127. },
  128. set(val) {},
  129. });
  130. watch(
  131. () => option,
  132. (newVal, oldVal) => {
  133. if (chartIns.value) {
  134. // console.log(newVal)
  135. const echartIns = toRaw(chartIns.value);
  136. echartIns.setOption(toRaw(newVal.value));
  137. }
  138. },
  139. { deep: true }
  140. );
  141. watch([() => props.width, () => props.height], (newVal, oldVal) => {
  142. if (chartIns.value) {
  143. const echartIns = toRaw(chartIns.value);
  144. nextTick(() => echartIns.resize());
  145. }
  146. });
  147. const store = useStore();
  148. const collapse = computed({
  149. get() {
  150. return store.state.collapse;
  151. },
  152. set(val) {},
  153. });
  154. watch(collapse, (val) => {
  155. if (chartIns.value) {
  156. setTimeout(() => {
  157. chartIns.value?.resize();
  158. }, 300);
  159. }
  160. });
  161. onMounted(() => {
  162. nextTick(() => {
  163. echarts.registerTheme("chartTheme", chartTheme);
  164. const echartIns = echarts.init(
  165. document.getElementById(chartId),
  166. "chartTheme"
  167. );
  168. chartIns.value = echartIns;
  169. echartIns.setOption(option.value);
  170. window.addEventListener("resize", () => {
  171. echartIns.resize();
  172. });
  173. });
  174. });
  175. </script>
  176. <template>
  177. <div
  178. :id="chartId"
  179. :style="{ height: props.height, width: props.width }"
  180. ></div>
  181. </template>