Echart 的常用配置

1. 常用的配置字段

 1、折线图
chartOption = {
   color: this.yearColor,  // 图表的默认颜色列表,多条线时依次循环
    tooltip: {
      trigger: 'axis',
    },
    grid: { left: 0, right: 0, bottom: 0, top: 30, containLabel: true },
    xAxis: {
      nameTextStyle: { color: '#000000A3' }, // 配置x轴的字体颜色
      type: 'category',         // 设置为分类轴
      axisLabel: { lineHeight: 28 }, // 设置
      boundaryGap: true,
      axisLine: { onZero: false }, // X 轴或者 Y 轴的轴线是否在另一个轴的 0 刻度上,只有在另一个轴为数值轴且包含 0 刻度时有效。
    },
    yAxis: {
      nameTextStyle: { fontSize: 12, padding: [10, 20, 0, 0], color: 'rgba(0,0,0,0.56)' },
      type: 'value',
      axisLine: { show: false }, // 是否显示
      splitNumber: 1,  // 坐标轴显示的分割段数
    },
    series: [
    {
        type: 'line',  // 图的类型,饼图,折线图,柱状图等
        symbol: 'none',
        markLine: {   // 设置图表标线,比如图的平均线,最大值等
          symbol: 'none',  // 标记的图形,例如'circle', 'rect', 'roundRect'
          precision: 2,  // 标线数值的精度,在显示平均值线的时候有用
          label: {
            show: true,
            align: 'left', // 平均线文字的左对齐
            position: 'start', // 平均线文字显示在x轴的开始位置,middle,end等
            verticalAlign: 'bottom', //文字显示在平均线的上方或下方
            color: 'rgba(0,0,0,0.56)', // 设置标线的颜色
            lineHeight: 24,
            fontSize: 14,
            padding: [0, 10, 0, 0], //  文字块的内边距
          },
          data: [{ name: '平均值', type: 'average' ,yAxis: 100}], // yAxis: Y 值为给定值的标记线,仅对数据值是一项的设置有效
        },
        endLabel: { show: false }, // 折线端点的标签
      },
      
    ],
  };

2、柱状图常用配置


