echarts 自定义legend 初始化为灰色

<template>

  <div>

    <!--城市消防安全综合态势 图表 -->

    <div id="main" style="width: 480px; height: 260px"></div>

  </div>

</template>

 

<script>

import cityData from "@/mock/rightCity";

var echarts = require("echarts"); // 引入echart 或者 import echarts from "echarts"

export default {

  name: "home",

 

  // 写在mounted 函数中

  mounted() {

    // 基于准备好的dom,初始化echarts实例

 

    var myChart = echarts.init(document.getElementById("main"));

    var option = {

      backgroundColor: "#091c2e",

      tooltip: {

        trigger: "axis",

        show: true,

      },

      legend: {

        show: true,

        icon: "rect",

        padding: [170, 50, 0, 0],

        // bottom: 0,

        textStyle: {

          fontSize: 12,

          color: "#fff",

        },

        data: [

          {

            name: "全市",

            icon: "rect",

          },

          {

            name: "黄浦",

            icon: "rect",

          },

          {

            name: "徐汇",

            icon: "rect",

          },

          {

            name: "长宁",

            icon: "rect",

          },

          {

            name: "静安",

            icon: "rect",

          },

          {

            name: "普陀",

            icon: "rect",

          },

          {

            name: "虹口",

            icon: "rect",

          },

          {

            name: "杨浦",

            icon: "rect",

          },

          {

            name: "闵行",

            icon: "rect",

          },

          {

            name: "宝山",

            icon: "rect",

          },

          {

            name: "嘉定",

            icon: "rect",

          },

          {

            name: "松江",

            icon: "rect",

          },

          {

            name: "金山",

            icon: "rect",

          },

          {

            name: "青浦",

            icon: "rect",

          },

          {

            name: "奉贤",

            icon: "rect",

          },

          {

            name: "崇明",

            icon: "rect",

          },

          {

            name: "浦东",

            icon: "rect",

          },

        ],

        selected: {

          全市: true,

          黄浦: true,

          徐汇: true,

          长宁: true,

          静安: true,

          普陀: false,

          虹口: false,

          杨浦: false,

          闵行: false,

          宝山: false,

          嘉定: false,

          松江: false,

          金山: false,

          青浦: false,

          奉贤: false,

          崇明: false,

          浦东: false,

        },

      },

      grid: {

        left: "5%",

        right: "5%",

        top: "10%",

        bottom: "40%",

        containLabel: true,

      },

      xAxis: {

        axisLine: {

          show: true,

        },

        axisTick: {

          show: false,

        },

        axisLabel: {

          interval: 0,

        },

        axisLine: {

          // 坐标轴刻度相关设置。

          show: true,

          alignWithLabel: false, // 类目轴中在 boundaryGap 为 true 的时候有效,可以保证刻度线和标签对齐。

          interval: "auto", // 坐标轴刻度的显示间隔,在类目轴中有效。默认同 axisLabel.interval 一样。

          inside: false, // 坐标轴刻度是否朝内,默认朝外。

          length: 5, // 坐标轴刻度的长度。

          lineStyle: {

            // 刻度线的样式设置。

            color: "#fff", // 刻度线的颜色,默认取 axisTick.lineStyle.color。

            width: 1,

            type: "solid", // (实线:'solid',虚线:'dashed',星罗棋布的:'dotted')

            shadowColor: "rgba(0, 0, 0, 0.5)",

            shadowBlur: 10,

            shadowOffsetX: 0,

            shadowOffsetY: 0,

            opacity: 1,

          },

        }, // 坐标轴轴线相关设置。

        // data: ['A', 'B', 'C', 'D', 'E', 'F', 'G']

        data: ["2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020"],

      },

      yAxis: {

        type: "value",

        min: "0",

        max: "20",

        axisLine: {

          show: true,

        },

        axisTick: {

          show: false,

        },

        axisLine: {

          // 坐标轴刻度相关设置。

          show: true,

          alignWithLabel: false, // 类目轴中在 boundaryGap 为 true 的时候有效,可以保证刻度线和标签对齐。

          interval: "auto", // 坐标轴刻度的显示间隔,在类目轴中有效。默认同 axisLabel.interval 一样。

          inside: false, // 坐标轴刻度是否朝内,默认朝外。

          length: 5, // 坐标轴刻度的长度。

          lineStyle: {

            // 刻度线的样式设置。

            color: "#fff", // 刻度线的颜色,默认取 axisTick.lineStyle.color。

            width: 1,

            type: "solid", // (实线:'solid',虚线:'dashed',星罗棋布的:'dotted')

            shadowColor: "rgba(0, 0, 0, 0.5)",

            shadowBlur: 10,

            shadowOffsetX: 0,

            shadowOffsetY: 0,

            opacity: 1,

          },

        }, // 坐标轴轴线相关设置。

      },

      series: [

        {

          name: "全市",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#3366cc",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.HP,

        },

        {

          name: "黄浦",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#dc3912",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.HP,

        },

        {

          name: "徐汇",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#ff9900",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.XH,

        },

        {

          name: "长宁",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#CDCDCD",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.CN,

        },

        {

          name: "静安",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#109618",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.JA,

        },

        {

          name: "普陀",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#990099",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.PT,

        },

        {

          name: "虹口",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#0099c6",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.HK,

        },

        {

          name: "杨浦",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#dd4477",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.YP,

        },

        {

          name: "闵行",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#66aa00",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.MH,

        },

        {

          name: "宝山",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#b82e2e",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.BS,

        },

        {

          name: "嘉定",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#994499",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.JD,

        },

        {

          name: "松江",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#22aa99",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.SJ,

        },

        {

          name: "金山",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#aaaa11",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.JS,

        },

        {

          name: "青浦",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#6633cc",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.QP,

        },

        {

          name: "奉贤",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#329262",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.FX,

        },

        {

          name: "崇明",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#a9c413",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.CM,

        },

        {

          name: "浦东",

          type: "line",

          smooth: true,

          symbol: "circle",

          symbolSize: 13,

          lineStyle: {},

          itemStyle: {

            color: "#8b0707",

            borderColor: "#fff",

            borderWidth: 2,

          },

 

          data: cityData.PD,

        },

      ],

    };

    myChart.setOption(option);

    // myChart.off('legendselectchanged') //解决重复触发

 

    // myChart.on("legendselectchanged", function (params) {

    //   myChart.setOption({

    //     legend: { selected: { [params.name]: true } },

    //   });

    //   console.log("点击了", params.name);

    //   // do something

    // });

    

  },

};

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts中,可以通过自定义legend来实现对图例的样式进行修改。通常情况下,可以使用官方提供的模板加载出样式,然后根据需求进行自定义修改。为了自定义legend的样式,可以使用legend.icon参数进行设置。通过设置legend.icon为自定义的图标,可以实现对图例的样式进行个性化的定制。此外,还可以使用图片链接或者base64图片来作为legend.icon的样式,以实现更多样式的自定义。另外,也可以使用矢量路径来设置legend.icon的样式,通过创建矢量路径并导出,可以实现更加灵活和多样化的图例样式。总结来说,Echarts提供了多种方式来自定义legend的样式,包括使用模板加载样式、自定义图标、使用图片链接或base64图片以及使用矢量路径来实现个性化的图例样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [echarts自定义legend样式的详细图文教程](https://blog.csdn.net/snow_living/article/details/127791432)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [echarts 图例(legend icon)图标自定义的几种方式](https://blog.csdn.net/rudy_zhou/article/details/111474179)[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^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值