【vue echart】完成一个简单echart图表+自适应

实现效果:

html:

javascript 复制代码
<div
       ref="echartOne"
       id="echartOne"
       style="width: 100%; height: 100%"
></div>

js:

javascript 复制代码
    getEchartOne() {
      let chart = this.$echarts.init(this.$refs.echartOne);
      chart.setOption({
        title: {
          text: "Stacked Line"
        },
        tooltip: {
          trigger: "axis"
        },
        legend: {
          data: ["Email", "Union Ads", "Video Ads", "Direct", "Search Engine"]
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
        },
        yAxis: {
          type: "value"
        },
        series: [
          {
            name: "Email",
            type: "line",
            stack: "Total",
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: "Union Ads",
            type: "line",
            stack: "Total",
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: "Video Ads",
            type: "line",
            stack: "Total",
            data: [150, 232, 201, 154, 190, 330, 410]
          },
          {
            name: "Direct",
            type: "line",
            stack: "Total",
            data: [320, 332, 301, 334, 390, 330, 320]
          },
          {
            name: "Search Engine",
            type: "line",
            stack: "Total",
            data: [820, 932, 901, 934, 1290, 1330, 1320]
          }
        ]
      });
//自适应部分
      window.addEventListener('resize', function () {
        chart.resize()     
})
    },

补充知识点:

更改标题,图例的颜色,x轴和y轴的颜色变为白色

javascript 复制代码
{
        title: {
          text: ""
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow"
          }
        },
        legend: {
          textStyle: {
            fontSize: 12,
            color: "#FFFFFF"
          }
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: [0, 0.01],
            data: citylist,
            // axisLabel: {
            //     color: fontColor,
            //     color: '#FFFFFF',
            //   },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#FFFFFF"
              }
            }
          }
        ],
        yAxis: {
          type: "value",
          axisLine: {
            show: false,
            lineStyle: {
              color: "white"
            }
          }
        },
        series: [
          {
            name: "产品个数/个",
            type: "bar",
            data: sumList,
            textStyle: {
              color: "#00FFFF"
            },
            itemStyle: {
              normal: {
                color: "#00FFFF"
              }
            }
          },
          {
            name: "溯源次数/次",
            type: "bar",
            data: numList,
            itemStyle: {
              normal: {
                color: "#FFFF00"
              }
            }
          }
        ]
      }
相关推荐
前端程序猿之路3 分钟前
Next.js 入门指南 - 从 Vue 角度的理解
前端·vue.js·语言模型·ai编程·入门·next.js·deepseek
大布布将军8 分钟前
⚡️ 深入数据之海:SQL 基础与 ORM 的应用
前端·数据库·经验分享·sql·程序人生·面试·改行学it
川贝枇杷膏cbppg17 分钟前
Redis 的 RDB 持久化
前端·redis·bootstrap
D_C_tyu40 分钟前
Vue3 + Element Plus | el-table 表格获取排序后的数据
javascript·vue.js·elementui
JIngJaneIL41 分钟前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
天外天-亮1 小时前
v-if、v-show、display: none、visibility: hidden区别
前端·javascript·html
jump_jump1 小时前
手写一个 Askama 模板压缩工具
前端·性能优化·rust
hellotutu1 小时前
vue2 从 sessionStorage 手动取 token 后,手动加入到 header
vue.js·token·session·header
be or not to be1 小时前
HTML入门系列:从图片到表单,再到音视频的完整实践
前端·html·音视频
90后的晨仔2 小时前
在macOS上无缝整合:为Claude Code配置魔搭社区免费API完全指南
前端