summaryrefslogtreecommitdiff
path: root/src/components/Sparkline.tsx
blob: 79f8bf5792fde7fa3dc38a46c5cfc04b19924fdf (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as React from 'react';
import { Line } from 'react-chartjs-2';

import { chartJSResource, chartStyles, commonDataSetProps } from '../misc/chart';
import prettyBytes from '../misc/pretty-bytes';

import s from './Sparkline.module.scss';

const { useMemo } = React;

const extraChartOptions: any = {
  responsive: true,
  maintainAspectRatio: false,
  parsing: false,
  animation: {
    duration: 1000,
    easing: 'linear',
  },
  animations: {
    y: {
      duration: 0,
    },
    x: {
      duration: 0,
    },
  },
  transitions: {
    active: {
      animation: {
        duration: 0,
      },
    },
  },
  plugins: {
    legend: { display: false },
    tooltip: {
      enabled: true,
      intersect: false,
      mode: 'index',
    },
  },
  scales: {
    x: {
      type: 'time',
      display: false,
    },
    y: {
      display: false,
      beginAtZero: true,
    },
  },
  elements: {
    line: {
      borderWidth: 1,
      tension: 0.4,
    },
    point: {
      radius: 0,
    },
  },
};

export default function Sparkline({ data: dataArray, labels, type, styleIndex = 0 }) {
  chartJSResource.read();

  const isMemory = type === 'inuse';

  const options = useMemo(() => {
    return {
      ...extraChartOptions,
      scales: {
        ...extraChartOptions.scales,
        y: {
          display: false,
          // 内存值稳定,不从零开始,让 Y 轴自动适应数据范围以显示波动
          beginAtZero: !isMemory,
        },
      },
      plugins: {
        ...extraChartOptions.plugins,
        tooltip: {
          ...extraChartOptions.plugins.tooltip,
          displayColors: false,
          callbacks: {
            title: () => '',
            label(context) {
              if (context.parsed.y !== null) {
                const suffix = isMemory ? '' : '/s';
                const raw = isMemory ? context.parsed.y : Math.expm1(context.parsed.y);
                return prettyBytes(raw) + suffix;
              }
              return '';
            },
          },
        },
      },
    };
  }, [type, isMemory]);

  const data = useMemo(
    () => ({
      datasets: [
        {
          ...commonDataSetProps,
          ...chartStyles[styleIndex][type],
          // 内存用原始值(变化幅度小,不需要压缩);流量用 log1p 压缩尖刺
          data: dataArray.map((v, i) => ({ x: labels[i], y: isMemory ? v : Math.log1p(v) })),
          fill: true,
        },
      ],
    }),
    [dataArray, labels, type, styleIndex, isMemory],
  );

  return (
    <div className={s.sparkline}>
      <Line data={data} options={options} redraw={false} />
    </div>
  );
}