封装echarts折线图

 

1、下载插件

  npm i echarts

2、components/ColorLine.vue

复制代码
<template>
  <div class="color-line" :id="id"></div>
</template>
<script>
const echarts = require('echarts/lib/echarts')
require('echarts/lib/chart/line')
import { GridComponent } from 'echarts/components'
echarts.use([GridComponent])
export default {
  props: {
    id: { type: String, required: true },
    color: { type: String, default: '' },
    optionData: {
      type: Array,
      default: function () {
        return [50, 75, 60, 90, 80, 40, 90]
      }
    },
    width: { type: String, default: '100px' },
    height: { type: String, default: '80px' }
  },
  methods: {
    initLine(id) {
      const charts = echarts.init(document.getElementById(id))
      charts.setOption({
        grid: { left: 0, right: 0, bottom: 0, top: 0 },
        xAxis: { type: 'category' },
        yAxis: { show: false },
        series: [
          {
            data: this.optionData,
            type: 'line',
            symbol: 'none', //
            smooth: true, // 圆滑曲线
            itemStyle: { normal: { lineStyle: { color: this.color } } }
          }
        ]
      })
    }
  },
  mounted() {
    document.getElementById(this.id).style.width = this.width
    document.getElementById(this.id).style.height = this.height
    this.initLine(this.id)
  }
}
</script>
<style lang="less" scoped>
.color-line {
  &[id^='main'] {
    background-color: rgba(red, 0.05);
  }
}
</style>
复制代码

3、引入、注册、使用

import ColorLine from '@/components/ColorLine.vue'
  components: { ColorLine }
复制代码
  data() {
    return {
      lineData: [
        { color: '#7994f2', data: [12, 89, 65, 42, 58, 82, 77] },
        { color: '#b7f279', data: [69, 39, 70, 102, 18, 68, 87] },
        { color: '#f279da', data: [94, 88, 117, 64, 58, 61, 67] },
        { color: '#79f2e6', data: [39, 50, 83, 109, 29, 102, 85] }
      ]
    }
  }
复制代码

DOM

    <div style="display:flex;justify-content:space-between;">
      <ColorLine v-for="(item,index) in lineData" :key="index" :id='"main"+index' :color="item.color" :optionData="item.data" width="180px" height="70px" />
    </div>

 

echarts折线图有个加载动画,比静态图好看。

posted @   吴小明-  阅读(192)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示