Browse Source

fix(client): implement custom plugin with type safety

pull/6264/head
KenTandrian 1 week ago
parent
commit
069f3fe71c
  1. 1
      apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts
  2. 1
      apps/client/src/app/components/investment-chart/investment-chart.component.ts
  3. 18
      libs/common/src/lib/chart-helper.ts
  4. 10
      libs/ui/src/lib/chart/chart.registry.ts
  5. 1
      libs/ui/src/lib/line-chart/line-chart.component.ts

1
apps/client/src/app/components/benchmark-comparator/benchmark-comparator.component.ts

@ -190,7 +190,6 @@ export class GfBenchmarkComparatorComponent implements OnChanges, OnDestroy {
display: false display: false
}, },
tooltip: this.getTooltipPluginConfiguration(), tooltip: this.getTooltipPluginConfiguration(),
// @ts-ignore
verticalHoverLine: { verticalHoverLine: {
color: `rgba(${getTextColor(this.colorScheme)}, 0.1)` color: `rgba(${getTextColor(this.colorScheme)}, 0.1)`
} }

1
apps/client/src/app/components/investment-chart/investment-chart.component.ts

@ -223,7 +223,6 @@ export class GfInvestmentChartComponent implements OnChanges, OnDestroy {
display: false display: false
}, },
tooltip: this.getTooltipPluginConfiguration(), tooltip: this.getTooltipPluginConfiguration(),
// @ts-ignore
verticalHoverLine: { verticalHoverLine: {
color: `rgba(${getTextColor(this.colorScheme)}, 0.1)` color: `rgba(${getTextColor(this.colorScheme)}, 0.1)`
} }

18
libs/common/src/lib/chart-helper.ts

@ -106,9 +106,9 @@ export function getTooltipPositionerMapTop(
} }
export function getVerticalHoverLinePlugin<T extends 'line' | 'bar'>( export function getVerticalHoverLinePlugin<T extends 'line' | 'bar'>(
chartCanvas: ElementRef, chartCanvas: ElementRef<HTMLCanvasElement>,
colorScheme: ColorScheme colorScheme: ColorScheme
): Plugin<T> { ): Plugin<T, { color: string; width: number }> {
return { return {
afterDatasetsDraw: (chart, _, options) => { afterDatasetsDraw: (chart, _, options) => {
const active = chart.getActiveElements(); const active = chart.getActiveElements();
@ -126,13 +126,15 @@ export function getVerticalHoverLinePlugin<T extends 'line' | 'bar'>(
const xValue = active[0].element.x; const xValue = active[0].element.x;
const context = chartCanvas.nativeElement.getContext('2d'); const context = chartCanvas.nativeElement.getContext('2d');
context.lineWidth = width; if (context) {
context.strokeStyle = color; context.lineWidth = width;
context.strokeStyle = color;
context.beginPath(); context.beginPath();
context.moveTo(xValue, top); context.moveTo(xValue, top);
context.lineTo(xValue, bottom); context.lineTo(xValue, bottom);
context.stroke(); context.stroke();
}
}, },
id: 'verticalHoverLine' id: 'verticalHoverLine'
}; };

10
libs/ui/src/lib/chart/chart.registry.ts

@ -2,7 +2,17 @@ import { getTooltipPositionerMapTop } from '@ghostfolio/common/chart-helper';
import { Tooltip, TooltipPositionerFunction, ChartType } from 'chart.js'; import { Tooltip, TooltipPositionerFunction, ChartType } from 'chart.js';
interface VerticalHoverLinePluginOptions {
color?: string;
width?: number;
}
declare module 'chart.js' { declare module 'chart.js' {
interface PluginOptionsByType<TType extends ChartType> {
verticalHoverLine: TType extends 'line' | 'bar'
? VerticalHoverLinePluginOptions
: never;
}
interface TooltipPositionerMap { interface TooltipPositionerMap {
top: TooltipPositionerFunction<ChartType>; top: TooltipPositionerFunction<ChartType>;
} }

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

@ -207,7 +207,6 @@ export class GfLineChartComponent
position: 'bottom' position: 'bottom'
}, },
tooltip: this.getTooltipPluginConfiguration(), tooltip: this.getTooltipPluginConfiguration(),
// @ts-ignore
verticalHoverLine: { verticalHoverLine: {
color: `rgba(${getTextColor(this.colorScheme)}, 0.1)` color: `rgba(${getTextColor(this.colorScheme)}, 0.1)`
} }

Loading…
Cancel
Save