From 1e942142daa0d1b8df97842931a4d2649645a4dc Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Fri, 27 May 2022 15:03:28 +0200 Subject: [PATCH] Add vertical hover line --- .../admin-market-data-detail.component.html | 2 + .../components/home-market/home-market.html | 2 + .../home-overview/home-overview.html | 1 + .../position-detail-dialog.html | 1 + .../lib/line-chart/line-chart.component.ts | 79 ++++++++++++++++++- 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html b/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html index 7264be84d..c3d905be0 100644 --- a/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html +++ b/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html @@ -2,8 +2,10 @@
{{ itemByMonth.key }}
diff --git a/apps/client/src/app/components/home-market/home-market.html b/apps/client/src/app/components/home-market/home-market.html index b55103bf0..d509a641d 100644 --- a/apps/client/src/app/components/home-market/home-market.html +++ b/apps/client/src/app/components/home-market/home-market.html @@ -7,11 +7,13 @@
diff --git a/apps/client/src/app/components/home-overview/home-overview.html b/apps/client/src/app/components/home-overview/home-overview.html index 7f804d990..e82ef7f5f 100644 --- a/apps/client/src/app/components/home-overview/home-overview.html +++ b/apps/client/src/app/components/home-overview/home-overview.html @@ -6,6 +6,7 @@ { + if (position === false) { + return false; + } + return { + x: position.x, + y: this.chart.chartArea.top + }; + }; } public ngAfterViewInit() { @@ -148,20 +165,43 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy { data, options: { animation: false, - plugins: { + elements: { + point: { + hoverBackgroundColor: getBackgroundColor(), + hoverRadius: 2 + } + }, + interaction: { intersect: false, mode: 'index' }, + plugins: { legend: { align: 'start', display: this.showLegend, position: 'bottom' + }, + tooltip: { + itemSort: (a, b) => { + // Reverse order + return b.datasetIndex - a.datasetIndex; + }, + mode: 'index', + position: 'top', + xAlign: 'center', + yAlign: 'bottom' + }, + verticalHoverLine: { + color: `rgba(${getTextColor()}, 0.1)` } }, scales: { x: { display: this.showXAxis, grid: { + borderColor: `rgba(${getTextColor()}, 0.2)`, + color: `rgba(${getTextColor()}, 0.8)`, display: false }, time: { + tooltipFormat: getDateFormatString(this.locale), unit: 'year' }, type: 'time' @@ -169,6 +209,8 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy { y: { display: this.showYAxis, grid: { + borderColor: `rgba(${getTextColor()}, 0.2)`, + color: `rgba(${getTextColor()}, 0.8)`, display: false }, max: this.yMax, @@ -204,6 +246,35 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy { }, spanGaps: true }, + plugins: [ + { + afterDatasetsDraw: (chart, x, options) => { + const active = chart.getActiveElements(); + + if (!active || active.length === 0) { + return; + } + + const color = options.color || `rgb(${getTextColor()})`; + const width = options.width || 1; + + const { + chartArea: { bottom, top } + } = chart; + const xValue = active[0].element.x; + + const context = this.chartCanvas.nativeElement.getContext('2d'); + context.lineWidth = width; + context.strokeStyle = color; + + context.beginPath(); + context.moveTo(xValue, top); + context.lineTo(xValue, bottom); + context.stroke(); + }, + id: 'verticalHoverLine' + } + ], type: 'line' }); }