Echarts折线图实现区域颜色渐变

效果图:

 渐变核心代码:

 series: [
      //第一个区域
      {
        name: "点击率日环比",
        type: "line",
        data:  [-12, 37, -69, -2, -48, 67, 94],
        color: "#FF9F2F",
        symbolSize: 6, //设定实心点的大小
        label: {
          show: true, // 在折线拐点上显示数据
          fontSize: 8,
          color: "#fff",
          fontWeight: 10,
        },
        areaStyle: {//区域样式
          origin: "start",//向最小值方向渐变,y轴有负值要写
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(255,159,47, 0.2)",
            },
            {
              offset: 1,
              color: "rgba(255,159,47, 0)",
            },
          ]),
        },
      },
      //第二个区域
      {
        name: "下载率日环比",
        type: "line",
        data: [-48, -4, 41, -3, 19, -60, 7],
        color: "#2FF5FF",
        symbolSize: 6,
        label: {
          show: true,
          fontSize: 8,
          color: "#fff",
          fontWeight: 10,
        },
        areaStyle: {
          origin: "start",
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(47,245,255, 0.2)",
            },
            {
              offset: 1,
              color: "rgba(47,245,255, 0)",
            },
          ]),
        },
      },
    ],

如果你想看别的部分,可以看整体代码:

<template>
  <div id="stacked-line"></div>
</template>

<script setup>
import * as echarts from "echarts";
import { onMounted } from "vue";
var option;
var lineData = [
  [-12, 37, -69, -2, -48, 67, 94],
  [-48, -4, 41, -3, 19, -60, 7],
];
onMounted(() => {
  var myChart = echarts.init(document.getElementById("stacked-line"));
  option = {
    tooltip: {
      trigger: "axis",
      textStyle: {
        align: "left",
        color: "#fff",
        fontSize: "12px",
      },
      backgroundColor: "rgba(82,82,82,0.4)",
      borderColor: "rgba(82,82,82,0.4)",
    },
    legend: {
      //字体对应折线标识
      data: ["点击率日环比", "下载率日环比"],
      right: "10%",
      top: "5%",
      textStyle: {
        color: "#fff",
        fontSize: "7px",
      },
      itemHeight: 6, //圆心
      itemWidth: 0, //折线
      itemGap: 16, //间隔
    },
    //折线图位置
    grid: {
      left: "1%",
      right: "4%",
      bottom: "1%",
      top: "18%",
      containLabel: true, //刻度标签
    },
    xAxis: {
      boundaryGap: false,
      axisLabel: {
        fontSize: "8px", //文字的字体大小
        color: "#fff",
      },
      axisTick: {
        //x轴刻度
        show: false,
      },
      axisLine: {
        //x轴线样式
        show: false,
        lineStyle: {
          color: "#ccc",
          type: [0, 5],
          dashOffset: 5,
        },
      },
      data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
    },
    yAxis: {
      type: "value",
      interval: 50,
      min: -100,
      max: 100,
      splitLine: {
        //分割线
        lineStyle: {
          color: "rgba(255,255,255,0.2)",
          type: [10, 10],
          dashOffset: 5,
        },
      },
      axisLabel: {
        color: "#fff",
        fontSize: "8px",
      },
    },
    series: [
      {
        name: "点击率日环比",
        type: "line",
        data: lineData[0],
        color: "#FF9F2F",
        symbolSize: 6, //设定实心点的大小
        label: {
          show: true, // 在折线拐点上显示数据
          fontSize: 8,
          color: "#fff",
          fontWeight: 10,
        },
        areaStyle: {
          origin: "start",
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(255,159,47, 0.2)",
            },
            {
              offset: 1,
              color: "rgba(255,159,47, 0)",
            },
          ]),
        },
      },
      {
        name: "下载率日环比",
        type: "line",
        data: lineData[1],
        color: "#2FF5FF",
        symbolSize: 6,
        label: {
          show: true,
          fontSize: 8,
          color: "#fff",
          fontWeight: 10,
        },
        areaStyle: {
          origin: "start",
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: "rgba(47,245,255, 0.2)",
            },
            {
              offset: 1,
              color: "rgba(47,245,255, 0)",
            },
          ]),
        },
      },
    ],
  };
  myChart.setOption(option, true);
});
</script>

<style lang="scss" scoped>
#stacked-line {
  width: 100%;
  height: 100%;
}
</style>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要为echarts折线图设置渐变色背景,可以使用areaStyle属性和LinearGradient函数来实现。在series中设置areaStyle的color属性为一个LinearGradient对象,通过设置colorStops属性来定义渐变色的起始和结束颜色。例如,可以使用以下代码来设置渐变色背景: ```javascript series: \[ { type: 'line', areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, \[ { offset: 0, color: 'rgba(0, 50, 100, 1)' }, { offset: 0.5, color: 'rgba(0, 50, 100, 0.5)' }, { offset: 1, color: 'rgba(0, 50, 100, 0)' } \]) }, // 其他配置项... } \] ``` 这段代码将折线图的背景设置为从上到下渐变颜色,起始颜色为rgba(0, 50, 100, 1),中间颜色为rgba(0, 50, 100, 0.5),结束颜色为rgba(0, 50, 100, 0)。你可以根据需要调整渐变色的起始和结束颜色,以及中间的颜色和透明度。\[1\]\[2\] 注意:在使用渐变色时,最好使用rgba格式的颜色,以便更好地控制透明度。\[2\] #### 引用[.reference_title] - *1* [Echarts折线图折线呈现为渐变线条](https://blog.csdn.net/u010234868/article/details/131475802)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Echarts 折线图 渐变色 不堆叠](https://blog.csdn.net/Windyluna/article/details/120025888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值