vue中使用echarts总结

参考echarts官方示例

下载安装

npm i echarts

导入

import Echarts from 'echarts';
//引入主题
import 'echarts/theme/shine.js';

图表初始化

  let echartsInstance = Echarts.init(
    document.getElementById(divId, "shine");
  );
  echartsInstance.setOption(option, true);
  window.addEventListener("resize", function () {
    echartsInstance.resize();
  });

常用图表基础配置

通过type设置,折线图为line,柱状图为bar

  • 折线图
var option = {
  title: {
    text: "堆叠区域图"
  },
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "cross",
      label: {
        backgroundColor: "#6a7985"
      }
    }
  },
  legend: {
    data: ["邮件营销", "联盟广告", "视频广告", "直接访问", "搜索引擎"]
  },
  toolbox: {
    feature: {
      saveAsImage: {}
    }
  },
  grid: {
    left: "3%",
    right: "4%",
    bottom: "3%",
    containLabel: true
  },
  xAxis: {
    type: "category",
    data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
  },
  yAxis: {
    type: "value"
  },
  series: [
    {
      data: [820, 932, 901, 934, 1290, 1330, 1320],
      type: "line",
      areaStyle: {}
    }
  ]
};
  • 柱状图
var dataAxis = ["点","击","柱","子","或","者","两","指","在","触","屏","上","滑","动","能","够","自","动","缩","放"];
var data = [220,182,191,234,290,330,310,123,442,321,90,149,210,122,133,34,198,123,125,220];
var yMax = 500;
var dataShadow = [];

for (var i = 0; i < data.length; i++) {
  dataShadow.push(yMax);
}

option = {
  title: {
    text: "特性示例:渐变色 阴影 点击缩放",
    subtext: "Feature Sample: Gradient Color, Shadow, Click Zoom"
  },
  xAxis: {
    data: dataAxis,
    axisLabel: {
      inside: true,
      textStyle: {
        color: "#fff"
      }
    },
    axisTick: {
      show: false
    },
    axisLine: {
      show: false
    },
    z: 10
  },
  yAxis: {
    axisLine: {
      show: false
    },
    axisTick: {
      show: false
    },
    axisLabel: {
      textStyle: {
        color: "#999"
      }
    }
  },
  dataZoom: [
    {
      type: "inside"
    }
  ],
  series: [
    {
      // For shadow
      type: "bar",
      itemStyle: {
        color: "rgba(0,0,0,0.05)"
      },
      barGap: "-100%",
      barCategoryGap: "40%",
      data: dataShadow,
      animation: false
    },
    {
      type: "bar",
      itemStyle: {
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          { offset: 0, color: "#83bff6" },
          { offset: 0.5, color: "#188df0" },
          { offset: 1, color: "#188df0" }
        ])
      },
      emphasis: {
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "#2378f7" },
            { offset: 0.7, color: "#2378f7" },
            { offset: 1, color: "#83bff6" }
          ])
        }
      },
      data: data
    }
  ]
};

// Enable data zoom when user click bar.
var zoomSize = 6;
myChart.on("click", function(params) {
  console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]);
  myChart.dispatchAction({
    type: "dataZoom",
    startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
    endValue:
      dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)]
  });
});

常用颜色渐变设置

  • 从下到上渐变
    方式一:
// 从下到上渐变
  colorLinearGradientFromBottom(fromColor, toColor) {
    return new Echarts.graphic.LinearGradient(0, 1, 0, 0, [{
      offset: 0,
      color: fromColor // 0% 处的颜色
    }, {
      offset: 1,
      color: toColor // 100% 处的颜色
    }], false)
  }

方式二:

colorLinearGradientFromBottom2(fromColor, toColor) {
    return {
      type: "linear",
      x: 0,
      y: 1,
      x2: 0,
      y2: 0,
      colorStops: [
        {
          offset: 0,
          color: fromColor // 0% 处的颜色
        },
        {
          offset: 1,
          color: toColor // 100% 处的颜色
        }
      ],
      global: false // 缺省为 false
    };
  }
  • 从左到右渐变
    方式一:
// 从左到右渐变
  colorLinearGradientFromLeft(fromColor, toColor) {
    return new Echarts.graphic.LinearGradient(0, 0, 1, 0, [{
      offset: 0,
      color: fromColor // 0% 处的颜色
    }, {
      offset: 1,
      color: toColor // 100% 处的颜色
    }], false)
  }

方式二:

colorLinearGradientFromLeft2(fromColor, toColor) {
    return {
      type: "linear",
      x: 0,
      y: 0,
      x2: 1,
      y2: 0,
      colorStops: [
        {
          offset: 0,
          color: fromColor // 0% 处的颜色
        },
        {
          offset: 1,
          color: toColor // 100% 处的颜色
        }
      ],
      global: false // 缺省为 false
    };
  }

option中常用参数设置

1. 图表位置设置,利用grid参数

    grid: {
        top: '20',
        right: '20',
        left: '50%',
        bottom: '10%',
        containLabel: true
      }

