Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dynamic numberformat conversion by using current locale #2757

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { UserModule } from "./user/user.module";
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
CookieService,
{ provide: ErrorHandler, useClass: MyErrorHandler },
{ provide: LOCALE_ID, useValue: Language.DEFAULT.key },
{ provide: LOCALE_ID, useFactory: () => (Language.getByKey(localStorage.LANGUAGE) ?? Language.getByBrowserLang(navigator.language)).key },
// Use factory for formly. This allows us to use translations in validationMessages.
{ provide: FORMLY_CONFIG, multi: true, useFactory: registerTranslateExtension, deps: [TranslateService] },
DeviceDetectorService,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/edge/history/abstracthistorychart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
public setOptions(options: Chart.ChartOptions): Promise<void> {

return new Promise<void>((resolve) => {
const locale = this.service.translate.currentLang;

Check failure on line 118 in ui/src/app/edge/history/abstracthistorychart.ts

View workflow job for this annotation

GitHub Actions / build-ui

'locale' is assigned a value but never used
const yAxis: HistoryUtils.yAxes = { position: "left", unit: this.unit, yAxisId: ChartAxis.LEFT };
const chartObject: HistoryUtils.ChartData = {
input: [],
Expand Down Expand Up @@ -152,7 +152,7 @@
const value = tooltipItem.dataset.data[tooltipItem.dataIndex];

const customUnit = tooltipItem.dataset.unit ?? null;
return label.split(":")[0] + ": " + NewAbstractHistoryChart.getToolTipsSuffix("", value, formatNumber, customUnit ?? unit, "line", locale, translate, conf);
return label.split(":")[0] + ": " + NewAbstractHistoryChart.getToolTipsSuffix("", value, formatNumber, customUnit ?? unit, "line", translate, conf);
};

options.plugins.tooltip.callbacks.labelColor = (item: Chart.TooltipItem<any>) => {
Expand Down Expand Up @@ -242,7 +242,7 @@
});

// Only one yAxis defined
options = NewAbstractHistoryChart.getYAxisOptions(options, yAxis, this.translate, "line", locale, this.datasets, true);
options = NewAbstractHistoryChart.getYAxisOptions(options, yAxis, this.translate, "line", this.datasets, true);
options = NewAbstractHistoryChart.applyChartTypeSpecificOptionsChanges("line", options, this.service, chartObject);
options.scales[ChartAxis.LEFT]["stacked"] = false;
options.scales.x["stacked"] = true;
Expand Down
4 changes: 3 additions & 1 deletion ui/src/app/edge/history/storage/singlechart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as Chart from "chart.js";
import { DefaultTypes } from "src/app/shared/service/defaulttypes";
import { ChartAxis, YAxisType } from "src/app/shared/service/utils";
import { Language } from "src/app/shared/type/language"

Check failure on line 9 in ui/src/app/edge/history/storage/singlechart.component.ts

View workflow job for this annotation

GitHub Actions / build-ui

Missing semicolon

import { ChannelAddress, Edge, EdgeConfig, Service, Utils } from "../../../shared/shared";
import { AbstractHistoryChart } from "../abstracthistorychart";
Expand Down Expand Up @@ -208,6 +209,7 @@

private applyControllerSpecificChartOptions(options: Chart.ChartOptions) {
const translate = this.translate;
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;

options.scales[ChartAxis.LEFT].min = null;
options.plugins.tooltip.callbacks.label = function (tooltipItem: Chart.TooltipItem<any>) {
Expand All @@ -227,7 +229,7 @@
label = translate.instant("General.DISCHARGE");
}
}
return label + ": " + formatNumber(value, "de", "1.0-2") + " kW";
return label + ": " + formatNumber(value, locale, "1.0-2") + " kW";
};

// Data doesnt have all datapoints for period
Expand Down
4 changes: 3 additions & 1 deletion ui/src/app/edge/history/storage/totalchart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { DefaultTypes } from "src/app/shared/service/defaulttypes";
import { ChartAxis, Utils, YAxisType } from "src/app/shared/service/utils";
import { ChannelAddress, Edge, EdgeConfig, Service } from "src/app/shared/shared";
import { Language } from "src/app/shared/type/language"

Check failure on line 10 in ui/src/app/edge/history/storage/totalchart.component.ts

View workflow job for this annotation

GitHub Actions / build-ui

Missing semicolon

import { AbstractHistoryChart } from "../abstracthistorychart";

Expand Down Expand Up @@ -245,6 +246,7 @@

private applyControllerSpecificChartOptions(options: Chart.ChartOptions) {
const translate = this.translate;
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;

options.scales[ChartAxis.LEFT].min = null;
options.plugins.tooltip.callbacks.label = function (tooltipItem: Chart.TooltipItem<any>) {
Expand All @@ -256,7 +258,7 @@
} else if (value > 0.005) {
label += " " + translate.instant("General.DISCHARGE");
}
return label + ": " + formatNumber(value, "de", "1.0-2") + " kW";
return label + ": " + formatNumber(value, locale, "1.0-2") + " kW";
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,9 @@ export class SchedulePowerAndSocChartComponent extends AbstractHistoryChart impl
private applyControllerSpecificOptions() {
const rightYAxis: HistoryUtils.yAxes = { position: "right", unit: YAxisType.PERCENTAGE, yAxisId: ChartAxis.RIGHT };
const leftYAxis: HistoryUtils.yAxes = { position: "left", unit: YAxisType.POWER, yAxisId: ChartAxis.LEFT };
const locale = this.service.translate.currentLang;

this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, rightYAxis, this.translate, "line", locale, ChartConstants.EMPTY_DATASETS, true);
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, leftYAxis, this.translate, "line", locale, ChartConstants.EMPTY_DATASETS, true);
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, rightYAxis, this.translate, "line", ChartConstants.EMPTY_DATASETS, true);
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, leftYAxis, this.translate, "line", ChartConstants.EMPTY_DATASETS, true);

