ScalarChart.tsx 8.9 KB
Newer Older
1 2 3
import type {Dataset, Range, ScalarDataset} from '~/resource/scalar';
import LineChart, {LineChartRef, XAxisType, YAxisType} from '~/components/LineChart';
import React, {FunctionComponent, useCallback, useMemo, useRef, useState} from 'react';
4
import {
P
Peter Pan 已提交
5 6
    SortingMethod,
    XAxis,
7
    axisRange,
8
    chartData,
P
Peter Pan 已提交
9
    options as chartOptions,
10
    nearestPoint,
11
    range,
12
    singlePointRange,
13 14 15 16
    sortingMethodMap,
    tooltip,
    transform,
    xAxisMap
P
Peter Pan 已提交
17
} from '~/resource/scalar';
18
import {rem, size} from '~/utils/style';
19
import type {scalar_axis_range, scalar_range, scalar_transform} from '@visualdl/wasm'; // eslint-disable-line @typescript-eslint/no-unused-vars
20

21
import ChartToolbox from '~/components/ChartToolbox';
22 23
import type {EChartOption} from 'echarts';
import type {Run} from '~/types';
P
Peter Pan 已提交
24
import TooltipTable from '~/components/TooltipTable';
25
import {cycleFetcher} from '~/utils/fetch';
26
import ee from '~/utils/event';
P
Peter Pan 已提交
27
import {format} from 'd3-format';
28
import queryString from 'query-string';
P
Peter Pan 已提交
29
import {renderToStaticMarkup} from 'react-dom/server';
30 31 32
import styled from 'styled-components';
import useHeavyWork from '~/hooks/useHeavyWork';
import {useRunningRequest} from '~/hooks/useRequest';
33 34
import {useTranslation} from 'react-i18next';
import wasm from '~/utils/wasm';
35

P
Peter Pan 已提交
36 37
const labelFormatter = format('.8');

38
const smoothWasm = () =>
39
    wasm<typeof scalar_transform>('scalar_transform').then((scalar_transform): typeof transform => params =>
P
Peter Pan 已提交
40
        scalar_transform(params.datasets, params.smoothing)
41
    );
42 43 44 45 46 47

const axisRangeWasm = () =>
    wasm<typeof scalar_axis_range>('scalar_axis_range').then((scalar_axis_range): typeof axisRange => params =>
        scalar_axis_range(params.datasets, params.outlier)
    );

48
const rangeWasm = () =>
49 50
    wasm<typeof scalar_range>('scalar_range').then((scalar_range): typeof range => params =>
        scalar_range(params.datasets)
51 52
    );

53 54
// const smoothWorker = () => new Worker('~/worker/scalar/smooth.worker.ts', {type: 'module'});
// const rangeWorker = () => new Worker('~/worker/scalar/range.worker.ts', {type: 'module'});
55

56 57 58 59 60 61 62 63
const Wrapper = styled.div`
    ${size('100%', '100%')}
    display: flex;
    flex-direction: column;
    align-items: stretch;
    justify-content: space-between;
`;

64
const StyledLineChart = styled(LineChart)`
65 66 67 68 69 70
    flex-grow: 1;
`;

const Toolbox = styled(ChartToolbox)`
    margin-left: ${rem(20)};
    margin-right: ${rem(20)};
P
Peter Pan 已提交
71
    margin-bottom: ${rem(18)};
72 73
`;

74
const Error = styled.div`
75
    ${size('100%', '100%')}
76 77 78 79 80
    display: flex;
    justify-content: center;
    align-items: center;
`;

81
type ScalarChartProps = {
82 83
    cid: symbol;
    runs: Run[];
84 85
    tag: string;
    smoothing: number;
P
Peter Pan 已提交
86 87
    xAxis: XAxis;
    sortingMethod: SortingMethod;
88
    outlier?: boolean;
89
    smoothedOnly?: boolean;
90
    showMostValue?: boolean;
91 92 93 94
    running?: boolean;
};