取值可以是固定值,也可以是百分比。

2. 鼠标悬停提示框设置,利用tooltip参数

tooltip: {
  trigger: "axis",
  axisPointer: {
    type: "line",
    crossStyle: {
      lineColor: "#fff"
    }
  }
}

3. 图表x轴设置,利用xAxis参数

xAxis: [
  {
    type: "category",
    boundaryGap: false,
    color: "#59588D",
    axisPointer: {
      lineStyle: {
        color: "#004E52",
        opacity: 0.5,
        width: 2
      },
      label: {
        show: true,
        formatter: function(params) {
          return echarts.format.formatTime("yyyy-MM-dd", params.value);
        },
        backgroundColor: "#004E52"
      }
    },
    // 刻度线颜色设置
    axisLine: {
      lineStyle: {
        color: "#ccc" // 颜色
      }
    },
    axisLabel: {
      // interval: 0, //坐标刻度之间显示间隔
      color: "white", //刻度值字体颜色设置
      formatter: "{value} m", //加单位显示
      rotate: 30 //旋转角度
    },
    //x轴线不显示
    splitLine: {
      show: false
    },
    data: axis
  }
];

4. 图表y轴设置,利用yAxis参数

  • 单个坐标轴参数设置
yAxis: [
  {
    name: "当前水位",
    type: "value",
    // 坐标轴放置位置
    position: "right",
    min: parseInt(min) - 5,
    max: parseInt(max) + 5,
    // 坐标轴线设置,不显示
    splitLine: {
      show: false
    },
    splitArea: { show: false },
    // 坐标轴颜色设置
    axisLine: {
      lineStyle: {
        color: "#face5a"
      }
    }
  }
]
  • 多个坐标轴参数设置,重点在位置position设置
yAxis: [
  {
    name: "当前水位",
    nameTextStyle: {
       color: "white"
    },
    type: "value",
    position: "right",
    min: parseInt(min) - 5,
    max: parseInt(max) + 5,
    splitLine: {
      show: false
    },
    axisLine: {
      lineStyle: {
        color: "#face5a"
      }
    }
  },
  {
    name: "超过人工溢流底板高程(m)",
    type: "value",
    nameTextStyle: {
       color: "white"
    },
    position: "left",
    min: 0,
    splitLine: {
      show: false
    },
    axisLine: {
      lineStyle: {
        color: "#3ae7c4"
      }
    }
  },
  {
    name: "标准水位(m)",
    type: "value",
    position: "right",
    offset: 60, // 坐标轴偏移量
    min: parseInt(min) - 5,
    splitLine: {
      show: false
    },
    axisLine: {
      lineStyle: {
        color: "#00aeff"
      }
    }
  }
]

nameTextStyle: 坐标轴名称的文字样式

  1. 显示数据设置,利用series参数
  • 单个折线图设置
series: [
  {
    name: "入库流量",
    type: "line",
    yAxisIndex: 0, // 第几个Y轴 索引值
    smooth: true, //曲线平滑
    symbol: "emptyCircle", // 设置拐点形状
    symbolSize: 10, //设置拐点大小
    //showAllSymbol: true,
    itemStyle: {
      normal: {
        // 折线颜色设置
        color: this.colorLinearGradientFromLeft(
          lineStyleRgb["黄"][0],
          lineStyleRgb["黄"][1]
        ),
        // 折线面积背景颜色设置
        areaStyle: {
          type: "default",
          color: this.colorLinearGradientFromBottom(
            shadowStyleRgb["黄"][0],
            shadowStyleRgb["黄"][1]
          )
        }
      }
    },
    // 数据赋值
    data: inFloatTotalData
  }
]

参数symbol:,默认取值:emptyCircle,还可以取值circle, rect, roundRect, triangle, diamond, pin, arrow, none

  • 多个折线图设置,通过yAxisIndex参数确定设置的轴线样式
series: [
  {
    name: "超过人工溢流底板高程",
    type: "line",
    yAxisIndex: 1, // 第几个Y轴 索引值
    smooth: true, //曲线平滑
    itemStyle: {
      normal: {
        color: this.LinearGradientLine("#3ae7c4", "#1972cd"),
        areaStyle: {
          type: "default",
          origin: "end",
          color: this.areaStyleColor(
            areaStyleRgb["蓝"][0],
            areaStyleRgb["蓝"][1]
          )
        }
        //以及在折线图每个日期点顶端显示数字
        // label: {
        //         show: true,
        //         position: 'top',
        //         textStyle: {
        //           color: 'white'
        //     }
        // }
      }
    },
    // areaStyle : {},
    data: data2
  },
  {
    name: "标准水位",
    type: "line",
    yAxisIndex: 2, // 第几个Y轴 索引值
    smooth: true, //曲线平滑
    itemStyle: {
      normal: {
        color: this.LinearGradientLine("#00aeff", "#4552fc"),
        areaStyle: {
          type: "default",
          origin: "end",
          color: this.areaStyleColor(
            areaStyleRgb["蓝"][0],
            areaStyleRgb["蓝"][1]
          )
        }
      }
    },
    data: data3
  }
]
  • 单个柱状图设置
