vue3 + ts + echart 实现柱形图表

首先封装Echart一个文件   代码如下

<script setup lang="ts">
import { ECharts, EChartsOption, init } from 'echarts';
import { ref, watch, onMounted, onBeforeUnmount } from 'vue';

// 定义props
interface Props {
  width?: string;
  height?: string;
  option: EChartsOption;
}
const props = withDefaults(defineProps<Props>(), {
  width: '100%',
  height: '100%',
  option: () => ({})
});

const myChartsRef = ref<HTMLDivElement>();
let myChart: ECharts;
// eslint-disable-next-line no-undef
let timer: string | number | NodeJS.Timeout | undefined;

// 初始化echarts
const initChart = (): void => {
  if (myChart !== undefined) {
    myChart.dispose();
  }
  myChart = init(myChartsRef.value as HTMLDivElement);
  // 拿到option配置项,渲染echarts
  myChart?.setOption(props.option, true);
};

// 重新渲染echarts
const resizeChart = (): void => {
  timer = setTimeout(() => {
    if (myChart) {
      myChart.resize();
    }
  }, 500);
};

onMounted(() => {
  initChart();
  window.addEventListener('resize', resizeChart);
});

onBeforeUnmount(() => {
  window.removeEventListener('resize', resizeChart);
  clearTimeout(timer);
  timer = 0;
});

watch(
    props.option,
    () => {
      initChart();
    },
    {
      deep: true
    }
);
</script>
<template>
  <div ref="myChartsRef" :style="{ height: height, width: width }" :option="option" />
</template>


第一一个案例代码如下    需要引入我们封装好的  Echart.vue文件

<script setup lang="ts">
import { reactive } from 'vue';
import Echarts from './index.vue';

const option = reactive({
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow',
      label: {
        show: true
      }
    }
  },
  grid: {
    left: '6%',
    top: '15%',
    right: '0',
    bottom: '10%'
  },
  legend: {
    data: ['昨日总人数', '今日实时人数'],
    top: '0',
    color: '#4ADEFE',
    fontSize: 14,
    selected: { 昨日使用率: false }, // 不需要显示的设置为false
    textStyle: {
      color:'#4ADEFE',
    },
    x : 'right',
    y : 'top',
  },
  xAxis: {
    data: [
      '会议室1',
      '会议室2',
      '会议室3',
      '会议室4',
      '会议室5',
      '会议室6',
      '会议室7',
      '会议室8',
      '会议室9'
    ],
    axisLine: {
      show: true, //隐藏X轴轴线
      lineStyle: {
        color: '#4ADEFE',
        width: 1
      }
    },
    axisTick: {
      show: true, //隐藏X轴刻度
      alignWithLabel: true
    },
    axisLabel: {
      show: true,
      color: '#4ADEFE', //X轴文字颜色
      fontSize: 12
    }
  },
  yAxis: [
    {
      type: 'value',
      name: '人数',
      nameTextStyle: {
        color: '#4ADEFE',
        fontSize: 12
      },
      splitLine: {
        show: true,
        lineStyle: {
          width: 1,
          color: '#4ADEFE'
        }
      },
      axisTick: {
        show: false
      },
      axisLine: {
        show: false
      },
      axisLabel: {
        show: true,
        color: '#4ADEFE',
        fontSize: 12
      }
    }
  ],
  series: [
    {
      name: '昨日总人数',
      type: 'bar',
      barWidth: 10,
      itemStyle: {
        color: {
          type: 'linear',
          x: 0, // 右
          y: 1, // 下
          x2: 0, // 左
          y2: 0, // 上
          colorStops: [
            {
              offset: 0,
              color: '#f3db5c' // 0% 处的颜色
            },
            {
              offset: 1,
              color: '#f3db5c' // 100% 处的颜色
            }
          ]
        }
      },
      data: [240, 145, 43, 35, 76, 154, 360, 42, 168]
    },
    {
      name: '今日实时人数',
      type: 'bar',
      barWidth: 10,
      itemStyle: {
        color: {
          type: 'linear',
          x: 0, // 右
          y: 1, // 下
          x2: 0, // 左
          y2: 0, // 上
          colorStops: [
            {
              offset: 0,
              color: '#4adefe' // 0% 处的颜色
            },
            {
              offset: 1,
              color: '#4adefe' // 100% 处的颜色
            }
          ]
        }
      },
      data: [133, 23, 114, 67, 89, 35, 67, 96, 90]
    }
  ]
});
</script>

<template>
  <div :style="{ width: '100%', height: '90%' }">
    <Echarts :option="option" />
  </div>
</template>

第二个案例同上

<script setup lang="ts">
import { reactive } from 'vue';
import Echarts from './index.vue';

