Browse Source

Add vertical hover line

pull/963/head
Thomas 3 years ago
parent
commit
1e942142da
  1. 2
      apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html
  2. 2
      apps/client/src/app/components/home-market/home-market.html
  3. 1
      apps/client/src/app/components/home-overview/home-overview.html
  4. 1
      apps/client/src/app/components/position/position-detail-dialog/position-detail-dialog.html
  5. 79
      libs/ui/src/lib/line-chart/line-chart.component.ts

2
apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html

@ -2,8 +2,10 @@
<gf-line-chart <gf-line-chart
class="mb-4" class="mb-4"
[historicalDataItems]="historicalDataItems" [historicalDataItems]="historicalDataItems"
[locale]="locale"
[showXAxis]="true" [showXAxis]="true"
[showYAxis]="true" [showYAxis]="true"
[symbol]="symbol"
></gf-line-chart> ></gf-line-chart>
<div *ngFor="let itemByMonth of marketDataByMonth | keyvalue" class="d-flex"> <div *ngFor="let itemByMonth of marketDataByMonth | keyvalue" class="d-flex">
<div class="date px-1 text-nowrap">{{ itemByMonth.key }}</div> <div class="date px-1 text-nowrap">{{ itemByMonth.key }}</div>

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

@ -7,11 +7,13 @@
</div> </div>
<gf-line-chart <gf-line-chart
class="mb-3" class="mb-3"
symbol="Fear & Greed Index"
yMax="100" yMax="100"
yMaxLabel="Greed" yMaxLabel="Greed"
yMin="0" yMin="0"
yMinLabel="Fear" yMinLabel="Fear"
[historicalDataItems]="historicalData" [historicalDataItems]="historicalData"
[locale]="user?.settings?.locale"
[showXAxis]="true" [showXAxis]="true"
[showYAxis]="true" [showYAxis]="true"
></gf-line-chart> ></gf-line-chart>

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

@ -6,6 +6,7 @@
<gf-line-chart <gf-line-chart
symbol="Performance" symbol="Performance"
[historicalDataItems]="historicalDataItems" [historicalDataItems]="historicalDataItems"
[locale]="user?.settings?.locale"
[ngClass]="{ 'pr-3': deviceType === 'mobile' }" [ngClass]="{ 'pr-3': deviceType === 'mobile' }"
[showGradient]="true" [showGradient]="true"
[showLoader]="false" [showLoader]="false"

1
apps/client/src/app/components/position/position-detail-dialog/position-detail-dialog.html

@ -24,6 +24,7 @@
benchmarkLabel="Average Unit Price" benchmarkLabel="Average Unit Price"
[benchmarkDataItems]="benchmarkDataItems" [benchmarkDataItems]="benchmarkDataItems"
[historicalDataItems]="historicalDataItems" [historicalDataItems]="historicalDataItems"
[locale]="data.locale"
[showGradient]="true" [showGradient]="true"
[showXAxis]="true" [showXAxis]="true"
[showYAxis]="true" [showYAxis]="true"

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

@ -11,7 +11,11 @@ import {
ViewChild ViewChild
} from '@angular/core'; } from '@angular/core';
import { primaryColorRgb, secondaryColorRgb } from '@ghostfolio/common/config'; import { primaryColorRgb, secondaryColorRgb } from '@ghostfolio/common/config';
import { getBackgroundColor } from '@ghostfolio/common/helper'; import {
getBackgroundColor,
getDateFormatString,
getTextColor
} from '@ghostfolio/common/helper';
import { import {
Chart, Chart,
Filler, Filler,
@ -19,7 +23,8 @@ import {
LineElement, LineElement,
LinearScale, LinearScale,
PointElement, PointElement,
TimeScale TimeScale,
Tooltip
} from 'chart.js'; } from 'chart.js';
import { LineChartItem } from './interfaces/line-chart.interface'; import { LineChartItem } from './interfaces/line-chart.interface';
@ -34,6 +39,7 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy {
@Input() benchmarkDataItems: LineChartItem[] = []; @Input() benchmarkDataItems: LineChartItem[] = [];
@Input() benchmarkLabel = ''; @Input() benchmarkLabel = '';
@Input() historicalDataItems: LineChartItem[]; @Input() historicalDataItems: LineChartItem[];
@Input() locale: string;
@Input() showGradient = false; @Input() showGradient = false;
@Input() showLegend = false; @Input() showLegend = false;
@Input() showLoader = true; @Input() showLoader = true;
@ -57,8 +63,19 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy {
LineElement, LineElement,
PointElement, PointElement,
LinearScale, LinearScale,
TimeScale TimeScale,
Tooltip
); );
Tooltip.positioners['top'] = (elements, position) => {
if (position === false) {
return false;
}
return {
x: position.x,
y: this.chart.chartArea.top
};
};
} }
public ngAfterViewInit() { public ngAfterViewInit() {
@ -148,20 +165,43 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy {
data, data,
options: { options: {
animation: false, animation: false,
plugins: { elements: {
point: {
hoverBackgroundColor: getBackgroundColor(),
hoverRadius: 2
}
},
interaction: { intersect: false, mode: 'index' },
plugins: <any>{
legend: { legend: {
align: 'start', align: 'start',
display: this.showLegend, display: this.showLegend,
position: 'bottom' position: 'bottom'
},
tooltip: {
itemSort: (a, b) => {
// Reverse order
return b.datasetIndex - a.datasetIndex;
},
mode: 'index',
position: <any>'top',
xAlign: 'center',
yAlign: 'bottom'
},
verticalHoverLine: {
color: `rgba(${getTextColor()}, 0.1)`
} }
}, },
scales: { scales: {
x: { x: {
display: this.showXAxis, display: this.showXAxis,
grid: { grid: {
borderColor: `rgba(${getTextColor()}, 0.2)`,
color: `rgba(${getTextColor()}, 0.8)`,
display: false display: false
}, },
time: { time: {
tooltipFormat: getDateFormatString(this.locale),
unit: 'year' unit: 'year'
}, },
type: 'time' type: 'time'
@ -169,6 +209,8 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy {
y: { y: {
display: this.showYAxis, display: this.showYAxis,
grid: { grid: {
borderColor: `rgba(${getTextColor()}, 0.2)`,
color: `rgba(${getTextColor()}, 0.8)`,
display: false display: false
}, },
max: this.yMax, max: this.yMax,
@ -204,6 +246,35 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy {
}, },
spanGaps: true 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' type: 'line'
}); });
} }

Loading…
Cancel
Save