Chandranil Bakshi 1 day ago
committed by GitHub
parent
commit
589a1f863b
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      apps/client/src/app/components/home-overview/home-overview.html
  2. 2
      libs/ui/src/lib/line-chart/line-chart.component.stories.ts
  3. 50
      libs/ui/src/lib/line-chart/line-chart.component.ts

2
apps/client/src/app/components/home-overview/home-overview.html

@ -69,6 +69,8 @@
unit="%" unit="%"
[class.pr-3]="deviceType() === 'mobile'" [class.pr-3]="deviceType() === 'mobile'"
[colorScheme]="user()?.settings?.colorScheme" [colorScheme]="user()?.settings?.colorScheme"
[dataDecimation]="true"
[dataDecimationThreshold]="100"
[hidden]="historicalDataItems()?.length === 0" [hidden]="historicalDataItems()?.length === 0"
[historicalDataItems]="historicalDataItems()" [historicalDataItems]="historicalDataItems()"
[isAnimated]="user()?.settings?.dateRange === '1d' ? false : true" [isAnimated]="user()?.settings?.dateRange === '1d' ? false : true"

2
libs/ui/src/lib/line-chart/line-chart.component.stories.ts

@ -19,6 +19,8 @@ type Story = StoryObj<GfLineChartComponent>;
export const Simple: Story = { export const Simple: Story = {
args: { args: {
dataDecimation: true,
dataDecimationThreshold: 1,
historicalDataItems: [ historicalDataItems: [
{ {
date: '2017-01-01', date: '2017-01-01',

50
libs/ui/src/lib/line-chart/line-chart.component.ts

@ -26,11 +26,14 @@ import {
import { import {
type AnimationsSpec, type AnimationsSpec,
Chart, Chart,
type ChartData,
Decimation,
Filler, Filler,
LinearScale, LinearScale,
LineController, LineController,
LineElement, LineElement,
PointElement, PointElement,
type Point,
TimeScale, TimeScale,
Tooltip, Tooltip,
type TooltipOptions type TooltipOptions
@ -54,6 +57,8 @@ export class GfLineChartComponent
@Input() benchmarkLabel = ''; @Input() benchmarkLabel = '';
@Input() colorScheme: ColorScheme; @Input() colorScheme: ColorScheme;
@Input() currency: string; @Input() currency: string;
@Input() dataDecimation = false;
@Input() dataDecimationThreshold: number;
@Input() historicalDataItems: LineChartItem[]; @Input() historicalDataItems: LineChartItem[];
@Input() isAnimated = false; @Input() isAnimated = false;
@Input() label: string; @Input() label: string;
@ -71,13 +76,14 @@ export class GfLineChartComponent
@ViewChild('chartCanvas') chartCanvas: ElementRef<HTMLCanvasElement>; @ViewChild('chartCanvas') chartCanvas: ElementRef<HTMLCanvasElement>;
public chart: Chart<'line'>; public chart: Chart<'line', (number | Point | null)[], string>;
public isLoading = true; public isLoading = true;
private readonly ANIMATION_DURATION = 1200; private readonly ANIMATION_DURATION = 1200;
public constructor(private changeDetectorRef: ChangeDetectorRef) { public constructor(private changeDetectorRef: ChangeDetectorRef) {
Chart.register( Chart.register(
Decimation,
Filler, Filler,
LineController, LineController,
LineElement, LineElement,
@ -118,14 +124,27 @@ export class GfLineChartComponent
private initialize() { private initialize() {
this.isLoading = true; this.isLoading = true;
const benchmarkPrices: number[] = []; const benchmarkPrices: (number | Point | null)[] = [];
const labels: string[] = []; const labels: string[] = [];
const marketPrices: number[] = []; const marketPrices: (number | Point | null)[] = [];
this.historicalDataItems?.forEach((historicalDataItem, index) => { this.historicalDataItems?.forEach((historicalDataItem, index) => {
benchmarkPrices.push(this.benchmarkDataItems?.[index]?.value); const label = historicalDataItem.date;
labels.push(historicalDataItem.date); const timestamp = new Date(historicalDataItem.date).getTime();
marketPrices.push(historicalDataItem.value); const benchmarkValue = this.benchmarkDataItems?.[index]?.value;
const marketValue = historicalDataItem.value;
benchmarkPrices.push(
this.dataDecimation
? { x: timestamp, y: benchmarkValue ?? null }
: benchmarkValue
);
labels.push(label);
marketPrices.push(
this.dataDecimation
? { x: timestamp, y: marketValue ?? null }
: marketValue
);
}); });
const gradient = this.chartCanvas?.nativeElement const gradient = this.chartCanvas?.nativeElement
@ -148,7 +167,7 @@ export class GfLineChartComponent
gradient.addColorStop(1, getBackgroundColor(this.colorScheme)); gradient.addColorStop(1, getBackgroundColor(this.colorScheme));
} }
const data = { const data: ChartData<'line', (number | Point | null)[], string> = {
labels, labels,
datasets: [ datasets: [
{ {
@ -181,6 +200,11 @@ export class GfLineChartComponent
if (this.chart) { if (this.chart) {
this.chart.data = data; this.chart.data = data;
this.chart.options.plugins ??= {}; this.chart.options.plugins ??= {};
this.chart.options.parsing = this.dataDecimation ? false : undefined;
this.chart.options.plugins.decimation = {
enabled: this.dataDecimation,
threshold: this.dataDecimationThreshold
};
this.chart.options.plugins.tooltip = this.chart.options.plugins.tooltip =
this.getTooltipPluginConfiguration(); this.getTooltipPluginConfiguration();
this.chart.options.animations = this.isAnimated this.chart.options.animations = this.isAnimated
@ -189,7 +213,9 @@ export class GfLineChartComponent
this.chart.update(); this.chart.update();
} else { } else {
this.chart = new Chart(this.chartCanvas.nativeElement, { this.chart = new Chart<'line', (number | Point | null)[], string>(
this.chartCanvas.nativeElement,
{
data, data,
options: { options: {
animations: this.isAnimated ? animations : undefined, animations: this.isAnimated ? animations : undefined,
@ -201,7 +227,12 @@ export class GfLineChartComponent
} }
}, },
interaction: { intersect: false, mode: 'index' }, interaction: { intersect: false, mode: 'index' },
parsing: this.dataDecimation ? false : undefined,
plugins: { plugins: {
decimation: {
enabled: this.dataDecimation,
threshold: this.dataDecimationThreshold
},
legend: { legend: {
align: 'start', align: 'start',
display: this.showLegend, display: this.showLegend,
@ -288,7 +319,8 @@ export class GfLineChartComponent
getVerticalHoverLinePlugin(this.chartCanvas, this.colorScheme) getVerticalHoverLinePlugin(this.chartCanvas, this.colorScheme)
], ],
type: 'line' type: 'line'
}); }
);
} }
} }

Loading…
Cancel
Save