-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathHistogramChart.tsx
More file actions
151 lines (138 loc) · 4.22 KB
/
HistogramChart.tsx
File metadata and controls
151 lines (138 loc) · 4.22 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2025 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { ReactElement, useMemo } from 'react';
import { FormatOptions, BucketTuple, ThresholdOptions } from '@perses-dev/core';
import { EChart, getFormattedAxis, useChartsTheme } from '@perses-dev/components';
import { use, EChartsCoreOption } from 'echarts/core';
import { CustomSeriesRenderItemAPI, CustomSeriesRenderItemParams } from 'echarts';
import { CustomChart } from 'echarts/charts';
import { getColorFromThresholds } from '../utils';
// eslint-disable-next-line react-hooks/rules-of-hooks
use([CustomChart]);
export interface HistogramChartData {
buckets: BucketTuple[];
}
export interface HistogramChartProps {
width: number;
height: number;
data: HistogramChartData;
format?: FormatOptions;
min?: number;
max?: number;
thresholds?: ThresholdOptions;
// TODO: exponential?: boolean;
}
export function HistogramChart({
width,
height,
data,
format,
min,
max,
thresholds,
}: HistogramChartProps): ReactElement | null {
const chartsTheme = useChartsTheme();
const transformedData = useMemo(() => {
return data.buckets.map(([bucket, lowerBound, upperBound, count]) => {
return {
value: [parseFloat(lowerBound), parseFloat(upperBound), parseFloat(count), bucket],
itemStyle: {
color: getColorFromThresholds(
parseFloat(lowerBound),
thresholds,
chartsTheme,
chartsTheme.echartsTheme[0] as string
),
},
};
});
}, [chartsTheme, data.buckets, thresholds]);
const minXAxis: number | undefined = useMemo(() => {
if (min) {
return min;
}
if (transformedData && transformedData[0]) {
return Math.min(0, Math.floor(transformedData[0]?.value[0] ?? 0));
}
return undefined;
}, [min, transformedData]);
const maxXAxis: number | undefined = useMemo(() => {
if (max) {
return max;
}
if (transformedData && transformedData[transformedData.length - 1]) {
return Math.ceil(transformedData[transformedData.length - 1]?.value[1] ?? 1);
}
return undefined;
}, [max, transformedData]);
const option: EChartsCoreOption = useMemo(() => {
if (!transformedData) return chartsTheme.noDataOption;
return {
title: {
show: false,
},
tooltip: {},
xAxis: {
scale: false,
min: minXAxis,
max: maxXAxis,
},
yAxis: getFormattedAxis({}, format),
series: [
{
type: 'custom',
renderItem: function (params: CustomSeriesRenderItemParams, api: CustomSeriesRenderItemAPI): unknown {
const yValue = api.value(2);
const start = api.coord([api.value(0), yValue]);
const size = api.size?.([(api.value(1) as number) - (api.value(0) as number), yValue]) as number[];
const style = api.style?.();
return {
type: 'rect',
shape: {
x: start[0],
y: start[1],
width: size[0],
height: size[1],
},
style: style,
};
},
label: {
show: false,
},
dimensions: ['from', 'to'],
encode: {
x: [0, 1],
y: 2,
tooltip: [0, 1],
itemName: 2,
},
data: transformedData,
},
],
};
}, [chartsTheme.noDataOption, format, maxXAxis, minXAxis, transformedData]);
return (
<EChart
style={{
width: width,
height: height,
}}
sx={{
padding: `${chartsTheme.container.padding.default}px`,
}}
option={option}
theme={chartsTheme.echartsTheme}
/>
);
}