SingleLine.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <div ref="chartRef" :style="{ height, width }"></div>
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, PropType, ref, Ref, reactive, watchEffect } from 'vue';
  6. import { useECharts } from '/@/hooks/web/useECharts';
  7. import { cloneDeep } from 'lodash-es';
  8. export default defineComponent({
  9. name: 'single-line',
  10. props: {
  11. chartData: {
  12. type: Array,
  13. default: () => [],
  14. },
  15. option: {
  16. type: Object,
  17. default: () => ({}),
  18. },
  19. width: {
  20. type: String as PropType<string>,
  21. default: '100%',
  22. },
  23. height: {
  24. type: String as PropType<string>,
  25. default: 'calc(100vh - 78px)',
  26. },
  27. },
  28. setup(props) {
  29. const chartRef = ref<HTMLDivElement | null>(null);
  30. const { setOptions, echarts } = useECharts(chartRef as Ref<HTMLDivElement>);
  31. const option = reactive({
  32. tooltip: {
  33. trigger: 'axis',
  34. axisPointer: {
  35. type: 'shadow',
  36. label: {
  37. show: true,
  38. backgroundColor: '#333',
  39. },
  40. },
  41. },
  42. xAxis: {
  43. type: 'category',
  44. data: [],
  45. },
  46. yAxis: {
  47. type: 'value',
  48. },
  49. series: [
  50. {
  51. type: 'line',
  52. showSymbol: false,
  53. smooth: true,
  54. areaStyle: {},
  55. data: [],
  56. },
  57. ],
  58. });
  59. watchEffect(() => {
  60. props.chartData && initCharts();
  61. });
  62. function initCharts() {
  63. if (props.option) {
  64. Object.assign(option, cloneDeep(props.option));
  65. }
  66. let seriesData = props.chartData.map((item) => {
  67. return item.value;
  68. });
  69. let xAxisData = props.chartData.map((item) => {
  70. return item.name;
  71. });
  72. option.series[0].data = seriesData;
  73. option.xAxis.data = xAxisData;
  74. setOptions(option);
  75. }
  76. return { chartRef };
  77. },
  78. });
  79. </script>