series: [
  {
    type: "bar",
    data: pData,
    barWidth: "40px",
    itemStyle: {
      normal: {
        color: params => {
          var colorList = this.colorListInit(pData);
          return colorList[params.dataIndex];
        }
      }
    }
  }
]

当柱状图中需根据不同取值设置不同颜色时,color中设置为函数形式,利用参数params中的params.dataIndex,通过颜色遍历,指定对应的颜色

colorListInit(pData) {
  let pColorList = pData.map((item, index) => {
    return this.colorLinearGradientFromBottom(
      lineStyleRgb[colorArray[index]][0],
      lineStyleRgb[colorArray[index]][1]
    );
  });
  return pColorList;
},
// 从下到上渐变
colorLinearGradientFromBottom(fromColor, toColor) {
  return new Echarts.graphic.LinearGradient(
    0,
    1,
    0,
    0,
    [
      {
        offset: 0,
        color: fromColor // 0% 处的颜色
      },
      {
        offset: 1,
        color: toColor // 100% 处的颜色
      }
    ],
    false
  );
}
  • 多个柱状图设置
series: [
  {
    name: legend[0],
    type: "bar",
    data: seriesData[legend[0]],
    /* barWidth: 10, //柱子宽度
          barGap: 1, //柱子之间间距 */
    itemStyle: {
      normal: {
        barBorderRadius: 5,
        color: this.colorGradient(colorList["黄"][0], colorList["黄"][1]),
        opacity: 1,
        shadowBlur: 13,
        shadowColor: " rgba(132,208,55,0.38)",
        shadowOffsetX: 0,
        shadowOffsetY: 0
      }
    }
  },
  {
    name: legend[1],
    type: "bar",
    data: seriesData[legend[1]],
    /* barWidth: 10,
          barGap: 1, */
    itemStyle: {
      normal: {
        barBorderRadius: 5,
        color: this.colorGradient(colorList["橙"][0], colorList["橙"][1]),
        opacity: 1,
        shadowBlur: 13,
        shadowColor: " rgba(252,113,25,0.38)",
        shadowOffsetX: 0,
        shadowOffsetY: 0
      }
    }
  }
]
  • 图表标线的设置,利用markLine参数
    a. 平行于Y轴趋势线
series: [
  {
    name: "平行于Y轴趋势线",
    type: "line",
    markLine: {
      data: [[{ coord: ["今天", 0] }, { coord: ["今天", 1000] }]]
    }
  },
  //以左轴为基准
  {
    name: "1",
    type: "line",
    yAxisIndex: 0, //以左轴为基准
    data: [700, 700, 700, 700, 700, 700, 700],
    lineStyle: {
      width: 2
    },
    symbolSize: 6,
    itemStyle: {
      normal: {
        color: Common.LinearGradientLine("#ff8e78", "#fc4545")
      }
    },
    areaStyle: {
      color: Common.areaStyleColor(
        Common.areaStyleRgb["xxsw"][1],
        Common.areaStyleRgb["xxsw"][0]
      )
    }
  },
  //以右轴为基准
  {
    name: "3",
    type: "line",
    yAxisIndex: 1, //以右轴为基准
    symbolSize: 6,
    data: [null, null, null, null, 10, 30, 80],
    lineStyle: {
      // type: 'dotted',
      // color: 'green',
      width: 2
    },
    itemStyle: {
      normal: {
        color: Common.LinearGradientLine("#f0ff00", "#4abd04")
      }
    },
    areaStyle: {
      color: Common.areaStyleColor(
        Common.areaStyleRgb["ycsw"][1],
        Common.areaStyleRgb["ycsw"][0]
      )
    }
  }
]
x轴标线.png

b. 平行于X轴趋势线

// 分别在Y值50/100/150/200/300处显示趋势线
series: {
  name: "Beijing AQI",
  type: "line",
  data: data.map(function(item) {
    return item[1];
  }),
  markLine: {
    silent: true,
    data: [
      {
        yAxis: 50
      },
      {
        yAxis: 100
      },
      {
        yAxis: 150
      },
      {
        yAxis: 200
      },
      {
        yAxis: 300
      }
    ]
  }
}
y轴标线.png
  1. 图例legend设置
  • 一般参数设置
legend: {
  data: ["入库流量", "出库流量", "库水位"],
  textStyle: {
    color: "white"
  },
  left: "right"
}
  • 较完整参数设置
legend: {
    data: legend, //['2005', '2008', '2010', '2013', '2017'],
    /* align: 'right',
    right: 0, */
    bottom: 0,
    textStyle: {
      color: "#979797"
    },
    //orient: 'vertical',
    itemWidth: 10,
    itemHeight: 10,
    itemGap: 15
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容