this.datasets = this.datasets.map((el: Chart.ChartDataset) => {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ export class ScheduleStateAndPriceChartComponent extends AbstractHistoryChart im
}

private applyControllerSpecificOptions() {
const locale = this.service.translate.currentLang;
const rightYaxisSoc: HistoryUtils.yAxes = { position: "right", unit: YAxisType.PERCENTAGE, yAxisId: ChartAxis.RIGHT, displayGrid: true };
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, rightYaxisSoc, this.translate, "line", locale, this.datasets, true);
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, rightYaxisSoc, this.translate, "line", this.datasets, true);

const rightYAxisPower: HistoryUtils.yAxes = { position: "right", unit: YAxisType.POWER, yAxisId: ChartAxis.RIGHT_2 };
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, rightYAxisPower, this.translate, "line", locale, this.datasets, true);
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, rightYAxisPower, this.translate, "line", this.datasets, true);

this.options.scales.x["time"].unit = calculateResolution(this.service, this.service.historyPeriod.value.from, this.service.historyPeriod.value.to).timeFormat;
this.options.scales.x["ticks"] = { source: "auto", autoSkip: false };
Expand Down Expand Up @@ -167,7 +166,7 @@ export class ScheduleStateAndPriceChartComponent extends AbstractHistoryChart im
});
const leftYAxis: HistoryUtils.yAxes = { position: "left", unit: this.unit, yAxisId: ChartAxis.LEFT, customTitle: this.currencyLabel };
[rightYaxisSoc, rightYAxisPower].forEach((element) => {
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, element, this.translate, "line", locale, this.datasets, true);
this.options = NewAbstractHistoryChart.getYAxisOptions(this.options, element, this.translate, "line", this.datasets, true);
});