prototypeOption: EChartsOption = {
    // 设置图表自带的图例的属性,形状,宽高,颜色
    legend: { icon: 'rect', itemWidth: 8, itemHeight: 8, textStyle: { color: ' rgba(0, 0, 0, 0.56)' } },
    // 设置图表组件离容器四周的距离,containLabel:grid 区域是否包含坐标轴的刻度标签
    grid: { left: 20, right: 16, bottom: 0, top: 30, containLabel: true },
    tooltip: {
      show: true,
      trigger: 'axis', // 触发类型,'item','axis','none'
      formatter: params => {
        if (!(params instanceof Array)) {
          params = [params];
        }
        return params
          .map((param, index) => {
            if (!param.value) {
              param.value = 0;
            }
            if (index < 2) {
              return `${index === 0 ? `${param.name}<br />` : ''}<div class="flex-box"><div class="mr-md">${param.marker}${
                param.seriesName
              }</div><div class="text-right">${this.metricsService.ReportMoneyText(Number(value))}</div></div>
                ${index === 1 ? `<div class="pt-xs text-disable">点击图表查看流水明细</div>` : ''}`;
            }
          })
          .join('');
      }
    },
    color: [ChartParameters.INCOME_COLOR, ChartParameters.EXPENSES_COLOR],
    xAxis: [{ type: 'category', axisLabel: { color: 'rgba(0,0,0,0.56)' }, axisLine: { show: false }, axisTick: { show: false } }],
    yAxis: [
       {
          type: 'value',
          name: '万元',
          nameTextStyle: { padding: [0, 40, 0, 0], color: 'rgba(0,0,0,0.56)' },
          axisLabel: { formatter: (v: any) => parseInt((v / 10000).toString()).toString(), color: 'rgba(0,0,0,0.56)' },
          axisLine: { show: false },
          axisTick: { show: false },
          splitNumber: 2, // 坐标轴分割段数
        }
      ],
    series: [ barMaxWidth:柱条最大宽度度,barMinHeight:最低高度,barGap:不同系列的柱间距离的百分比,stack:数据堆叠
      { name: '贷款、借款流入', type: 'bar', data: [], barMaxWidth: 12, barMinHeight: 1, barGap: '10%', stack: 'cr' },
      { name: '偿还贷款、借款', type: 'bar', data: [], barMaxWidth: 12, barMinHeight: 1, barGap: '10%', stack: 'dr' },
      { name: '贷款、借款流入', type: 'bar', data: [], barMaxWidth: 12, barMinHeight: 1, barGap: '10%', stack: 'cr' },
      { name: '偿还贷款、借款', type: 'bar', data: [], barMaxWidth: 12, barMinHeight: 1, barGap: '10%', stack: 'dr' },
      {
        name: '流入',
        data: this.getOptionList(list, maxValue, isAmount ? 'amount' : 'transactionNumber'),
        color: this.paymentFlag === 'R' ? ChartParameters.INCOME_COLOR : ChartParameters.EXPENSES_COLOR,
        borderRadius: [2, 2, 0, 0], // 柱状图显示圆角
        label: {
           formatter: params => {
             return [
                `{a|${isAmount ? this.metricsService.ReportMoneyText(Number(params.value)) : `${params.value}笔`}}`,
                `{b|${this.standardPercentPipe.transform(percents[params.dataIndex] || 0)}}`
              ].join('\n');
            },
         show: true,
         position: 'top',
         rich: {  // 通过富文本来显示丰富的标签样式
            a: {
                fontSize: '12px',
                color: 'rgba(0, 0, 0, 0.88)',
                padding: [0, 0, 0, isAmount ? 0 : 4]
              },
              b: {
                fontSize: '12px',
                padding: [4, 0, 0, isAmount ? 10 : 0],
                color: 'rgba(0, 0, 0, 0.56)'
              }
            }
          }
      }
    ]
  };

3、柱状图图例与图例旁边的文字垂直居中

 legend: {
      icon: 'roundRect',
      itemWidth: 8,
      itemHeight: 8,
      textStyle: { color: 'rgba(0, 0, 0, 0.56)', padding: [0, 5, -4, 0], rich: { a: { verticalAlign: 'middle' } } }
    },

4、点击柱状图上任意位置,获取x轴值

 html
         <div
            echarts
            [options]="prototypeOption"
            [merge]="stackMergeOption"
            [theme]="'customed'"
            style="height: 240px"
            (chartInit)="onChartInit($event, 'chartInstance')"
          ></div>

js
常用配置
  prototypeOption: EChartsOption = {
    legend: {
      icon: 'roundRect',
      itemWidth: 8,
      itemHeight: 8,
      textStyle: { color: 'rgba(0, 0, 0, 0.56)', padding: [0, 5, -4, 0], rich: { a: { verticalAlign: 'middle' } } }
    },

    grid: { left: 15, right: 16, bottom: 0, top: 50, containLabel: true },
    tooltip: {
      show: true,
      trigger: 'axis',
      formatter: params => {
        if (!(params instanceof Array)) {
          params = [params];
        }
        return params
          .map((param, index) => {
            if (!param.value) {
              param.value = 0;
            }

            const value = param.data < 0 ? -1 * Number(param.value) : Number(param.value);
            return `${index === 0 ? `${param.name}<br />` : ''}<div class="flex-box"><div class="mr-md">${param.marker}${
              param.seriesName
            }</div><div class="text-right">${this.metricsService.ReportMoneyText(value)}</div></div>
              ${index === params.length - 1 ? `<div class="pt-xs text-disable">点击图表查看流水明细</div>` : ''}`;
          })
          .join('');
      }
    },
    xAxis: [{ type: 'category', data: [], axisLabel: { color: 'rgba(0,0,0,0.56)' } }],
    yAxis: [
      {
        type: 'value',
        name: '万元',
        nameTextStyle: { padding: [0, 40, 0, 0], color: 'rgba(0,0,0,0.56)' },
        axisLabel: { formatter: (v: any) => parseInt((v / 10000).toString()).toString(), color: 'rgba(0,0,0,0.56)' },
        axisLine: { show: false },
        splitNumber: 3
      }
    ]
  };