const data = {
  "orderNum":[
    "39",
    "77",
    "96",
    "41",
    "24",
    "17",
    "0",
    "10"
  ],
  "categoryArr":[
    "订购附属",
    "新装",
    "拆机",
    "改客户资料",
    "补换卡",
    "过户",
    "换挡",
    "移机"
  ],
  "avgTime":[
    "10.79",
    "17.05",
    "14.84",
    "10.07",
    "5.58",
    "10.36",
    "0.00",
    "4.43"
  ],
  "legendArr":[
    "耗时时间",
    "订单量"
  ]
}
let maxOrder=Math.max.apply(null,data.orderNum);
const option = reactive({
  title : {text:'',subtext:'',top:'3',right:'0'},
  tooltip: {trigger: 'axis'},
  grid: {left: '8%',right: '8%',bottom: '10%'},
  xAxis: {
    type: 'category',
    axisLine: {
      lineStyle: {
        color: '#57617B'
      }
    },
    axisLabel: {
      interval:0,
      textStyle: {
        color:'#fff',
      }
    },
    data: data.categoryArr
  },
  yAxis:[
    {
      type: 'value',name: '',
      axisLine: {lineStyle: {color: '#57617B'}},
      axisLabel: {margin: 10,textStyle: {fontSize: 12},textStyle: {color:'#fff'},formatter:'{value}分'},
      splitLine: {show: false}
    },
    {
      type: 'value',name: '',max:maxOrder+parseInt(maxOrder*0.2),
      axisLabel: {margin: 10,textStyle: {fontSize: 12},textStyle: {color:'#fff'},formatter:'{value}笔'},
      splitLine: {
        show: true,
        lineStyle:{
          type:'dashed',
          color: ['#25CEF3']
        }
      }
    }
  ],
  series: [
    {
      name:'耗时时间',
      type:'line',
      yAxisIndex:0,
      smooth: false,
      symbolSize:5,
      lineStyle: { normal: {width: 2}},
      areaStyle: {
        normal: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [
              {
                offset: 0,
                color: 'rgba(230, 48, 123, 0.8)'
              },
              {
                offset: 0.8,
                color: 'rgba(230, 48, 123, 0)'
              }
            ],
            globalCoord: false // 缺省为 false
          },
          shadowColor: 'rgba(0, 0, 0, 0.1)',
          shadowBlur: 10
        }
      },
      itemStyle: {normal: { color: '#DA2F78'}},
      data:data.avgTime
    },
    {
      name:'订单量',
      type:'bar',
      barWidth:12,
      yAxisIndex:1,
      itemStyle : {
        normal: {
          barBorderRadius:[10, 10, 0, 0],
          color: {
            type: 'linear',
            x: 0,
            y: 1,
            x2: 0,
            y2: 0,
            colorStops: [
              {
                offset: 0,
                color: "#4033F9"
              },
              {
                offset: 0.8,
                color: "#BA97F9"
              }
            ],
            globalCoord: false // 缺省为 false
          },
          shadowColor: 'rgba(0, 0, 0, 0.1)',
        }
      },
      data:data.orderNum
    }
  ]
});
</script>

<template>
  <div :style="{ width: '100%', height: '90%' }">
    <Echarts :option="option" />
  </div>
</template>

更多案例可以查看我的个人网站   会持续更新  相关案例   人间且慢行 | 前端网站大全 | web前端开发

  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于 Vue 3 + TypeScript + Vite 项目,你可以使用 Vue 的官方插件 `vue-axios` 或者 `vue-resource` 来实现网络通信。下面以 `vue-axios` 为例,给出步骤: 1. 安装依赖: ```shell npm install axios vue-axios ``` 2. 在项目的入口文件(一般是 `main.ts`)中引入和使用插件: ```typescript import { createApp } from 'vue' import axios from 'axios' import VueAxios from 'vue-axios' import App from './App.vue' const app = createApp(App) app.use(VueAxios, axios) app.mount('#app') ``` 3. 在需要发送网络请求的组件中使用: ```vue <template> <div> <button @click="fetchData">发送请求</button> <ul> <li v-for="item in dataList" :key="item.id">{{ item.text }}</li> </ul> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { AxiosInstance } from 'axios' export default defineComponent({ data() { return { dataList: [] } }, methods: { fetchData() { // 发送 GET 请求示例 this.axios .get('/api/data') .then((response) => { this.dataList = response.data }) .catch((error) => { console.error(error) }) } }, mounted() { // 在 mounted 钩子中,this.axios 可以直接使用 this.fetchData() } }) </script> ``` 在上述示例中,我们通过 `VueAxios` 插件将 `axios` 实例注入到了 Vue 的实例中,在组件中可以通过 `this.axios` 直接使用。你可以根据接口文档的要求来选择合适的请求方法(例如 `get`、`post`、`put` 等),然后处理返回的结果。 当然,如果你对其他网络请求库或者自己封装的网络请求工具库更感兴趣,也可以在 Vue 3 + TypeScript + Vite 项目中自行选择使用。以上仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值