this.options.scales[ChartAxis.LEFT] = {
Expand Down
4 changes: 3 additions & 1 deletion ui/src/app/edge/live/common/storage/storage.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { AbstractFlatWidget } from "src/app/shared/components/flat/abstract-flat-widget";
import { CurrentData , ChannelAddress, EdgeConfig, Utils } from "src/app/shared/shared";
import { DateUtils } from "src/app/shared/utils/date/dateutils";
import { Language, MyTranslateLoader } from "src/app/shared/type/language";

Check failure on line 7 in ui/src/app/edge/live/common/storage/storage.component.ts

View workflow job for this annotation

GitHub Actions / build-ui

`src/app/shared/type/language` import should occur before import of `src/app/shared/utils/date/dateutils`

Check failure on line 7 in ui/src/app/edge/live/common/storage/storage.component.ts

View workflow job for this annotation

GitHub Actions / build-ui

'MyTranslateLoader' is defined but never used

Check failure on line 7 in ui/src/app/edge/live/common/storage/storage.component.ts

View workflow job for this annotation

GitHub Actions / build-ui

'MyTranslateLoader' is defined but never used

import { StorageModalComponent } from "./modal/modal.component";

Expand Down Expand Up @@ -50,6 +51,7 @@
* @returns only positive and 0
*/
public convertPower(value: number, isCharge?: boolean) {
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;
if (value == null) {
return "-";
}
Expand All @@ -58,7 +60,7 @@

// Round thisValue to Integer when decimal place equals 0
if (thisValue > 0) {
return formatNumber(thisValue, "de", "1.0-1") + " kW"; // TODO get locale dynamically
return formatNumber(thisValue, locale, "1.0-1") + " kW";

} else if (thisValue == 0 && isCharge) {
// if thisValue is 0, then show only when charge and not discharge
Expand Down
4 changes: 3 additions & 1 deletion ui/src/app/edge/settings/powerassistant/powerassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Component } from "@angular/core";
import { AbstractFlatWidget } from "src/app/shared/components/flat/abstract-flat-widget";
import { DataService } from "src/app/shared/components/shared/dataservice";
import { Language } from "src/app/shared/type/language"

Check failure on line 6 in ui/src/app/edge/settings/powerassistant/powerassistant.ts

View workflow job for this annotation

GitHub Actions / build-ui

Missing semicolon
import { ChannelAddress, CurrentData, EdgeConfig, Utils } from "../../../shared/shared";
import { LiveDataService } from "../../live/livedataservice";

Expand Down Expand Up @@ -221,10 +222,11 @@
export namespace Converter {
export function unit(unit: string): (value: any) => string {
return function (value: any): string {
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;
if (value == null) {
return "-";
} else if (value >= 0) {
return formatNumber(value, "de", "1.0-0") + " " + unit;
return formatNumber(value, locale, "1.0-0") + " " + unit;
}
};
}
Expand Down
30 changes: 16 additions & 14 deletions ui/src/app/shared/components/chart/abstracthistorychart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
const datasets: Chart.ChartDataset[] = [];
const displayValues: HistoryUtils.DisplayValue<HistoryUtils.CustomOptions>[] = chartObject.output(channelData.data, labels);
const legendOptions: { label: string, strokeThroughHidingStyle: boolean, hideLabelInLegend: boolean; }[] = [];
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;

Check failure on line 133 in ui/src/app/shared/components/chart/abstracthistorychart.ts

View workflow job for this annotation

GitHub Actions / build-ui

'locale' is assigned a value but never used
displayValues.forEach((displayValue, index) => {
let nameSuffix = null;

Expand Down Expand Up @@ -352,19 +353,17 @@
translate: TranslateService,
legendOptions: { label: string, strokeThroughHidingStyle: boolean; }[],
channelData: { data: { [name: string]: number[]; }; },
locale: string,
config: EdgeConfig,
datasets: Chart.ChartDataset[],
chartOptionsType: XAxisType,
labels: (Date | string)[],
): Chart.ChartOptions {

let tooltipsLabel: string | null = null;
let options: Chart.ChartOptions = Utils.deepCopy(<Chart.ChartOptions>Utils.deepCopy(AbstractHistoryChart.getDefaultOptions(chartOptionsType, service, labels)));
const displayValues: HistoryUtils.DisplayValue<HistoryUtils.CustomOptions>[] = chartObject.output(channelData.data);

chartObject.yAxes.forEach((element) => {
options = AbstractHistoryChart.getYAxisOptions(options, element, translate, chartType, locale, datasets, true);
options = AbstractHistoryChart.getYAxisOptions(options, element, translate, chartType, datasets, true);
});

options.plugins.tooltip.callbacks.title = (tooltipItems: Chart.TooltipItem<any>[]): string => {
Expand Down Expand Up @@ -395,7 +394,7 @@
tooltipsLabel = AbstractHistoryChart.getToolTipsAfterTitleLabel(unit, chartType, value, translate);
}

return label.split(":")[0] + ": " + AbstractHistoryChart.getToolTipsSuffix(tooltipsLabel, value, displayValue.custom?.formatNumber ?? chartObject.tooltip.formatNumber, unit, chartType, locale, translate, config);
return label.split(":")[0] + ": " + AbstractHistoryChart.getToolTipsSuffix(tooltipsLabel, value, displayValue.custom?.formatNumber ?? chartObject.tooltip.formatNumber, unit, chartType, translate, config);
};

options.plugins.tooltip.callbacks.labelColor = (item: Chart.TooltipItem<any>) => {
Expand Down Expand Up @@ -436,6 +435,7 @@
};

options.plugins.tooltip.callbacks.afterTitle = function (items: Chart.TooltipItem<any>[]) {
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;

if (items?.length === 0) {
return null;
Expand All @@ -459,7 +459,7 @@

const totalValue = datasets.filter(el => el.stack == stack).reduce((_total, dataset) => Utils.addSafely(_total, Math.abs(dataset.data[datasetIndex])), 0);
if (afterTitle) {
return afterTitle + ": " + formatNumber(totalValue, "de", chartObject.tooltip.formatNumber) + " " + tooltipsLabel;
return afterTitle + ": " + formatNumber(totalValue, locale, chartObject.tooltip.formatNumber) + " " + tooltipsLabel;
}

return null;
Expand All @@ -474,7 +474,7 @@
function rebuildScales(chart: Chart.Chart) {
let options = chart.options;
chartObject.yAxes.forEach((element) => {
options = AbstractHistoryChart.getYAxisOptions(options, element, translate, chartType, locale, _dataSets, true);
options = AbstractHistoryChart.getYAxisOptions(options, element, translate, chartType, _dataSets, true);
});
}

Expand Down Expand Up @@ -524,8 +524,8 @@
* @param locale the current locale
* @returns the chart options {@link Chart.ChartOptions}
*/
public static getYAxisOptions(options: Chart.ChartOptions, element: HistoryUtils.yAxes, translate: TranslateService, chartType: "line" | "bar", locale: string, datasets: Chart.ChartDataset[], showYAxisType?: boolean): Chart.ChartOptions {

public static getYAxisOptions(options: Chart.ChartOptions, element: HistoryUtils.yAxes, translate: TranslateService, chartType: "line" | "bar", datasets: Chart.ChartDataset[], showYAxisType?: boolean): Chart.ChartOptions {
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;
const baseConfig = ChartConstants.DEFAULT_Y_SCALE_OPTIONS(element, translate, chartType, datasets, showYAxisType);
switch (element.unit) {
case YAxisType.RELAY:
Expand Down Expand Up @@ -623,15 +623,16 @@
* @returns a string, that is either the baseName, if no suffix is provided, or a baseName with a formatted number
*/
public static getTooltipsLabelName(baseName: string, unit: YAxisType, suffix?: number | string): string {
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;
if (suffix != null) {
if (typeof suffix === "string") {
return baseName + " " + suffix;
} else {
switch (unit) {
case YAxisType.ENERGY:
return baseName + ": " + formatNumber(suffix / 1000, "de", "1.0-1") + " kWh";
return baseName + ": " + formatNumber(suffix / 1000, locale, "1.0-1") + " kWh";
case YAxisType.PERCENTAGE:
return baseName + ": " + formatNumber(suffix, "de", "1.0-1") + " %";
return baseName + ": " + formatNumber(suffix, locale, "1.0-1") + " %";
case YAxisType.RELAY:
case YAxisType.TIME: {
const pipe = new FormatSecondsToDurationPipe(new DecimalPipe(Language.DE.key));
Expand All @@ -651,7 +652,9 @@
* @param title the YAxisType
* @returns the tooltips suffix
*/
public static getToolTipsSuffix(label: any, value: number, format: string, title: YAxisType, chartType: "bar" | "line", language: string, translate: TranslateService, config: EdgeConfig): string {
public static getToolTipsSuffix(label: any, value: number, format: string, title: YAxisType, chartType: "bar" | "line", translate: TranslateService, config: EdgeConfig): string {
const language: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).key;
const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;
let tooltipsLabel: string | null = null;
switch (title) {
case YAxisType.RELAY: {
Expand Down Expand Up @@ -693,7 +696,7 @@
break;
}

return formatNumber(value, "de", format) + " " + tooltipsLabel;
return formatNumber(value, locale, format) + " " + tooltipsLabel;
}

public static getDefaultOptions(xAxisType: XAxisType, service: Service, labels: (Date | string)[]): Chart.ChartOptions {
Expand Down Expand Up @@ -1073,8 +1076,7 @@
* Sets the Labels of the Chart
*/
protected setChartLabel() {
const locale = this.service.translate.currentLang;
this.options = AbstractHistoryChart.getOptions(this.chartObject, this.chartType, this.service, this.translate, this.legendOptions, this.channelData, locale, this.config, this.datasets, this.xAxisScalingType, this.labels);
this.options = AbstractHistoryChart.getOptions(this.chartObject, this.chartType, this.service, this.translate, this.legendOptions, this.channelData, this.config, this.datasets, this.xAxisScalingType, this.labels);
this.loading = false;
this.stopSpinner();
}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/shared/components/flat/abstract-flat-widget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-strict-ignore
import { Directive, Inject, Input, OnDestroy, OnInit } from "@angular/core";
import { FormBuilder, FormGroup } from "@angular/forms";
import { Directive, Input, OnDestroy, OnInit, Inject } from "@angular/core";

Check failure on line 3 in ui/src/app/shared/components/flat/abstract-flat-widget.ts

View workflow job for this annotation

GitHub Actions / build-ui

`@angular/core` import should occur before import of `@angular/forms`
import { ActivatedRoute, Router } from "@angular/router";
import { ModalController } from "@ionic/angular";
import { TranslateService } from "@ngx-translate/core";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { formatNumber } from "@angular/common";
import { Component, Input } from "@angular/core";
import { ChannelAddress, CurrentData, Utils } from "src/app/shared/shared";
import { Language } from "src/app/shared/type/language"

Check failure on line 4 in ui/src/app/shared/components/modal/modal-phases/modal-phases.ts

View workflow job for this annotation

GitHub Actions / build-ui

Missing semicolon

import { AbstractModalLine } from "../abstract-modal-line";
import { TextIndentation } from "../modal-line/modal-line";
Expand Down Expand Up @@ -46,8 +47,8 @@
* @returns converted value
*/
protected CONVERT_TO_POSITIVE_WATT = (value: number | null): string => {

const locale: string = (Language.getByKey(localStorage.LANGUAGE) ?? Language.DEFAULT).i18nLocaleKey;
value = Utils.absSafely(value) ?? 0;
return formatNumber(value, "de", "1.0-0") + " W";
return formatNumber(value, locale, "1.0-0") + " W";
};
}
2 changes: 1 addition & 1 deletion ui/src/app/shared/components/shared/testing/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class OeChartTester {
legendOptions.push(AbstractHistoryChart.getLegendOptions(label, displayValue));
});

const options = AbstractHistoryChart.getOptions(chartData, chartType, testContext.service, testContext.translate, legendOptions, channelData.result, locale, config, datasets, xAxisType, labels);
const options = AbstractHistoryChart.getOptions(chartData, chartType, testContext.service, testContext.translate, legendOptions, channelData.result, config, datasets, xAxisType, labels);

chartData.yAxes.filter(axis => axis.unit != null).forEach(axis => {
// Remove custom scale calculations from unittest, seperate unittest existing
Expand Down
Loading