echarts 折线图

1. vue按需引入折线图

/**
 * echarts版本 5.3.2
 * main.js
 */
// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from 'echarts/core'
// 引入折线图图表,图表后缀都为 Chart
import { LineChart } from 'echarts/charts'
// 引入提示框,标题,直角坐标系,数据集,内置数据转换器组件,组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  LegendComponent,
  DataZoomComponent
} from 'echarts/components';
// 标签自动布局,全局过渡动画等特性
import { LabelLayout, UniversalTransition } from 'echarts/features'
// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from 'echarts/renderers'

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  LegendComponent,
  DataZoomComponent,
  LineChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer
])

Vue.prototype.$echarts = echarts

2. 移动端折线图

2.1 代码

<template>
  <div class="line-echart-container">
    <div ref="lineEchart"></div>
  </div>
</template>

<script>
export default {
  name: 'LineEchart',
  data() {
    return {
    }
  },
  methods: {
    initOption() {
      return {
        title: {
          text: '标题',
          top: 'bottom',
          left: 'center',
          textStyle: {
            fontSize: '12px',
            color: 'rgba(177, 177, 177, 1)',
          }
        },
        grid: {
          top: 0,
          bottom: '25%'
        },
        xAxis: {
          show: false,
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          show: false,
          min: 'dataMin', // 取数据在该轴上的最小值作为最小刻度
          max: 'dataMax',
          type: 'value'
        },
        series: [
          {
            type: 'line',
            symbol: 'none', //去掉折线图中的节点
            areaStyle: { // 区域填充样式
              color: 'rgba(255, 234, 234, 1)'
            },
            lineStyle: {
              color: 'rgba(255, 36, 66, 1)',
              width: 1,
            },
            data: [150, 230, 224, 218, 135, 147, 260]
          }
        ]
      }
    }
  },
  mounted () {
    const myChart = this.$echarts.init(this.$refs.lineEchart)
    myChart.setOption(this.initOption())

    window.onresize = function() {
      myChart.resize()
    }
  }
}
</script>

<style scoped lang="scss">
  .line-echart-container {
    > div {
      width: 80px;
      height: 70px;
    }
  }
</style>

2.2. 效果图


3. 双折线图

3.1 代码

<template>
  <div class="line-echart-container">
    <div ref="lineEchart"></div>
  </div>
</template>

<script>
import echartsHandIcon from '@/assets/slider.svg'

export default {
  name: 'LineEchart',
  data() {
    return {
    }
  },
  created() {
    this.$nextTick(() => {
      const myChart = this.$echarts.init(this.$refs.lineEchart)
      myChart.setOption(this.initOption())
  
      window.onresize = () => {
        myChart.resize()
      }
    })
  },
  methods: {
    initOption() {
      const y1Data = [100, 280, 350, 460, 880, 300, 400, 160, 330, 770]

      return {
        title: {
          subtext: '单位:万元',
          top: 0,
          left: 10,
          subtextStyle: {
            fontFamily: 'MicrosoftYaHei',
            color: '#8C8C8C'
          }
        },
        dataZoom: [{
          type: 'slider',
          bottom: 6,
          height: 12,
          handleIcon: `image://${echartsHandIcon}`,
          handleSize: 24,
          fillerColor: 'rgba(245, 245, 245, .7)', // 选中范围的填充颜色
          borderColor: '#f5f5f5', // 边框颜色
          dataBackground: { // 数据阴影的样式
            areaStyle: { // 阴影的填充样式
              color: '#E6E6E6',
              shadowColor: '#E6E6E6'
            }
          },
          textStyle: {
            color: '#939FAE'
          }
        }],
        tooltip: {
          trigger: 'axis',
          padding: [0, 8, 4],
          axisPointer: {
            lineStyle: {
              color: '#e5e5e599'
            }
          },
          textStyle: {
            fontFamily: 'MicrosoftYaHei',
            color: '#8C8C8C',
            fontSize: 10,
            lineHeight: 20
          }
        },
        legend: {
          top: 10,
          right: 0,
          icon: 'circle',
          itemWidth: 6,
          itemHeight: 6,
          textStyle: {
            fontFamily: 'MicrosoftYaHei',
            color: '#8c8c8c'
          },
        },
        grid: {
          left: 16,
          right: 16,
          top: 50,
          bottom: 20,
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false, // 坐标轴两边留白策略
          axisLabel: {
            color: '#8C8C8C'
          },
          axisLine: {
            onZero: false, // 有负数时,轴线是否在0刻度线上
            lineStyle: {
              color: '#E5E5E5'
            }
          },
          axisTick: {
            show: false
          },
          data: ['01/01','01/02','01/03','01/04','01/05','01/06','01/07','01/08','01/09','01/10']
        },
        yAxis: [
          {
            type: 'value',
            min: 0,
            max: Math.ceil(Math.max.apply(null, y1Data) / 5) * 5,
            interval: Math.ceil(Math.max.apply(null, y1Data) / 5),
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#8C8C8C'
            },
            splitLine: { // 分隔线样式
              lineStyle: {
                color: ['#e5e5e599']
              }
            }
          },
          {
            type: 'value',
            min: 0,
            max: 100,
            axisLine: {
              show: false
            },
            axisLabel: {
              color: '#8C8C8C',
              formatter: '{value}%'
            },
            axisTick: {
              show: false
            },
            splitLine: {
              lineStyle: {
                color: ['#e5e5e599']
              }
            }
          }
        ],
        series: [
          {
            name: '金额',
            type: 'line',
            showSymbol: false,
            symbol: 'circle', // 标记的图形
            symbolSize: 2,
            lineStyle: {
              width: 1,
              color: '#1136F5'
            },
            data: y1Data
          },
          {
            name: '比率',
            type: 'line',
            yAxisIndex: 1,
            showSymbol: false,
            symbol: 'circle',
            symbolSize: 2,
            lineStyle: {
              width: 1,
              color: '#00A870'
            },

            data: [0, 10, 20, 40, 80, 15, 30, 25, 35, 40]
          }
        ]
      }
    }
  }
}
</script>

<style scoped lang="scss">
  .line-echart-container {
    > div {
      width: 300px;
      height: 240px;
      margin: auto;
    }
  }
</style>

3.2 效果图

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容