处理点击事件:
import {  ChangeDetectorRef } from '@angular/core';
import { EChartsOption } from 'echarts';

 constructor(  private cdr: ChangeDetectorRef) {}

  onChartInit(e, instance: string) {
    this[instance] = e;
    this[instance].getZr().on('click', params => {
      const pointInPixel = [params.offsetX, params.offsetY];
      if (this[instance].containPixel('grid', pointInPixel)) {
        const pointInGrid = this[instance].convertFromPixel({ seriesIndex: 0 }, pointInPixel);
        const xIndex = pointInGrid[0];
        const op = this[instance].getOption();
        const month = op.xAxis[0].data[xIndex]; // x轴对应的坐标
        this.onChartBarClick({ name: month });
        this.cdr.detectChanges();
      }
    });
  }

参考文章:

echarts折线图设置渐变半透明面积颜色_suoh's Blog的博客-CSDN博客_echarts折线图面积图渐变色echarts的X轴显示特定个数label并修改样式_echarts x轴显示个数_keloneko的博客-CSDN博客
 

Echarts点击多组数据多个柱子中的一个柱子,获取当前点击的是第几组数据,并获取点击的是当前组别第几根柱子,以及对应横坐标,_tangdou369098655的博客-CSDN博客_echarts柱状图每列两条数据怎么知道点的是哪一条


echarts--点击图表任意位置,获取到该点的XY坐标值 - 简书

 

echarts的legend的小图标与文本垂直对齐_echarts legend垂直居中_斯图尔te的博客-CSDN博客

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
echarts中,可以通过设置柱子的itemStyle属性来实现堆叠柱状图柱子的圆角效果。有几种不同的写法可以实现这个效果。 第一种写法是在每个系列的数据项中设置itemStyle属性,根据条件判断是否给柱子添加圆角效果。具体的代码如下所示: ``` series.push({ name: legs[i], type: 'bar', stack: 'x', data: tempNum, itemStyle: { barBorderRadius: [4, 4, 0, 0] } }); ``` 以上代码会给每个系列的每个数据块都添加圆角效果。 第二种写法是直接给整个系列设置itemStyle属性,这样会应用到该系列的每一个数据块身上。具体的代码如下所示: ``` series: { name: '分数', type: 'bar', data: [], itemStyle: { barBorderRadius: [4, 4, 0, 0] } } ``` 另外,如果要实现上面的数据为0时,相邻下面的柱子处理为圆角的效果,可以使用下面的解决方法: ``` { name: 'Bing', type: 'bar', stack: 'Search Engine', emphasis: { focus: 'series' }, data: [ { value: 50, label: {}, itemStyle: { // 判断红色的第一个数据是否为0,如果不为0,则设置为barBorderRadius: [0, 0, 0, 0;如果为0,则设置为barBorderRadius: [12, 12, 0, 0] barBorderRadius: [12, 12, 0, 0] } }, { value: 50, label: {}, itemStyle: { barBorderRadius: [0, 0, 0, 0] } } ] } ``` 以上就是关于echarts堆叠柱状图柱子的圆角效果的几种写法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [echarts 堆叠柱状图 顶部圆角效果](https://blog.csdn.net/m0_48571414/article/details/126305173)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [echarts 堆叠柱状图 顶端圆角](https://blog.csdn.net/kangmingyaner/article/details/122086892)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值