Skip to content

Commit

Permalink
create :: error graph
Browse files Browse the repository at this point in the history
  • Loading branch information
dutexion committed Aug 7, 2024
1 parent ed9b287 commit 59086b5
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/components/graph/TraceErrorGraph.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { useEffect, useRef } from 'react';
import Plotly from 'plotly.js-dist-min';

interface DataPoint {
[timestamp: string]: string;
}

interface JsonData {
[key: string]: DataPoint;
}

interface PlotlyChartProps {
jsonData: JsonData;
}

export const TraceErrorGraph: React.FC<PlotlyChartProps> = ({ jsonData }) => {
const chartRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (chartRef.current && jsonData['1']) {
const timestamps = Object.keys(jsonData['1']).map((ts) => ts.substring(11, 16));
const values = Object.values(jsonData['1']).map(Number);

const trace = {
x: timestamps,
y: values,
type: 'bar' as const,
marker: {
color: 'rgba(204, 23, 29, 1)',
},
};

const layout: Partial<Plotly.Layout> = {
width: 428,
height: 180,
margin: {
l: 30,
r: 30,
t: 20,
b: 40,
},
xaxis: {
title: '',
fixedrange: true,
tickvals: timestamps.filter((_, i) => i % Math.ceil(timestamps.length / 4) === 0),
ticktext: timestamps.filter((_, i) => i % Math.ceil(timestamps.length / 4) === 0),
},
yaxis: {
title: '',
fixedrange: true,
},
dragmode: false as const,
hovermode: false as const,
};

const config: Partial<Plotly.Config> = {
displayModeBar: false,
staticPlot: true,
scrollZoom: false,
};

Plotly.newPlot(chartRef.current, [trace], layout, config);
}
}, [jsonData]);

return <div ref={chartRef} />;
};

0 comments on commit 59086b5

Please sign in to comment.