echarts图例多个一排,对齐换行和横向柱状图y轴label左对齐

 1、图例对齐

 设置legend宽度和padding

 legend: {

     

      orient: 'horizontal',

      bottom: '0%',

      left: '0%',

      icon: 'diamond',

      itemWidth: 12,

      itemHeight: 12,

      itemGap: 10,

      width: '280rem',

      align: 'left',

      textStyle: {

        color: '#fff',

        rich: {

          a: {

            width: 20,

            // color: () => {},

            height: 38,

            padding: [

              0, // 上

              40, // 右

              0, // 下

              0, // 左

            ],

          },

        },

      },

      formatter: (e) => {

        return '{a| ' + e + '}{b|' + ' ' + '}';

      },

    },

export const placeConfig = {
  option: {
    grid: {
      top: '1%',
      left: '2%',
      right: '2%',
      bottom: '0',
      containLabel: true,
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}:{c}({d}%)',
    },
    legend: {
      // show: true,
      orient: 'horizontal',
      bottom: '0%',
      left: '0%',
      icon: 'diamond',
      itemWidth: 12,
      itemHeight: 12,
      itemGap: 10,
      width: '280px',
    
      align: 'left',
      textStyle: {
        color: '#fff',
        rich: {
          a: {
            width: 20,
            // color: () => {},
            height: 38,
            padding: [
              0, // 上
              40, // 右
              0, // 下
              0, // 左
            ],
          },
        },
      },
      formatter: (e) => {
        return '{a| ' + e + '}{b|' + ' ' + '}';
        // return `${name}  ${((val / total) * 100).toFixed(2)}%`;
      },
    },
    labelLine: {
      lineStyle: {
        color: '#fff',
        wdith: 1,
      },
    },
    series: [
      {
        type: 'pie',
        radius: '54px',
        center: ['50%', '80px'],
        data: [],

        minAngle: 10,
        startAngle: 90,

        // alignTo: 'labelLine',
        label: {
          show: false,
          color: '#fff',
          overflow: 'none',
          formatter: (v) => {
            return `${v.data.rate}% \n ${v.data.name} `;
          },
        },
        labelLine: {
          show: true,
          // minTurnAngle: 270,
          // length: 10,
          // length2: 30,
        },
      },
    ],
  },
};

 

//饼图
export const pieConfig = {
  option: {
    grid: {
      top: '0',
      left: '3%',
      right: '3%',
      bottom: '0',
      containLabel: true,
    },
    tooltip: {
      trigger: 'item',
      // formatter: '{b}:{c}({d}%)',
      formatter: '{b}:{c}',
    },
    legend: {
      top: 'center',
      left: '40%',
      orient: 'vertical',
      width: 480,
      height: 180,
      itemGap: 20,
      // icon: 'circle',
      textStyle: {
        color: 'rgba(235, 245, 255, 1)',
        width: 200,
        height: 20,
      },
    },
    series: [
      {
        type: 'pie',
        radius: '60%',
        center: ['20%', '50%'],
        data: [],
        label: {
          show: false,
        },
      },
    ],
  },
};

 

const [personPie, setPersonPie] = useState(pieConfig);


const numsResult = _.cloneDeep(personPie);
const codeDatas = [{name:'xxx',value:123}]
numsResult.option.series[0].radius = ['40%', '70%'];
      // numsResult.option.legend.icon = 'rect';
      numsResult.option.legend.width = 20;
      numsResult.option.legend.itemHeight = 0;
      numsResult.option.legend.itemWidth = 0;
      numsResult.option.legend.borderRadius = 0;
      numsResult.option.series[0].data = codeDatas;
      numsResult.option.legend.formatter = (e) => {
        let data = codeDatas;
        let value = 0;
        let name = '';
        data.forEach((el) => {
          if (e == el.name) {
            name = el.name;
            value = el.value;
          }
        });
        let title = name;
        if (title.length > 30) {
          return ['{a|}' + '{b|' + title.substr(0, 15) + '...' + ': ' + value + '个' + '}'].join(
            '\n',
          );
        } else {
          return ['{a|}' + '{b|' + title + ': ' + value + '个' + '}'].join('\n');
        }
      };
      numsResult.option.legend.textStyle = {
        //rich放在textStyle里面
        // color: '#fff',

        rich: {
          a: {
            width: 39,
            height: 14,
            lineHeight: 15,
            backgroundColor: [],
            borderRadius: 6,
          },
          b: {
            lineHeight: 15,
            borderRadius: 15,
            padding: [0, 0, 0, 5],
          },
          c: {
            lineHeight: 15,
            borderRadius: 15,
            padding: [0, 0, 0, 5],
          },
        },
      };

 

 2、y轴label对齐

 

