亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Echarts如何自定義label標(biāo)簽的樣式(formatter,rich,添加圖標(biāo)等操作)

 更新時間:2023年02月16日 11:14:25   作者:前端小白在寫bug  
通常情況下,echarts中對于圖像的設(shè)置是統(tǒng)一的,下面這篇文章主要給大家介紹了關(guān)于Echarts如何自定義label標(biāo)簽的樣式的相關(guān)資料,包括formatter,rich,添加圖標(biāo)等操作,需要的朋友可以參考下

前言

最近公司項(xiàng)目中在使用 Echarts 繪制圖表時,由于默認(rèn)的 label 標(biāo)簽不能滿足設(shè)計(jì)稿需求,所以研究了對 label標(biāo)簽進(jìn)行格式化的方案,了解到可以使用 formatter 內(nèi)容格式器對標(biāo)簽內(nèi)容進(jìn)行調(diào)整, formatter 支持字符串模板和回調(diào)函數(shù)兩種形式,下面以餅圖為例對 formatter 格式化標(biāo)簽 label標(biāo)簽遇到的情況記錄如下

1、默認(rèn)效果

1.1) 默認(rèn)label 如下

1.2)默認(rèn)樣式代碼如下

option = {
  title: {
    text: 'Referer of a Website',
    subtext: 'Fake Data',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: 'Access From',
      type: 'pie',
      radius: '50%',
      data: [
        { value: 1048, name: 'Search Engine' },
        { value: 735, name: 'Direct' },
        { value: 580, name: 'Email' },
        { value: 484, name: 'Union Ads' },
        { value: 300, name: 'Video Ads' }
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
};

2、使用字符串模板進(jìn)行格式化

2.1) 通過 formatter 進(jìn)行文本的格式化,效果如下

2.2) 樣式代碼如下

這里使用字符串模板進(jìn)行設(shè)置,其中模板變量有{a}, , {c}, ublnpf9mb,在不同圖表類型下代表含義分別為
折線圖、柱狀圖、K線圖:{a} (系列名稱)、(類目值),{c}(數(shù)值),ublnpf9mb(無)
散點(diǎn)圖(氣泡)圖 : {a}(系列名稱),(數(shù)據(jù)名稱),{c}(數(shù)值數(shù)組), ublnpf9mb(無)
地圖 : {a}(系列名稱),(區(qū)域名稱),{c}(合并數(shù)值), ublnpf9mb(無)
餅圖、儀表盤、漏斗圖: {a}(系列名稱),(數(shù)據(jù)項(xiàng)名稱),{c}(數(shù)值), ublnpf9mb(百分比)

	series: [
          {
            name: "占比",
            type: "pie",
            radius: ["30%", "55%"],
            center: ["50%", "50%"],
            avoidLabelOverlap: false,
            label: {
              show: true,
              formatter: "{d|ublnpf9mb%} \n {b|}",
              rich: {
                d: {
                  color: '#fff',
                  fontSize: 16,
                  lineHeight: 24,
                  height: 24
                },
                b: {
                  color: "#D3EBFF",
                  fontSize: 14,
                  lineHeight: 20,
                  align: 'left',
                }
              },
              textStyle: {
                color: "#fff",
                align: "right",
                fontSize: 14,
              },
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
            data: this.seriesData,
          },
        ],
      };

3、使用富文本標(biāo)簽樣式

此方法可參考官網(wǎng)api: https://echarts.apache.org/zh/option.html#series-pie.label.formatter

3.1)雖然 label 無法設(shè)置 html 文本,echarts 提供了豐富的文本標(biāo)簽配置項(xiàng)進(jìn)行樣式設(shè)置,效果圖如下:

3.2) 樣例代碼如下:

