vue项目中使用echarts图表

vue项目中使用echarts图表

npm install echarts

在使用echarts的页面引入

import echarts from "echarts";

使用echrts绘制柱状图

  • 实例化对象

    // 实例化对象
    let myChart = echarts.init(
    document.querySelector(".bar .chart") as HTMLCanvasElement
    );
    
  • 指定配置和数据

    // 指定配置和数据
        let option = {
          color: ["#2f89cf"],
          tooltip: {
            trigger: "axis",
            axisPointer: {
              // 坐标轴指示器,坐标轴触发有效
              type: "shadow", // 默认为直线,可选为:'line' | 'shadow'
            },
          },
          grid: {
            left: "0%",
            top: "10px",
            right: "0%",
            bottom: "4%",
            containLabel: true,
          },
          xAxis: [
            {
              type: "category",
              data: xAxisdata,
              axisTick: {
                alignWithLabel: true,
              },
              /* 修改X轴刻度标签 */
              axisLabel: {
                color: "rgba(255,255,255,.6)",
                fontSize: "10",
              },
              //  x轴样式不显示
              axisLine: {
                show: false,
                // 如果想要设置单独的线条样式
                // lineStyle: {
                //    color: "rgba(255,255,255,.1)",
                //    width: 1,
                //    type: "solid"
              },
            },
          ],
          yAxis: [
            {
              type: "value",
              // y 轴文字标签样式
              axisLabel: {
                color: "rgba(255,255,255,.6)",
                fontSize: "12",
              },
              // y轴线条样式
              axisLine: {
                lineStyle: {
                  color: "rgba(255,255,255,.1)",
                  // width: 1,
                  // type: "solid"
                },
              },
              // y 轴分隔线样式
              splitLine: {
                lineStyle: {
                  color: "rgba(255,255,255,.1)",
                },
              },
            },
          ],
          series: [
            {
              name: "本月产量",
              type: "bar",
              barWidth: "35%",
              data: yAxisdata,
              itemStyle: {
                // 修改柱子圆角
                barBorderRadius: 5,
              },
            },
          ],
        };
    
  • 把配置给到实例对象

    // 把配置项给到实例对象
    myChart.setOption(option);
    
  • 设置图表随屏幕自适应

     /* 设置图表随屏幕自适应 */
     window.addEventListener("resize", function() {
    	 myChart.resize();
     });
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值