-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08a4fb5
commit 9dbcd1e
Showing
12 changed files
with
298 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from 'react'; | ||
|
||
export const BrushHandleComponent: React.FC<{ | ||
x?: number; | ||
y?: number; | ||
width?: number; | ||
height?: number; | ||
isDark?: boolean; | ||
}> = ({ x, y, width, height, isDark }) => { | ||
if (x === undefined || y === undefined || width === undefined || height === undefined) { | ||
return null; | ||
} | ||
|
||
const triangleSize = 6; | ||
const color = isDark ? '#D2D2D2' : '#0066CC'; | ||
return ( | ||
<g> | ||
<line x1={x} x2={x} y1={y} y2={height + y} style={{ stroke: color, strokeDasharray: '5 3' }} /> | ||
<polygon | ||
points={`${x},${y} ${x - triangleSize},${y - triangleSize} ${x + triangleSize},${y - triangleSize}`} | ||
style={{ fill: color }} | ||
/> | ||
</g> | ||
); | ||
}; | ||
|
||
export default BrushHandleComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
Chart, | ||
ChartAxis, | ||
ChartBar, | ||
ChartLegendTooltip, | ||
ChartStack, | ||
ChartThemeColor, | ||
createContainer | ||
} from '@patternfly/react-charts'; | ||
import * as React from 'react'; | ||
import { TimeRange } from '../../utils/datetime'; | ||
import { getDateMsInSeconds, getDateSInMiliseconds } from '../../utils/duration'; | ||
import { NamedMetric } from '../../api/loki'; | ||
import { getFormattedRateValue } from '../../utils/metrics'; | ||
import BrushHandleComponent from './brush-handle'; | ||
import './metrics-content.css'; | ||
import { | ||
ChartDataPoint, | ||
defaultDimensions, | ||
Dimensions, | ||
getHistogramRangeFromLimit, | ||
observe, | ||
toDatapoints | ||
} from './metrics-helper'; | ||
|
||
export type HistogramProps = { | ||
id: string; | ||
title: string; | ||
totalMetric: NamedMetric; | ||
limit: number; | ||
isDark: boolean; | ||
setRange: (tr: TimeRange) => void; | ||
}; | ||
|
||
export const Histogram: React.FC<HistogramProps> = ({ id, title, totalMetric, limit, isDark, setRange }) => { | ||
const VoronoiContainer = createContainer('voronoi', 'brush'); | ||
const datapoints: ChartDataPoint[] = toDatapoints(totalMetric); | ||
|
||
const containerRef = React.createRef<HTMLDivElement>(); | ||
const [dimensions, setDimensions] = React.useState<Dimensions>(defaultDimensions); | ||
React.useEffect(() => { | ||
observe(containerRef, dimensions, setDimensions); | ||
}, [containerRef, dimensions]); | ||
|
||
return ( | ||
<div id={`chart-${id}`} className="metrics-content-div" ref={containerRef}> | ||
<Chart | ||
themeColor={ChartThemeColor.multiUnordered} | ||
ariaTitle={title} | ||
containerComponent={ | ||
<VoronoiContainer | ||
activateData={false} | ||
cursorDimension="x" | ||
voronoiDimension="x" | ||
brushDimension="x" | ||
labels={(dp: { datum: ChartDataPoint }) => { | ||
return dp.datum.y || dp.datum.y === 0 ? getFormattedRateValue(dp.datum.y, 'count') : 'n/a'; | ||
}} | ||
labelComponent={<ChartLegendTooltip title={(datum: ChartDataPoint) => datum.date} />} | ||
mouseFollowTooltips={true} | ||
voronoiPadding={0} | ||
onBrushDomainChange={(updated?: { x?: Array<Date | number> }) => { | ||
if (totalMetric && limit && updated?.x && updated.x.length && typeof updated.x[0] === 'object') { | ||
const start = getDateMsInSeconds(updated.x[0].getTime()); | ||
const range = getHistogramRangeFromLimit(totalMetric, limit, start); | ||
|
||
updated.x = [new Date(getDateSInMiliseconds(range.from)), new Date(getDateSInMiliseconds(range.to))]; | ||
} | ||
}} | ||
onBrushDomainChangeEnd={(domain?: { x?: Array<Date | number> }) => { | ||
if ( | ||
domain?.x && | ||
domain.x.length > 1 && | ||
typeof domain.x[0] === 'object' && | ||
typeof domain.x[1] === 'object' | ||
) { | ||
const start = domain.x[0]; | ||
const end = domain.x[1]; | ||
|
||
if (start.getTime() < end.getTime()) { | ||
setRange?.({ from: getDateMsInSeconds(start.getTime()), to: getDateMsInSeconds(end.getTime()) }); | ||
} | ||
} | ||
}} | ||
handleComponent={<BrushHandleComponent isDark={isDark} />} | ||
defaultBrushArea="none" | ||
handleWidth={0} | ||
brushStyle={{ stroke: 'transparent', fill: 'black', fillOpacity: 0.1, zIndex: 999999 }} | ||
allowDrag={true} | ||
allowDraw={true} | ||
allowResize={true} | ||
brushDomain={{ x: [0, 0], y: [0, 0] }} | ||
/> | ||
} | ||
//TODO: fix refresh on selection change to enable animation | ||
//animate={true} | ||
scale={{ x: 'time', y: 'linear' }} | ||
width={dimensions.width} | ||
height={dimensions.height} | ||
domainPadding={{ x: 50, y: 0 }} | ||
padding={{ | ||
top: 10, | ||
right: 10, | ||
bottom: 35, | ||
left: 60 | ||
}} | ||
> | ||
<ChartAxis fixLabelOverlap /> | ||
<ChartAxis dependentAxis showGrid fixLabelOverlap tickFormat={y => getFormattedRateValue(y, 'count')} /> | ||
<ChartStack> | ||
<ChartBar | ||
name={`bar-${id}`} | ||
key={`bar-${id}`} | ||
data={datapoints} | ||
barWidth={(dimensions.width / datapoints.length) * 0.8} | ||
alignment={'start'} | ||
labelComponent={<g />} | ||
/> | ||
</ChartStack> | ||
</Chart> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Histogram; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.