feat(chart-area): add series statistics display and computation for chart data
This commit is contained in:
@@ -33,6 +33,9 @@ export class DeesChartArea extends DeesElement {
|
||||
@state()
|
||||
accessor chart: IChartApi | null = null;
|
||||
|
||||
@state()
|
||||
accessor seriesStats: Array<{ name: string; latest: number; min: number; max: number; avg: number; color: string }> = [];
|
||||
|
||||
@property()
|
||||
accessor label: string = 'Untitled Chart';
|
||||
|
||||
@@ -172,7 +175,7 @@ export class DeesChartArea extends DeesElement {
|
||||
for (const [, api] of this.seriesApis) {
|
||||
const color = colors[idx % colors.length];
|
||||
api.applyOptions({
|
||||
topColor: this.hslToRgba(color, isDark ? 0.2 : 0.3),
|
||||
topColor: this.hslToRgba(color, isDark ? 0.4 : 0.5),
|
||||
bottomColor: this.hslToRgba(color, 0),
|
||||
lineColor: color,
|
||||
});
|
||||
@@ -194,7 +197,7 @@ export class DeesChartArea extends DeesElement {
|
||||
chartSeries.forEach((s, index) => {
|
||||
const color = colors[index % colors.length];
|
||||
const api = this.chart!.addSeries(this.lcBundle!.AreaSeries, {
|
||||
topColor: this.hslToRgba(color, isDark ? 0.2 : 0.3),
|
||||
topColor: this.hslToRgba(color, isDark ? 0.4 : 0.5),
|
||||
bottomColor: this.hslToRgba(color, 0),
|
||||
lineColor: color,
|
||||
lineWidth: 2,
|
||||
@@ -218,6 +221,23 @@ export class DeesChartArea extends DeesElement {
|
||||
|
||||
this.seriesApis.set(s.name || `series-${index}`, api);
|
||||
});
|
||||
this.computeStats(chartSeries);
|
||||
}
|
||||
|
||||
private computeStats(chartSeries: ChartSeriesConfig) {
|
||||
const isDark = !this.goBright;
|
||||
const colors = this.getSeriesColors(isDark);
|
||||
this.seriesStats = chartSeries.map((s, index) => {
|
||||
const values = s.data.map(d => d.y);
|
||||
if (values.length === 0) {
|
||||
return { name: s.name || `series-${index}`, latest: 0, min: 0, max: 0, avg: 0, color: colors[index % colors.length] };
|
||||
}
|
||||
const latest = values[values.length - 1];
|
||||
const min = Math.min(...values);
|
||||
const max = Math.max(...values);
|
||||
const avg = Math.round((values.reduce((sum, v) => sum + v, 0) / values.length) * 100) / 100;
|
||||
return { name: s.name || `series-${index}`, latest, min, max, avg, color: colors[index % colors.length] };
|
||||
});
|
||||
}
|
||||
|
||||
private updatePriceLines(name: string, api: ISeriesApi<any>, data: Array<{ x: any; y: number }>, color: string) {
|
||||
@@ -468,6 +488,7 @@ export class DeesChartArea extends DeesElement {
|
||||
api.setData(this.convertDataToLC(filtered));
|
||||
this.updatePriceLines(name, api, filtered, colors[index % colors.length]);
|
||||
});
|
||||
this.computeStats(newSeries);
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -505,6 +526,7 @@ export class DeesChartArea extends DeesElement {
|
||||
api.setData(this.convertDataToLC(s.data));
|
||||
this.updatePriceLines(name, api, s.data, colors[index % colors.length]);
|
||||
});
|
||||
this.computeStats(newSeries);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user