const ScalarChart: FunctionComponent<ScalarChartProps> = ({
95
    cid,
96 97 98 99 100 101
    runs,
    tag,
    smoothing,
    xAxis,
    sortingMethod,
    outlier,
102
    smoothedOnly,
103
    showMostValue,
104 105
    running
}) => {
P
Peter Pan 已提交
106
    const {t, i18n} = useTranslation(['scalar', 'common']);
107

108 109
    const echart = useRef<LineChartRef>(null);

P
Peter Pan 已提交
110
    const {data: datasets, error, loading} = useRunningRequest<(ScalarDataset | null)[]>(
P
Peter Pan 已提交
111
        runs.map(run => `/scalar/list?${queryString.stringify({run: run.label, tag})}`),
112 113
        !!running,
        (...urls) => cycleFetcher(urls)
114 115
    );

116 117 118 119 120 121
    const [maximized, setMaximized] = useState<boolean>(false);
    const toggleMaximized = useCallback(() => {
        ee.emit('toggle-chart-size', cid, !maximized);
        setMaximized(m => !m);
    }, [cid, maximized]);

P
Peter Pan 已提交
122
    const xAxisType = useMemo(() => (xAxis === XAxis.WallTime ? XAxisType.time : XAxisType.value), [xAxis]);
123 124 125 126 127

    const [yAxisType, setYAxisType] = useState<YAxisType>(YAxisType.value);
    const toggleYAxisType = useCallback(() => {
        setYAxisType(t => (t === YAxisType.log ? YAxisType.value : YAxisType.log));
    }, [setYAxisType]);
128

129 130
    const transformParams = useMemo(
        () => ({
131
            datasets: datasets?.map(data => data ?? []) ?? [],
132 133 134 135
            smoothing
        }),
        [datasets, smoothing]
    );
136
    const smoothedDatasetsOrUndefined = useHeavyWork(smoothWasm, null, transform, transformParams);
137 138 139 140
    const smoothedDatasets = useMemo<NonNullable<typeof smoothedDatasetsOrUndefined>>(
        () => smoothedDatasetsOrUndefined ?? [],
        [smoothedDatasetsOrUndefined]
    );
141

142
    const axisRangeParams = useMemo(
143 144 145 146 147 148
        () => ({
            datasets: smoothedDatasets,
            outlier: !!outlier
        }),
        [smoothedDatasets, outlier]
    );
149 150 151 152
    const yRange = useHeavyWork(axisRangeWasm, null, axisRange, axisRangeParams);

    const datasetRangesParams = useMemo(() => ({datasets: smoothedDatasets}), [smoothedDatasets]);
    const datasetRanges = useHeavyWork(rangeWasm, null, range, datasetRangesParams);
153

154 155 156 157 158 159
    const ranges: Record<'x' | 'y', Range | undefined> = useMemo(() => {
        let x: Range | undefined = undefined;
        let y: Range | undefined = yRange;

        // if there is only one point, place it in the middle
        if (smoothedDatasets.length === 1 && smoothedDatasets[0].length === 1) {
160
            if ([XAxisType.value, XAxisType.log].includes(xAxisType)) {
161 162 163 164 165
                x = singlePointRange(smoothedDatasets[0][0][xAxisMap[xAxis]]);
            }
            y = singlePointRange(smoothedDatasets[0][0][2]);
        }
        return {x, y};
166
    }, [smoothedDatasets, yRange, xAxisType, xAxis]);
167

168 169
    const data = useMemo(
        () =>
170
            chartData({
171
                data: smoothedDatasets.slice(0, runs.length),
172
                ranges: showMostValue ? datasetRanges ?? [] : [],
173
                runs,
174 175
                xAxis,
                smoothedOnly
176
            }),
177
        [smoothedDatasets, datasetRanges, runs, xAxis, smoothedOnly, showMostValue]
178 179
    );

P
Peter Pan 已提交
180 181 182 183 184
    const maxStepLength = useMemo(
        () => String(Math.max(...smoothedDatasets.map(i => Math.max(...i.map(j => j[1]))))).length,
        [smoothedDatasets]
    );

185 186
    const formatter = useCallback(
        (params: EChartOption.Tooltip.Format | EChartOption.Tooltip.Format[]) => {
P
Peter Pan 已提交
187
            const series: Dataset[number] = Array.isArray(params) ? params[0].data : params.data;
188 189 190 191
            const points = nearestPoint(smoothedDatasets ?? [], runs, series[1]).map((point, index) => ({
                ...point,
                ...datasetRanges?.[index]
            }));
192
            const sort = sortingMethodMap[sortingMethod];
P
Peter Pan 已提交
193
            const sorted = sort(points, series);
194
            const {columns, data} = tooltip(sorted, maxStepLength, i18n);
P
Peter Pan 已提交
195 196 197
            return renderToStaticMarkup(
                <TooltipTable run={t('common:runs')} runs={sorted.map(i => i.run)} columns={columns} data={data} />
            );
198
        },
199
        [smoothedDatasets, datasetRanges, runs, sortingMethod, maxStepLength, t, i18n]
200 201
    );

P
Peter Pan 已提交
202 203 204 205 206
    const options = useMemo(
        () => ({
            ...chartOptions,
            tooltip: {
                ...chartOptions.tooltip,
207 208 209
                formatter,
                hideDelay: 300,
                enterable: true
P
Peter Pan 已提交
210 211 212
            },
            xAxis: {
                type: xAxisType,
P
Peter Pan 已提交
213 214 215 216
                ...ranges.x,
                axisPointer: {
                    label: {
                        formatter:
P
Peter Pan 已提交
217 218 219
                            xAxisType === XAxisType.time
                                ? undefined
                                : ({value}: {value: number}) => labelFormatter(value)
P
Peter Pan 已提交
220 221
                    }
                }
P
Peter Pan 已提交
222 223 224 225 226 227 228 229 230
            },
            yAxis: {
                type: yAxisType,
                ...ranges.y
            }
        }),
        [formatter, ranges, xAxisType, yAxisType]
    );

231 232 233
    // display error only on first fetch
    if (!data && error) {
        return <Error>{t('common:error')}</Error>;
234 235
    }

236
    return (
237
        <Wrapper>
P
Peter Pan 已提交
238
            <StyledLineChart ref={echart} title={tag} options={options} data={data} loading={loading} zoom />
239 240 241 242 243
            <Toolbox
                items={[
                    {
                        icon: 'maximize',
                        activeIcon: 'minimize',
P
Peter Pan 已提交
244 245
                        tooltip: t('scalar:maximize'),
                        activeTooltip: t('scalar:minimize'),
246 247 248 249 250
                        toggle: true,
                        onClick: toggleMaximized
                    },
                    {
                        icon: 'restore-size',
P
Peter Pan 已提交
251
                        tooltip: t('scalar:restore'),
252 253 254 255
                        onClick: () => echart.current?.restore()
                    },
                    {
                        icon: 'log-axis',
P
Peter Pan 已提交
256
                        tooltip: t('scalar:toggle-log-axis'),
257 258 259 260 261
                        toggle: true,
                        onClick: toggleYAxisType
                    },
                    {
                        icon: 'download',
P
Peter Pan 已提交
262
                        tooltip: t('scalar:download-image'),
263 264 265 266 267
                        onClick: () => echart.current?.saveAsImage()
                    }
                ]}
            />
        </Wrapper>
268 269 270 271
    );
};

export default ScalarChart;