vue3 + ts + echart 实现柱形图表

news/2024/7/19 14:54:58 标签: vue.js, 前端, javascript, typescript, js, echarts

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

js"><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文件

js"><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>

第二个案例同上

js"><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前端开发


http://www.niftyadmin.cn/n/5387200.html

相关文章

Web服务器基础介绍

目录 Web服务器基础介绍 一、HTML是什么&#xff1f; 二、静态网页和动态网页 1、静态网页 2、动态网页 3、动态网页语言 PHP JSP Python Ruby 三、HTTP协议 1、HTTP协议是什么&#xff1f; 2、HTTP请求访问的方法 3、GET与POST比较 GET&#xff1a; POST&…

世界顶级名校计算机专业学习使用教材汇总

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-IauYk2cGjEyljid0 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-siz…

【Spring】SpringBoot 创建和使用

目 录 一.什么是Spring Boot&#xff1f;为什么要学Spring Boot&#xff1f;二.Spring Boot 优点三.Spring Boot 项目创建3.1 使用 Idea 创建3.2 网页版创建注意事项&#xff1a;包路径错误小结&#xff1a;约定大于配置 一.什么是Spring Boot&#xff1f;为什么要学Spring Boo…

代码随想录:二分查找和双指针

二分查找 lc704 题目lc704回忆y总的两个二分套路&#xff1a;acwing代码如下&#xff1a; class Solution { public:int search(vector<int>& nums, int target) {int left 0, right nums.size() - 1;while(left < right){int mid (left right) >> 1…

2 Nacos适配达梦数据库实现方案

1、修改源代码方式 Nacos 原生是不支持达梦数据库的,所以就要想办法让它 “支持”,因为是开源软件,我们可以从源码入手,在流行的 1.x 、2.x 或最新版本代码的基本上进行修改。 主要涉及到以下内容的修改: com/alibaba/nacos/persistence/datasource/ExternalDataS

flask请求时间记录和日志处理

时间记录 在Python中&#xff0c;如果需要记录一个函数执行的时间&#xff0c;可以通过装饰器的方式来实现&#xff0c;避免在每个函数中进行重复编码。 def time_summary(func):def wrapper(*args, **kwargs):# 以秒为单位的时间戳start time.time()result func(*args, **…

3.Swift导航栏的使用

Swift 导航栏的使用 一、基本使用 1.1 创建导航栏 在AppDelegate 如下方法中添加创建导航栏的代码&#xff1a; func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { …

如何利用内网穿透工具在企业微信开发者中心实现本地接口服务回调

文章目录 1. Windows安装Cpolar2. 创建Cpolar域名3. 创建企业微信应用4. 定义回调本地接口5. 回调和可信域名接口校验6. 设置固定Cpolar域名7. 使用固定域名校验 企业微信开发者在应用的开发测试阶段&#xff0c;应用服务通常是部署在开发环境&#xff0c;在有数据回调的开发场…