-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSparkLine.tsx
More file actions
89 lines (75 loc) · 2.46 KB
/
SparkLine.tsx
File metadata and controls
89 lines (75 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { computed } from 'mobx';
import { observer } from 'mobx-react';
import { ObservedComponent } from 'mobx-react-helper';
import { i18n, I18nContext } from '../../models/Translation';
import { IndexHistoryPoint } from '../../types/finance';
import styles from './SparkLine.module.less';
export interface SparkLineProps {
points: IndexHistoryPoint[];
chartId: string;
}
@observer
export class SparkLine extends ObservedComponent<SparkLineProps, typeof i18n> {
static contextType = I18nContext;
@computed
get chartData() {
const { points } = this.observedProps;
if (!points.length) return { polyline: '', gradientStops: [] as number[] };
const values = points.map(({ value }) => value);
const min = Math.min(...values);
const max = Math.max(...values);
const delta = max - min || 1;
const polylinePoints = points
.map(({ value }, index, { length }) => {
const x = (index / Math.max(length - 1, 1)) * 100;
const y = ((max - value) / delta) * 40;
return `${x.toFixed(2)},${y.toFixed(2)}`;
})
.join(' ');
const gradientOffsets = [0, 50, 100];
return { polyline: polylinePoints, gradientStops: gradientOffsets };
}
renderContent() {
const { t } = this.observedContext,
{ chartId } = this.observedProps,
{ polyline, gradientStops } = this.chartData;
const gradientId = `${chartId}-gradient`;
return (
<div className={styles.sparkline}>
<svg viewBox="0 0 100 40" role="img" aria-label={t('index_sparkline_60d_label')}>
<defs>
<linearGradient id={gradientId} x1="0%" y1="0%" x2="100%" y2="0%">
{gradientStops.map(offset => (
<stop
key={offset}
offset={`${offset}%`}
stopColor="var(--bs-primary)"
stopOpacity="0.5"
/>
))}
</linearGradient>
</defs>
<polyline
fill="none"
stroke={`url(#${gradientId})`}
strokeWidth="2"
strokeLinejoin="round"
strokeLinecap="round"
points={polyline}
/>
</svg>
</div>
);
}
render() {
const { t } = this.observedContext,
{ points } = this.props;
return points.length ? (
this.renderContent()
) : (
<div className={styles.sparkline}>
<div className={styles.placeholder}>{t('data_preparing')}</div>
</div>
);
}
}