All files / progress si-chart-progress.component.ts

77.27% Statements 51/66
70.58% Branches 24/34
72.22% Functions 13/18
75.8% Lines 47/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                            1x               13x   6x             6x                 6x                             6x                                 6x                 6x 6x 6x 6x 6x       6x 6x   6x 6x 5x 25x 25x           6x       180x       25x 25x     25x     25x 25x   25x                         180x                                                                                               25x 25x       28x 28x 28x 28x 28x       25x 25x 25x                                         1x 1x 1x             1x 1x 2x 2x     2x     1x      
/**
 * Copyright (c) Siemens 2016 - 2026
 * SPDX-License-Identifier: MIT
 */
import { Component, input } from '@angular/core';
import { SiChartBaseComponent, echarts } from '@siemens/charts-ng/common';
import { SiCustomLegendComponent } from '@siemens/charts-ng/custom-legend';
import { SiChartLoadingSpinnerComponent } from '@siemens/charts-ng/loading-spinner';
import { PieSeriesOption } from 'echarts';
import { PieChart } from 'echarts/charts';
import { LegendComponent } from 'echarts/components';
 
import { ProgressChartSeries, ProgressValueUpdate } from './si-chart-progress.interface';
 
echarts.use([PieChart, LegendComponent]);
 
@Component({
  selector: 'si-chart-progress',
  imports: [SiCustomLegendComponent, SiChartLoadingSpinnerComponent],
  templateUrl: '../common/si-chart-base.component.html',
  styleUrl: '../common/si-chart-base.component.scss'
})
export class SiChartProgressComponent extends SiChartBaseComponent {
  /** The series for the chart. */
  readonly series = input<ProgressChartSeries[]>();
 
  /**
   * How big the data can get in degrees.
   *
   * @defaultValue 220
   */
  readonly dataAngle = input(220);
 
  // item width in % radius
  private itemWidth!: number;
 
  // item gap in % radius
  private itemGap!: number;
 
  // base radius of an item
  private baseRadius = 98;
 
  private sizeFactor!: number;
 
  // placement chart in % on canvas
  private center!: number;
 
  // color of disabled part of item
  private grey!: string;
 
  // where the data starts. not very intuitive:
  // 0° is at 3 o'clock, goes counterclockwise, but series drawing goes clockwise
  private startAngle!: number;
 
  protected override afterChartInit(): void {
    this.chart.on('legendselectchanged', (params: any) => {
      this.updateRadius(params.selected);
      this.updateEChart();
    });
  }
 
  protected override themeChanged(): void {
    this.applyOptions();
  }
 
  protected override handleSelectionChanged(event: any): void {
    super.handleSelectionChanged(event);
    this.updateRadius(event.selected);
    this.updateEChart();
  }
 
  protected override applyOptions(): void {
    this.actualOptions = {
      series: [],
      legend: [
        {
          data: []
        }
      ]
    };
 
    const dataAngle = this.dataAngle();
    this.startAngle = dataAngle === 360 ? 90 : 270 - (360 - dataAngle) / 2;
    this.itemWidth = this.getThemeCustomValue(['progress', 'itemWidth'], 6);
    this.itemGap = this.getThemeCustomValue(['progress', 'itemGap'], 6);
    this.grey = this.getThemeCustomValue(['progress', 'grey'], '#E6E6E6');
 
    // a factor to change the size of base radius and center related to
    // dataAngle proportionately
    this.sizeFactor = 1 - Math.sin((((this.dataAngle() - 180) / 2) * Math.PI) / 180);
    this.center = 50 + 25 * this.sizeFactor;
 
    const seriesValue = this.series();
    if (seriesValue) {
      seriesValue.forEach(series => {
        this.addSeries(series.name, series.percent);
        Iif (this.showLegend()) {
          this.addLegendItem(series.name);
        }
      });
    }
 
    this.applyTitles();
  }
 
  private labelFormatter(p: any): string {
    return p.seriesName + '\n' + Math.round((100 / this.dataAngle()) * p.value) + ' %';
  }
 
  private addSeries(name: string, percent: number): void {
    const optionSeries = this.actualOptions.series as PieSeriesOption[];
    const index = optionSeries.length;
 
    const offset =
      !this.showCustomLegend() && this.subTitle()
        ? this.getThemeCustomValue(['subTitle', 'legend', 'top'], 0)
        : 0;
    const top = !this.sizeFactor ? 32 + offset : Math.floor(offset / 2);
    const bottom = !this.sizeFactor ? undefined : -offset;
 
    const item: PieSeriesOption = {
      top,
      bottom,
      center: ['50%', this.center + '%'],
      name,
      type: 'pie',
      colorBy: 'series',
      radius: this.calculateRadius(index),
      avoidLabelOverlap: false,
      tooltip: { show: false },
      startAngle: this.startAngle,
      label: {
        show: false,
        formatter: p => this.labelFormatter(p),
        position: 'center'
      },
      labelLine: {
        show: false
      },
      emphasis: {
        label: {
          show: true,
          lineHeight: 30,
          fontSize: '30',
          fontWeight: 'bold'
        }
      },
      itemStyle: {
        borderWidth: 0
      },
      data: [
        {
          value: 0,
          name: 'v' + index,
          label: {
            lineHeight: 30,
            fontSize: 30,
            color: 'inherit'
          }
        },
        {
          value: 0,
          name: 'a' + index,
          itemStyle: { color: this.grey },
          emphasis: {
            scale: false,
            itemStyle: { color: this.grey }
          },
          cursor: 'default',
          label: { show: false }
        },
        {
          value: 0,
          name: 'b' + index,
          itemStyle: { color: 'rgba(0,0,0,0)' },
          cursor: 'default',
          label: { show: false }
        }
      ]
    };
 
    this.updateData(item, percent);
    optionSeries.push(item);
  }
 
  private updateData(series: any, percent: number): void {
    const angle = (this.dataAngle() * percent) / 100;
    series.data[0].value = angle;
    series.data[1].value = this.dataAngle() - angle;
    series.data[2].value = 360 - this.dataAngle();
    series.realValue = percent;
  }
 
  private calculateRadius(index: number): string[] {
    const outerRadius = this.baseRadius - index * (this.itemWidth + this.itemGap);
    const innerRadius = outerRadius - this.itemWidth;
    return [innerRadius + '%', outerRadius + '%'];
  }
 
  private updateRadius(selected: any): void {
    let visibleIndex = 0;
    const optionSeries = this.actualOptions.series as PieSeriesOption[];
    optionSeries.forEach(series => {
      if (selected[series.name!] === false) {
        return;
      }
      series.radius = this.calculateRadius(visibleIndex);
      visibleIndex++;
    });
  }
 
  protected override applyDataZoom(): void {}
 
  /**
   * Update single value of the progress chart.
   */
  changeSingleValue(index: number, percent: number): void {
    const optionSeries = this.actualOptions.series as PieSeriesOption[];
    this.updateData(optionSeries[index], percent);
    this.updateEChart();
  }
 
  /**
   * Update multiple values of the progress chart.
   */
  changeMultiValues(updateValues: ProgressValueUpdate[]): void {
    const optionSeries = this.actualOptions.series as PieSeriesOption[];
    updateValues.forEach(update => {
      const currentSeries = optionSeries[update.seriesIndex];
      Iif (!currentSeries) {
        return;
      }
      this.updateData(currentSeries, update.percent);
    });
 
    this.updateEChart();
  }
}