Vue ECharts y轴设置固定分段 + makerline设置为不同的样式作为区分 - 附完整示例

13 篇文章 0 订阅

ECharts:一个基于 JavaScript 的开源可视化图表库。

目录

 效果

一、介绍

1、官方文档:Apache ECharts

2、官方示例

二、准备工作

1、安装依赖包

 2、示例版本 

三、完整示例


 效果

一、介绍

1、官方文档:Apache ECharts

Apache EChartsApache ECharts,一款基于JavaScript的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。icon-default.png?t=N7T8https://echarts.apache.org/zh/index.html

2、官方示例

二、准备工作

1、安装依赖包

npm install echarts --save

 2、示例版本 

"echarts": "^5.4.2",

三、完整示例

​
<template>
  <div id="main"></div>
</template>
<script>
import * as echarts from "echarts";
export default {
  name: "chartView",
  data() {
    return {
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    initChart() {
      var chartDom = document.getElementById("main");
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        xAxis: {
          type: "category",
          nameTextStyle: {
            padding: 30,
          },
          boundaryGap: false,
          data: ["2012", "2013", "2014", "2015", "2016"],
          axisLine: {
            show: true,
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          type: "value",
          // show: false,
          splitLine: {
            lineStyle: {
              type: [10, 5],
            },
            show: false,
          },
          axisLine: {
            show: true,
          },
          min: 0,
          max: "100",
          axisLabel: {
            formatter: "{value} %",
            textStyle: {
              // 设置分段颜色
              color: function (value) {
                console.log(value);
                if (value >= 0 && value <= 30) {
                  return "#2c3e50";
                } else if (value > 30 && value <= 60) {
                  return "#2c3e50";
                } else if (value > 60 && value < 90) {
                  return "#2c3e50";
                } else if (value === 90) {
                  return "";
                } else if (value > 90 && value <= 100) {
                  return "#2c3e50";
                }
              },
            },
          },
          splitNumber: 3,
        },
        tooltip: {
          trigger: "axis",
          textStyle: {
            align: "left",
          },
          formatter: function (params, ticket, callback) {
            console.log(params);
            console.log(ticket);
            console.log(callback);
            const { name, value, marker } = params[0];
            const content = `时间:${name}<br>${marker}占比:${value}%`;
            return content;
          },
        },
        series: [
          {
            data: ["20", "60", "38", "72", "35", "28", "30"],
            type: "line",
            smooth: true,
            markLine: {
              symbol: "none",
              data: [
                {
                  silent: false, //鼠标悬停事件  true没有,false有
                  lineStyle: {
                    //警戒线的样式  ,虚实  颜色
                    type: "dashed",
                    color: "orange",
                  },
                  label: {
                    position: "end",
                    color: "orange",
                    formatter: function (params) {
                      console.log(params);
                      const { value } = params;
                      const content = `${value}%`;
                      return content;
                    },
                  },
                  yAxis: 30, // 警戒线的标注值,可以有多个yAxis,多条警示线   或者采用   {type : 'average', name: '平均值'},type值有  max  min  average,分为最大,最小,平均值
                },
                {
                  silent: false,
                  lineStyle: {
                    type: "dashed",
                    color: "red",
                  },
                  label: {
                    position: "end",
                    color: "red",
                    formatter: function (params) {
                      console.log(params);
                      const { value } = params;
                      const content = `${value}%`;
                      return content;
                    },
                  },
                  yAxis: 60,
                },
                {
                  silent: false,
                  lineStyle: {
                    type: "dashed",
                    color: "brown",
                  },
                  label: {
                    position: "end",
                    color: "brown",
                    formatter: function (params) {
                      console.log(params);
                      const { value } = params;
                      const content = `${value}%`;
                      return content;
                    },
                  },
                  yAxis: 100,
                },
              ],
            },
          },
        ],
      };

      option && myChart.setOption(option);
    },
  },
};
</script>

<style lang="less" scoped>
#main {
  width: 1000px;
  height: 500px;
}
</style>

​

 欢迎扫码下方二维码关注VX公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值