设置grid左边位置和label的宽度和边距,每个宽度的图表不一样数据长度不一样这些值都不一样,自己调试

grid: {
      top: '1%',
      left: '-100px',
      right: '5%',
      bottom: '5%',
      containLabel: true,
    }, 

yAxis: {
      type: 'category',
      axisLabel: {
        show: true,
        overflow: 'truncate',
        color: '#fff',
        width: 150,//label宽度
        margin: 120,//距离y轴的位置
        align: 'left',
      },
   
    },

//当日
export const todaybarChartConfig = {
  option: {
    grid: {
      top: '1%',
      left: '-100px',
      right: '5%',
      bottom: '5%',
      containLabel: true,
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999',
        },
      },
    },
    legend: {
      show: false,
      x: 'right', // 'center' | 'left' | {number},
      y: 'top', // 'center' | 'bottom' | {number}
      textStyle: { color: '#fff' },
      itemWidth: 15,
      itemHeight: 15,
      icon: 'rect',
    },
    yAxis: {
      type: 'category',

      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
      axisLabel: {
        show: true,
        overflow: 'truncate',
        color: '#fff',
        width: 150,
        margin: 120,
        align: 'left',
      },
      splitLine: {
        show: false,
      },
      splitArea: {
        show: false,
      },
      data: [],
    },
    xAxis: [
      {
        type: 'value',
        axisLabel: {
          color: '#fff',
        },
        nameTextStyle: {
          color: '#fff',
        },
        splitLine: {
          show: false,
        },
        axisLine: {
          show: false,
        },
        axisLabel: {
          color: '#fff',
          show: false,
        },
        axisTick: {
          show: false,
        },
      },
    ],
    series: [
      {
        type: 'bar',
        barMaxWidth: '8px',
        label: {
          show: true,
          position: 'right',
          color: '#fff',
        },
        itemStyle: {
          normal: {
            borderRadius: 10,//
            color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
              {
                offset: 0,
                color: 'rgba(0, 149, 255, 1)',
              },
              {
                offset: 1,
                color: 'rgba(55, 255, 253, 1)',
              },
            ]),
          },
        },
        data: [],
      },
    ],
  },
};

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ECharts图例换行对齐可以通过调整图例的排列方式来实现。具体的方法如下: 1. 使用legend.type属性指定图例的排列方式,可以使用'plain', 'scroll'或者'plain' + 'scroll',其中'plain'表示图例排列在一行或一列中,'scroll'表示图例可以通过滚动查看,'plain' + 'scroll'表示图例先按照一行或一列排列,当不足时自动换行或换列。 2. 使用legend.orient属性指定图例的方向,可以使用'horizontal'表示水平方向,'vertical'表示垂直方向。 3. 使用legend.width属性指定图例的宽度,可以使用像素值或百分比。 4. 使用legend.itemGap属性指定图例项之间的间距,可以使用像素值或百分比。 通过以上方法可以实现ECharts图例换行对齐。具体的示例代码如下: ```javascript option = { legend: { type: 'scroll', orient: 'horizontal', width: '80%', itemGap: 10, data: ['图例1', '图例2', '图例3', '图例4', '图例5', '图例6', '图例7', '图例8', '图例9', '图例10', '图例11', '图例12'] }, series: [ { name: '系列1', type: 'pie', radius: '55%', data: [ {value: 335, name: '图例1'}, {value: 310, name: '图例2'}, {value: 234, name: '图例3'}, {value: 135, name: '图例4'}, {value: 1548, name: '图例5'}, {value: 1548, name: '图例6'}, {value: 1548, name: '图例7'}, {value: 1548, name: '图例8'}, {value: 1548, name: '图例9'}, {value: 1548, name: '图例10'}, {value: 1548, name: '图例11'}, {value: 1548, name: '图例12'} ] } ] }; ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值