index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div class="echart" :class="{ bg, noPadding }" :style="{ height }" ref="chart"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import * as echarts from "echarts";
  6. import { onMounted, ref, watch } from "vue";
  7. // import * as chartOptions from './options.js'
  8. import { useStore } from "/@/store/index";
  9. const store = useStore();
  10. const loadingOption = {
  11. // maskColor: 'rgba(255, 255, 255, 0.1)'
  12. maskColor: "rgba(255, 255, 255, 0)",
  13. text: "",
  14. color: "#409eff",
  15. // textColor: '#000',
  16. // zlevel: 0,
  17. // fontSize: 12,
  18. // showSpinner: true,
  19. spinnerRadius: 18,
  20. lineWidth: 2,
  21. // fontWeight: 'normal',
  22. // fontStyle: 'normal',
  23. // fontFamily: 'sans-serif'
  24. };
  25. const props = defineProps({
  26. bg: Boolean,
  27. noPadding: Boolean,
  28. auto: {
  29. type: Boolean,
  30. default: false,
  31. },
  32. autoLoading: {
  33. type: Boolean,
  34. default: true,
  35. },
  36. height: {
  37. type: String,
  38. default: "100%",
  39. },
  40. type: {
  41. type: String,
  42. default: "Bar",
  43. },
  44. option: {
  45. type: Object,
  46. default: () => {
  47. return {};
  48. },
  49. },
  50. });
  51. let myChart: any = null;
  52. let optionCache: any = null;
  53. const chart = ref();
  54. // 绘制图形
  55. const draw = (option?: object) => {
  56. myChart && myChart.dispose();
  57. myChart = echarts.init(chart.value);
  58. myChart.hideLoading();
  59. if (option) {
  60. optionCache = option;
  61. myChart.setOption(option);
  62. } else {
  63. // myChart.setOption(chartOptions[`get${props.type}Option`]({ ...props.option }))
  64. }
  65. };
  66. const loading = () => {
  67. myChart?.setOption({}, { notMerge: true });
  68. myChart?.showLoading(loadingOption);
  69. };
  70. const hideLoading = () => {
  71. myChart?.hideLoading();
  72. };
  73. const getChart = () => {
  74. return myChart;
  75. };
  76. const download = (name: string = "chart picture") => {
  77. const picInfo = myChart.getDataURL({
  78. type: "png",
  79. pixelRatio: 2,
  80. backgroundColor: "#fff",
  81. });
  82. let elink = document.createElement("a");
  83. elink.download = name;
  84. elink.style.display = "none";
  85. elink.href = picInfo;
  86. document.body.appendChild(elink);
  87. elink.click();
  88. URL.revokeObjectURL(elink.href);
  89. document.body.removeChild(elink);
  90. };
  91. // 配置变化 重绘
  92. watch(
  93. () => props.option,
  94. () => {
  95. draw();
  96. }
  97. );
  98. watch(
  99. () => store.state.global.resize,
  100. () => {
  101. draw(optionCache);
  102. }
  103. );
  104. onMounted(() => {
  105. myChart = echarts.init(chart.value);
  106. props.autoLoading && loading();
  107. props.auto && draw();
  108. });
  109. defineExpose({ draw, loading, download, getChart, hideLoading });
  110. </script>
  111. <style scoped lang="scss">
  112. .echart {
  113. height: 100%;
  114. width: 100%;
  115. // margin: 1vh 0;
  116. padding: 2px;
  117. }
  118. .bg {
  119. background: #444;
  120. }
  121. </style>