echarts 提供了豐富的文本標(biāo)簽配置項(xiàng),包括:
字體基本樣式設(shè)置:fontStyle、fontWeight、fontSize、fontFamily
文字顏色:color
文字描邊:textBorderColor、textBorderWidth
文字陰影:textShadowColor、textShadowBlur、textShadowOffsetX、textShadowOffsetY
文本塊或文本片段大小:lineHeight、width、height、padding
文本塊或文本片段的對齊:align、verticalAlign
文本塊或文本片段的邊框、背景(顏色或圖片):backgroundColor、borderColor、borderWidth、borderRadius
文本塊或文本片段的陰影:shadowColor、shadowBlur、shadowOffsetX、shadowOffsetY
文本塊的位置和旋轉(zhuǎn):position、distance、rotate

	label: {
        formatter: '{a|{a}}\n{hr|}\n  {b|:}{c}  {per|ublnpf9mb%}  ',
        backgroundColor: '#F6F8FC',
        borderColor: '#8C8D8E',
        borderWidth: 1,
        borderRadius: 4,
        rich: {
          a: {
            color: '#6E7079',
            lineHeight: 22,
            align: 'center'
          },
          hr: {
            borderColor: '#8C8D8E',
            width: '100%',
            borderWidth: 1,
            height: 0
          },
          b: {
            color: '#4C5058',
            fontSize: 14,
            fontWeight: 'bold',
            lineHeight: 33
          },
          per: {
            color: '#fff',
            backgroundColor: '#4C5058',
            padding: [3, 4],
            borderRadius: 4
          }
        }
      },

4、使用回調(diào)函數(shù)進(jìn)行內(nèi)容格式化

4.1)通過回調(diào)函數(shù)方式可以對格式化內(nèi)容進(jìn)行更自由的設(shè)置,比如下面樣例只有“直接訪問”這個區(qū)塊使用大標(biāo)簽,其他都是用小標(biāo)簽:

4.2) 樣式代碼如下:

	label: {
         normal: {
           formatter: (params) => {
             console.log(params);
             //只有“直接訪問”使用大標(biāo)簽,其他都使用小標(biāo)簽
             if(params.data.name == '直接訪問') {
               return '{title|' + params.seriesName +'}{abg|}\n{bTitle|來源:}{bValue|'
               + params.data.name + '}\n{hr|}\n{leftGap|}{cIcon|}{cValue|'
               + params.data.value + '}\n{leftGap|}{dIcon|}{cValue|'
               + params.percent + '%}';
             } else {
               return '{bTitle|來源:}{bValue|' + params.data.name + '}';
             }
           },
           backgroundColor: '#eee', //標(biāo)簽背景顏色
           borderColor: '#777',  //外層邊框顏色
           borderWidth: 1, //外層邊框?qū)挾?
           borderRadius: 4, //外層邊框圓角
           rich: {
             title: {
               color: '#eee',
               align: 'center'
             },
             abg: {
              backgroundColor: '#333',
              width: '100%',
              align: 'right',
              height: 25,
              borderRadius: [4, 4, 0, 0]
            },
            bTitle: {
              color: '#333',
              height: 24,
              padding: [0, 5, 0, 5],
              align: 'left'
            },
            bValue: {
              color: '#333',
              height: 24,
              padding: [0, 5, 0, 5],
              align: 'right'
            },
            hr: {
              borderColor: '#777',
              width: '100%',
              borderWidth: 0.5,
              height: 0
            },
            leftGap: {
              width:5,
              align: 'left',
            },
            cIcon: {
              height: 15,
              align: 'left',
              margin: [0, 5, 0, 5],
              backgroundColor: {
                 image: '/static/countIcon.png'
              }
            },
            cValue: {
              height: 25,
              padding: [0, 10, 0, 30],
              align: 'right'
            },
            dIcon: {
              height: 15,
              align: 'left',
              backgroundColor: {
                 image: '/static/percentIcon.png'
              }
            },
            dValue: {
              height: 25,
              padding: [0, 10, 0, 30],
              align: 'right'
            },
           }
         }

總結(jié)

到此這篇關(guān)于Echarts如何自定義label標(biāo)簽的樣式的文章就介紹到這了,更多相關(guān)Echarts自定義label標(biāo)簽樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論