echarts 折线图1.0

158 阅读1分钟

记录一下做过的折线图

基础配置请参考 Echarts (基本配置) - 掘金 (juejin.cn)

这里直接上代码

<template>
  <div>
    <div class="myChart" ref="myChart_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null, // echarts实例对象
      xData: ["11.00", "11.10", "11.20", "11.30", "11.40", "11.50", "12.00"],
      yData: ["-22","-26","-28","-25","-22","-24","-26"],
    };
  },

  //由于初始化echarts实例对象需要使用到dom元素,必须要在mounted就调用
  mounted() {
    this.initEchart();
  },
  methods: {
    //创建一个echart
    initEchart() {
      this.chartInstance = this.$echarts.init(this.$refs.myChart_ref);

      const option = {
        width: 368,
        height: 113,

        grid: {
          top: "10%",
          right: "0%",
          left: "10%",
          bottom: "0%",
          show: true,
          borderColor: "transparent",
          backgroundColor: "rgba(9, 24, 48, 0.4)",
        },

        xAxis: [
          {
            type: "category",
            boundaryGap: false, //顶头显示
            data: this.xData,
            //坐标轴刻度标签的相关设置
            axisLabel: {
              margin: 10,
              color: "#e2e9ff",
              textStyle: {
                fontSize: 12,
              },
            },
            //坐标轴在 grid 区域中的分隔线
            splitLine: {
              show: true,
              lineStyle: {
                color: "rgba(255,255,255,0.06)",
              },
            },
            // 刻度线
            axisTick: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            min: -30, // 坐标轴刻度最小值。
            max: -20, // 坐标轴刻度最大值。
            splitNumber: 2, // 坐标轴的分割段数,是一个预估值,实际显示会根据数据稍作调整。
            interval: 2, // 强制设置坐标轴分割间隔。

            //坐标轴刻度标签的相关设置
            axisLabel: {
              color: "#e2e9ff",
              textStyle: {
                fontSize: 12,
              },
            },

            //坐标轴在 grid 区域中的分隔线
            splitLine: {
              lineStyle: {
                color: "rgba(255,255,255,0.06)",
              },
            },
            // 刻度线
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            type: "line",
            symbol: "circle",
            showAllSymbol: true, //显示所有数据点
            symbolSize: 6, //原点大小
            smooth: true, //开启平滑处理

            lineStyle: {
              normal: {
                width: 2,
                color: "rgba(255, 201, 125, 0.8)", // 线条颜色
              },
            },

            itemStyle: {
              normal: {
                label: {
                  show: true,
                  color: "white", // 在折线拐点上显示数据
                },
                color: "rgba(255,255,255,1)", //折线点的颜色
              },
            },

            areaStyle: {
              //区域填充样式
              type: "linear",
              normal: {
                origin: "start", //为负数时的 渐变方向
                color: new echarts.graphic.LinearGradient(
                  0,
                  1,
                  0,
                  0,
                  [
                    {
                      offset: 0,
                      color: "rgba(255,255,255,0)",
                    },
                    {
                      offset: 1,
                      color: "rgba(255, 201, 125, 0.5)",
                    },
                  ],
                  false // 缺省为 false
                ),
              },
            },

            data: this.yData,
          },
        ],
      };

      this.chartInstance.setOption(option);
    },
  },
};
</script>
<style lang="less" scoped>
.myChart {
  position: absolute;
  left: 800px;
  top: 400px;
  width: 448px;
  height: 180px;
  background: rgb(0, 255, 94);
}
</style>

先说明一下option和myChart的关系

当时做折线图的时候也花了时间去看。
当时想设置背景色,就直接设置了myChart,发现不对。不符合我想要的坐标轴的数值在一个背景色的外面

注:这是yData没有值的情况下的显示。
蓝色区域canvas的宽高,对应的就是myChart的宽高,
具体显示的区域的宽高,则是在option中定义的

image.png

当我修改option的width为400时,会发现背景色区域变宽了

image.png

grid直角坐标系,默认是在左上角

image.png

image.png

给左和上设置位置距离,就能看到数据了 注意:背景色的设置就是在grid中

image.png

再看一下x轴的配置需要注意的点,数据顶头显示

给xData增加上数据

image.png

顶头显示这个配置是boundaryGap

image.png

image.png

取消注释后

image.png

Y值为负数时,区域样式的设置中 渐变色如何设置

当series的data负数时,只是设置areaStyle
image.png

渐变色会朝上,相当于渐变色的方向一直朝向的是 x轴

image.png

通过设置 origin: "start"

image.png

方向可以调整正确

image.png