2301_79179032 2025-08-05 18:00 采纳率: 63%
浏览 16
已结题

echarts折线图更改legend的样式

echarts折线图更改legend的样式,改为和照片一样的相同颜色的线


option = {
  title: {
    text: 'Stacked Line'
  },
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
  },
  toolbox: {
    feature: {
      saveAsImage: {}
    }
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: 'Email',
      type: 'line',
      stack: 'Total',
      data: [120, 132, 101, 134, 90, 230, 210]
    },
    {
      name: 'Union Ads',
      type: 'line',
      stack: 'Total',
      data: [220, 182, 191, 234, 290, 330, 310]
    },
    {
      name: 'Video Ads',
      type: 'line',
      stack: 'Total',
      data: [150, 232, 201, 154, 190, 330, 410]
    },
    {
      name: 'Direct',
      type: 'line',
      stack: 'Total',
      data: [320, 332, 301, 334, 390, 330, 320]
    },
    {
      name: 'Search Engine',
      type: 'line',
      stack: 'Total',
      data: [820, 932, 901, 934, 1290, 1330, 1320]
    }
  ]
};

img

  • 写回答

4条回答 默认 最新

  • 檀越@新空间 2025-08-05 18:01
    关注

    晚上好🌙🌙🌙
    本答案参考通义千问

    要更改 ECharts 折线图中 legend 的样式,并使 legend 中的图例项与对应系列的线条颜色保持一致,你可以通过设置 legenditemStyle 属性来实现。此外,如果你希望 legend 的图标(小方块)也与线条颜色一致,可以通过自定义 legend.icon 来实现。


    ✅ 重点说明:

    • legend.itemStyle.color:用于设置 legend 图例的颜色。
    • series.lineStyle.color:用于设置折线图的颜色。
    • legend.icon:可以自定义 legend 图例的形状和颜色。

    ✅ 修改后的完整代码如下:

    option = {
      title: {
        text: 'Stacked Line'
      },
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'],
        // 设置图例项的颜色(可选)
        itemStyle: {
          color: '#5470c6' // 默认颜色,如果需要动态匹配,可结合 series 颜色
        },
        // 自定义图例图标为圆形或方形(根据需求选择)
        icon: 'circle' // 或 'rect'(矩形)
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      toolbox: {
        feature: {
          saveAsImage: {}
        }
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: 'Email',
          type: 'line',
          stack: 'Total',
          lineStyle: {
            color: '#5470c6' // 设置该系列的折线颜色
          },
          data: [120, 132, 101, 134, 90, 230, 210]
        },
        {
          name: 'Union Ads',
          type: 'line',
          stack: 'Total',
          lineStyle: {
            color: '#91cc75'
          },
          data: [220, 182, 191, 234, 290, 330, 310]
        },
        {
          name: 'Video Ads',
          type: 'line',
          stack: 'Total',
          lineStyle: {
            color: '#fac858'
          },
          data: [150, 232, 201, 154, 190, 330, 410]
        },
        {
          name: 'Direct',
          type: 'line',
          stack: 'Total',
          lineStyle: {
            color: '#ee6666'
          },
          data: [320, 332, 301, 334, 390, 330, 320]
        },
        {
          name: 'Search Engine',
          type: 'line',
          stack: 'Total',
          lineStyle: {
            color: '#73c0de'
          },
          data: [820, 932, 901, 934, 1290, 1330, 1320]
        }
      ]
    };
    

    🛠️ 解决方案步骤:

    1. series 中为每个折线图设置 lineStyle.color

      • 确保每条折线的颜色是唯一的,并且符合你想要的样式。
    2. legend 中设置 itemStyle.color

      • 如果你希望所有图例颜色统一,可以在这里设置一个默认颜色。
      • 如果希望每个图例颜色与对应的折线一致,可以在 legend 中使用 dataitemStyle 动态绑定颜色(需配合 ECharts 的 API 实现)。
    3. 设置 legend.iconcirclerect

      • 这决定了图例的形状,比如圆形或矩形。

    📌 注意事项:

    • 如果你需要 动态绑定图例颜色,可以使用 ECharts 的 color 配置项,或者通过 JavaScript 动态生成 legend 数据和颜色。
    • 如果你使用的是 Vue/React/Angular 等框架,也可以将颜色作为变量传入配置对象中。

    如需进一步定制图例样式(如字体、间距等),可参考 ECharts 官方文档中的 legend 配置项。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 8月5日