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