vue3+ts图表组件

子组件

<template>
  <div ref="echartsContainer" :style="containerStyles"></div>
</template>

<script setup lang="ts">
import { defineProps, ref, onMounted, onBeforeUnmount, watch, watchEffect } from 'vue';
import * as echarts from 'echarts';

const props = defineProps(['chartOptions', 'styles', 'isMenuCollapsed', 'chartData']);

const echartsContainer = ref<HTMLElement | null>(null);
let chart: echarts.ECharts | null = null;

const containerStyles = computed(() => {
  return {
    ...props.styles,
  };
});

// 创建图表
const createChart = () => {
  if (echartsContainer.value) {
    chart = echarts.init(echartsContainer.value);
    chart.setOption(props.chartOptions);
  }
};
// 自适应方法
const resizeChart = () => {
  if (chart) {
    chart.resize();
  }
};
 // 监听窗口大小变化
onMounted(() => {
  createChart();
  window.addEventListener('resize', resizeChart);
});
onBeforeUnmount(() => {
  if (chart) {
    window.removeEventListener('resize', resizeChart);
    chart.dispose();
  }
});
// 监听菜单折叠状态的变化,调整图表
watch(props.chartOptions, () => {
  if (chart) {
    // 当 props.chartData 变化时更新图表数据
    chart.setOption(props.chartOptions, true);
    resizeChart();
  }
  resizeChart();
});
</script>

父组件使用

<template>
<div>
    <EChartsComponent :chartOptions="chartOptions" :styles="{ width: '100%', height : '400px' }" />
</div>
</template>

<script setup lang="ts">
import EChartsComponent from './components/EChartsComponent.vue';

// 柱状图
const chartOptions = ref({
  color: ['#0C80B9', '#EF9847', '#F6E021', '#0066FF'],
  grid: {
    left: 15,
    top: 60,
    right: 80,
    bottom: 0,
    containLabel: true
  },
  title: {
    show: false,
    text: '暂无数据',
    left: 'center',
    top: 'middle',
    textStyle: {
      fontSize: 24,
      fontWeight: 'normal'
    }
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    },
    borderWidth: 0,
    backgroundColor: 'rgba(0,0,0,0.8)',
    textStyle: {
      color: '#FFFFFF'
    }
  },
  legend: {
    itemWidth: 12,
    itemHeight: 8,
    left: 15,
    itemGap: 16,
    textStyle: {
      color: '#49445F',
      fontWight: 'bold',
      fontFamily: '微软雅黑',
      fontSize: 12
    },
    data: ['类型一', '类型二', '类型三']
  },
  xAxis: [
    {
      type: 'category',
      name: '单位:岁',
      nameLocation: 'end',
      nameGap: 10,
      nameTextStyle: {
        color: '#A9A6B7',
        fontFamily: 'Arial'
      },
      axisTick: { show: false },
      // axisLine: { show: false },
      axisLabel: {
        show: true,
        color: '#A9A6B7'
      },
      lineStyle: {
        color: '#000000'
      },
      data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
    }
  ],
  yAxis: [
    {
      type: 'value',
      name: '单位:人',
      nameTextStyle: {
        color: '#A9A6B7',
        fontFamily: 'Arial'
      },
      axisTick: { show: false },
      axisLine: { show: false },
      splitLine: {
        show: true,
        lineStyle: {
          color: '#E3EAEF'
        }
      }
    }
  ],
  series: [
    {
      name: '类型一',
      type: 'bar',
      barGap: '40%',
      // barWidth: 16,
      barMaxWidth: 16,
      barMinWidth: 8,
      emphasis: {
        focus: 'series'
      },
      data: [200,150,130,150,200,80,50]
    },
    {
      name: '类型二',
      type: 'bar',
      barGap: '40%',
      // barWidth: 16,
      barMaxWidth: 16,
      barMinWidth: 8,
      emphasis: {
        focus: 'series'
      },
      data: [100,80,60,150,200,130,100]
    },
    {
      name: '类型三',
      type: 'bar',
      barGap: '40%',
      // barWidth: 16,
      barMaxWidth: 16,
      barMinWidth: 8,
      emphasis: {
        focus: 'series'
      },
      data: [80,150,150,200,100,80,100]
    }
  ]
});
</script>

-End-

Vue 3 结合 TypeScript 的项目中,引入 Mermaid 组件主要是为了在页面上动态生成流程图、序列图等图表。首先,你需要安装相关的依赖: ```bash npm install @vue/apollo-composable @vue/cli-plugin-vuex mermaid --save ``` 然后,在 Vue 文件中,你可以通过插件如 `vue-mermaid` 来渲染 Mermaid 语法: 1. 首先,在 main.ts 或 main.js 中导入并配置 Mermaid: ```javascript import { createApp } from &#39;vue&#39; import App from &#39;./App.vue&#39; import MermaidVue from &#39;vue-mermaid&#39; // 如果需要配置 Mermaid 全局路径,例如从 CDN 加载 const isProduction = process.env.NODE_ENV === &#39;production&#39; const mermaidConfig = { ...({ offline: true, theme: &#39;default&#39; }), // 本地运行开启离线模式 /* 如果使用CDN,配置如下 */ /* html: function (content) { return &#39;<div class="mermaid">&#39; + content + &#39;</div>&#39;; }*/ } createApp(App) .use(MermaidVue, { mermaidOptions: mermaidConfig }) .mount(&#39;#app&#39;) ``` 2. 在模板中使用 Mermaid 组件: ```html <template> <div> <mermaid :source=&#39;yourDiagramCode&#39;></mermaid> <!-- 这里是你的 Mermaid 代码 --> </div> </template> <script lang="ts"> import { ref } from &#39;vue&#39;; export default { setup() { const yourDiagramCode = ref(` graph TD; A --> B; B --> C; `); return { yourDiagramCode, }; }, }; </script> ``` 在上述例子中,`yourDiagramCode` 变量保存了你要展示的 Mermaid 代码,通过 `<mermaid>` 组件将其渲染成图表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值