Skip to content

Commit

Permalink
fix svg ref and add more descriptive names
Browse files Browse the repository at this point in the history
  • Loading branch information
arrowtip committed Dec 14, 2023
1 parent f1e42f3 commit 2fbcf76
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions src/components/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ interface GraphProps {

function Graph({ nodeName, oeName }: GraphProps) {
const [history, setHistory] = useState<ObjectEntryEvent[]>([]);
const svgRef = useRef("graphRef");
const lineRef = useRef<any>();
const svgRef = useRef(null); // see react docs for DOM manipulation with refs
const xScaleRef = useRef<d3.ScaleLinear<number, number, never>>(d3.scaleLinear().range([0, 100]));
const yScaleRef = useRef<d3.ScaleLinear<number, number, never>>(d3.scaleLinear().range([0, 100]));
const line = d3.line()
.curve(d3.curveBasis)
.x((d, i: number) => {

Check failure on line 22 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'i' is declared but its value is never read.

Check failure on line 22 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

'i' is declared but its value is never read.

Check failure on line 22 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'i' is declared but its value is never read.
const oeEvt: ObjectEntryEvent = (d as unknown) as ObjectEntryEvent;
return xScaleRef.current(oeEvt.timestamp)})
.y((d, i: number) => {

Check failure on line 25 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'i' is declared but its value is never read.

Check failure on line 25 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

'i' is declared but its value is never read.

Check failure on line 25 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'i' is declared but its value is never read.
const oeEvt: ObjectEntryEvent = (d as unknown) as ObjectEntryEvent;
return yScaleRef.current(oeEvt.value as number)});


const frameSize: number = 1000; // in milliseconds
const minInterval: number = 1000; // in milliseconds
Expand All @@ -41,12 +50,13 @@ function Graph({ nodeName, oeName }: GraphProps) {
return newVec;
})
// redraw chart line
d3.select("path")
.attr("d", lineRef.current)
let path = d3.select(svgRef.current).select("g g path.line")
.attr("d", (d) => line(d))

Check failure on line 54 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

No overload matches this call.

Check failure on line 54 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

No overload matches this call.

Check failure on line 54 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

No overload matches this call.
.attr("transform", null);
// d3.active("path")
// .attr("transform", "translate(" + xScaleRef.current(0) + ",0)")
// .transition();
console.log(path)
d3.active(this)

Check failure on line 57 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

Object is possibly 'null'.

Check failure on line 57 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'this' implicitly has type 'any' because it does not have a type annotation.

Check failure on line 57 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

Object is possibly 'null'.

Check failure on line 57 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

'this' implicitly has type 'any' because it does not have a type annotation.

Check failure on line 57 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

Object is possibly 'null'.

Check failure on line 57 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'this' implicitly has type 'any' because it does not have a type annotation.
.attr("transform", "translate(" + xScaleRef.current(0) + ",0)")
.transition();
};

let unsubscribe = await listen<ObjectEntryHistoryEvent>(historyResponse.event_name, handleNewEvent);
Expand All @@ -68,47 +78,46 @@ function Graph({ nodeName, oeName }: GraphProps) {
useEffect(() => {
console.log("d3 useEffect called");
// setting up svg
const w = 700;
const h = 300;
const width = 700;
const height = 300;
const margin = 40;
const svg = d3.select(svgRef.current)
.attr("width", w)
.attr("height", h)
.attr("width", width)
.attr("height", height)
.style("background", "#d3d3d3")
.style("margin", margin)
.style("overflow", "visible")
const group = svg.append("g");
const svgGroup = svg.append("g");

// setting up scaling
xScaleRef.current = d3.scaleLinear()
.range([0, w]);
const yScale = d3.scaleLinear()
.domain([200, 0])
.range([h, 0]);
.range([0, width]);
yScaleRef.current = d3.scaleLinear()
.domain([0, 200])
.range([height, 0]);

// setting the axes
const xAxis = d3.axisBottom(xScaleRef.current)

Check failure on line 100 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

'xAxis' is declared but its value is never read.

Check failure on line 100 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

'xAxis' is declared but its value is never read.

Check failure on line 100 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

'xAxis' is declared but its value is never read.
.tickFormat(d3.timeFormat("%M:%S"));

Check failure on line 101 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (macos-latest)

No overload matches this call.

Check failure on line 101 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (ubuntu-latest)

No overload matches this call.

Check failure on line 101 in src/components/Graph.tsx

View workflow job for this annotation

GitHub Actions / test-tauri (windows-latest)

No overload matches this call.

lineRef.current = d3.line()
.curve(d3.curveBasis)
.x((d: ObjectEntryEvent, i: number) => { return xScale(d.timestamp)})
.y((d: ObjectEntryEvent, i: number) => { return yScale(d.value)});
// setting up the data for svg

group.append("defs").append("clipPath")
// set up clipping of plot outside of chart rectangle
svgGroup.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", w)
.attr("height", h);
group.append("g")
.attr("width", width)
.attr("height", height);
// create subgroups for x- and y-axis
svgGroup.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + h + ")")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScaleRef.current));
group.append("g")
svgGroup.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(yScale));
group.append("g")
.call(d3.axisLeft(yScaleRef.current));
// create subgroup for actual line of chart (path)
svgGroup.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.datum(history)
Expand All @@ -120,7 +129,7 @@ function Graph({ nodeName, oeName }: GraphProps) {
if (history.length > 0) {
}

}, [history]);
}, []);

return (<div>
<svg ref={svgRef}></svg>
Expand Down

0 comments on commit 2fbcf76

Please sign in to comment.