React component not rendering after useRef object gets updated - reactjs

I have the following component which should generate a d3 bar chart and update it every 3 seconds. It receives a dataset from its parent every 3 seconds.
Right now, it generates the chart but it does not update it. I can confirm that the dataset is updated and received every 3 seconds and then useEffect is triggered.
I have tried to set intervals and replace useRef but it did not work.
Any idea? Any other way I should structure this kind of component to generate D3 elements?
Let me know if you need more info. Thanks
import React, { useRef, useEffect } from "react";
import * as d3 from "d3";
export const BarChart = ({ dataset }) => {
const ref = useRef();
useEffect(() => {
if (dataset && dataset[0].valor) {
const svgHeight = 200;
const svgWidth = 350;
const barPadding = 30;
const barWidth = svgWidth / dataset.length;
const svgElement = d3
.select(ref.current)
.attr("width", svgWidth)
.attr("height", svgHeight);
svgElement
.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("y", d => svgHeight - d.valor - 40)
.attr("x", (d, i) => i * barWidth)
.attr("height", d => d.valor)
.attr("width", barWidth - barPadding)
.attr("fill", d => (d.valor > 47 ? "blue" : "red"));
}
}, [dataset]);
return (
<div>
<svg ref={ref} />
</div>
);
};

I found the solution. The issue was not related to useRef but to the d3 code that I wrote, instead of using .enter().append('rect') I should have used .join(). See below:
svgElement
.selectAll("rect")
.data(dataset)
.join("rect")
.attr("y", d => svgHeight - d.valor - 40)
.attr("x", (d, i) => i * barWidth)
.attr("height", d => d.valor)
.attr("width", barWidth - barPadding)
.attr("fill", d => (d.valor > 47 ? "blue" : "red"));
}

Related

How to use d3 with reactJS / nextJS?

I would like to add a d3 chart, following the tutorials but nothing happens at all. i'm actually not sure if useEffect() is at the good "timing", if I should use componentDidMount, or if it's just not the good way to add in the element... Seems like I'm missing something here!
import React from 'react';
import * as d3 from "d3";
import { useEffect } from 'react';
function drawChart() {
const data = [12, 5, 6, 6, 9, 10];
const h = 100;
const w = 100;
const svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h)
.style("margin-left", 100);
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * 70)
.attr("y", (d, i) => h - 10 * d)
.attr("width", 65)
.attr("height", (d, i) => d * 10)
.attr("fill", "green")
}
const chart: React.FunctionComponent = () => {
useEffect(() => {
drawChart();
}, []);
return (
<div>
</div>
);
};
export default chart;
What can be a source of bugs in this example is that d3 is appending the SVG to the body, which is completely outside of the React DOM.
A better approach could be to add the SVG in the JSX, and use the reference (useRef in hooks) to tell D3 where the chart must be rendered:
import * as React from "react";
import * as d3 from "d3";
function drawChart(svgRef: React.RefObject<SVGSVGElement>) {
const data = [12, 5, 6, 6, 9, 10];
const h = 120;
const w = 250;
const svg = d3.select(svgRef.current);
svg
.attr("width", w)
.attr("height", h)
.style("margin-top", 50)
.style("margin-left", 50);
svg
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", (d, i) => i * 40)
.attr("y", (d, i) => h - 10 * d)
.attr("width", 20)
.attr("height", (d, i) => d * 10)
.attr("fill", "steelblue");
}
const Chart: React.FunctionComponent = () => {
const svg = React.useRef<SVGSVGElement>(null);
React.useEffect(() => {
drawChart(svg);
}, [svg]);
return (
<div id="chart">
<svg ref={svg} />
</div>
);
};
export default Chart;
Here is a codePen for the example

React + D3 + Force-Directed Tree + Adjustable Link Strength

I'm trying to make Force-Directed Tree with React and it works. But I cannot modify "link strength", if I pass it outside component through the props.
Honestly, I can change "strength", but I need to append d3 svg to my react ref div after that to see the changes. And whole graph will be redrawn.
I find example by Mike Bostock. He advice to modify the parameters of a force-directed graph with reheat the simulation using simulation.alpha and simulation.restart. But I cannot make it works with react. Nothing happens.
Here is my code:
export default function Hierarchy(props) {
const {
strength,
lineColor,
lineStroke,
width,
height,
nodeSize,
nodeColor,
} = props;
const root = d3.hierarchy(data);
const links = root.links();
const nodes = root.descendants();
const svg = d3.create("svg");
const link = svg
.append("g")
.selectAll("line")
.data(links)
.join("line");
const node = svg
.append("g")
.selectAll("circle")
.data(nodes)
.join("circle");
function applyStyle(selectionSVG) {
selectionSVG
.attr("width", width)
.attr("height", height)
.attr("viewBox", [-width / 2, -height / 2, width, height]);
selectionSVG
.selectAll("circle")
.attr("r", nodeSize)
.attr("fill", nodeColor)
selectionSVG
.selectAll("line")
.attr("stroke", lineColor)
.attr("stroke-width", lineStroke);
}
applyStyle(svg);
const divRef = React.useRef(null);
const linkForce = d3
.forceLink(links)
.id(d => d.id)
.distance(0)
.strength(strength);
const simulation = d3
.forceSimulation(nodes)
.force("link", linkForce)
.force("charge", d3.forceManyBody().strength(-500))
.force("x", d3.forceX())
.force("y", d3.forceY());
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
node.attr("cx", d => d.x).attr("cy", d => d.y);
});
//ComponentDidMount
useEffect(() => {
//Append d3 svg to ref div
var div = d3.select(divRef.current);
if (div.node().firstChild) {
div.node().removeChild(div.node().firstChild);
}
div.node().appendChild(svg.node());
}, []);
//ComponentDidUpdate
useEffect(() => {
simulation.force("link").strength(strength);
simulation.alpha(1).restart();
}, [strength]);
//ComponentDidUpdate
useEffect(() => {
var div = d3.select(divRef.current);
applyStyle(div.select("svg"));
});
//Render
return <div id="hierarchyTree" ref={divRef} />;
}
Here is Sandbox.
I find solution, if anybody interesting.
The fact is simulation was not saved when component was updated. So I create ref for it.
const simulationRef = React.useRef(simulation)
and replace it in useEffect section
//ComponentDidUpdate
useEffect(() => {
simulationRef.current.force("link").strength(strength)
simulationRef.current.alpha(1).restart()
console.log(simulationRef.current)
}, [strength])
After that everything works fine.

d3 / React / Hooks - Updates do not clean up old rects and Groups

I am working on a realtime updating bar chart implementation using d3 with React and Hooks.
Though the new 'g' groups and rects do get added to the svg, the old groups do not seem to be getting cleared up. So, the rects just get added on top of the old rects, as do the axis groups.
I am using the .join() API so I shouldn't need to do manually clean up with exit.remove() right? I am completely new to d3 so forgive the uncertainty.
app:
import React, { useRef, useEffect, useState } from 'react';
import * as firebase from 'firebase';
import Chart from './Chart';
function useInterval(callback, delay) {
const savedCallback = useRef();
// Remember the latest callback.
useEffect(() => {
savedCallback.current = callback;
}, [callback]);
// Set up the interval.
useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}
function App() {
const [menu, setMenu] = useState([]);
const db = firebase.firestore();
useEffect(() => {
db.collection('dishes')
.get()
.then((res) => {
let data = [];
for (let doc of res.docs) {
data.push(doc.data());
}
setMenu(data);
});
}, []);
useInterval(() => {
let newMenu = [];
newMenu = [...menu];
if (newMenu[0] && newMenu[0].hasOwnProperty('orders')) {
newMenu[0].orders += 50;
setMenu(newMenu);
}
}, 3000);
return (
<div className="App">{menu.length > 0 ? <Chart data={menu} /> : null}</div>
);
}
export default App;
Chart component:
import React, { useRef, useEffect } from 'react';
import * as d3 from 'd3';
const Chart = ({ data }) => {
const height = 600;
// Generate a ref instance
const svgRef = useRef();
useEffect(() => {
const svg = d3.select(svgRef.current);
const margin = { top: 20, right: 20, bottom: 100, left: 100 };
const graphWidth = 600 - margin.left - margin.right;
const graphHeight = height - margin.top - margin.bottom;
// Find the maximum order value
const max = d3.max(data, (d) => d.orders);
// Establish the y scale
// i.e., map my max value to the pixel max value ratio
const y = d3
.scaleLinear()
.domain([0, max * 1.25])
.range([graphHeight, 0]);
// Calculates width and coordinates for each bar
// Can add padding here
const x = d3
.scaleBand()
.domain(data.map((item) => item.name))
.range([0, graphHeight])
.padding(0.25);
const graph = svg
.append('g')
.attr('width', graphWidth)
.attr('height', graphWidth)
.attr('transform', `translate(${margin.left}, ${margin.top})`);
// Creat axis groups for legends and labels
const xAxisGroup = graph
.append('g')
.attr('transform', `translate(0,${graphHeight})`);
const yAxisGroup = graph.append('g');
// Append the graph to the DOM
graph
.selectAll('rect')
.data(data, (entry, i) => entry)
.join(
(enter) => enter.append('rect'),
(update) => update.append('class', 'new'),
(exit) => exit.remove()
)
.transition()
.duration(300)
.attr('width', x.bandwidth)
.attr('height', (d) => graphHeight - y(d.orders))
.attr('fill', 'orange')
.attr('x', (d) => x(d.name))
.attr('y', (d) => y(d.orders));
// Create the axes
const xAxis = d3.axisBottom(x);
const yAxis = d3
.axisLeft(y)
// .ticks(3)
.tickFormat((d) => d + ' orders');
// Append the axes to the graph
xAxisGroup.call(xAxis);
yAxisGroup.call(yAxis);
xAxisGroup
.selectAll('text')
.attr('transform', 'rotate(-40)')
.attr('text-anchor', 'end');
}, [data]);
return (
<div>
<svg ref={svgRef} height={height} width="600" />
</div>
);
};
export default Chart;

passing data from a react based component to vanilla js class

I am trying to pass data from react class base component to a vanillajs class so this class is able to render D3 bar chart ,
I've tried passing the data from the react component through the contractor of the vanilla class , i have the data available in the vanilla class when i try to consol log it , but when i want to call the data variable in the method call d3.data() it is empty , here is the code
React class
//imports..
const _data = []
const firebaseConfig = {
//configuration ..
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore()
class TableOfD3 extends Component {
constructor(){
super()
this.svgId = `SVG_${uuid()}`
}
getData(){
db.collection('db').get().then( res=>{
res.docs.forEach(doc => {
_data.push(doc.data())
})
}
componentDidMount(){
this.start()
}
componentDidUpdate(){
this.start()
}
start(){
this._graph = new D3TableEngine('#' + this.svgId,_data)
this._graph.start()
}
render() {
return (
<div>
<svg id={this.svgId}></svg>
</div>
);
}
}
export default TableOfD3;
// vanillajs class
export default class D3TableEngine {
constructor(svgId, passedData) {
this._svg = d3.select(`${svgId}`);
this._svg.attr('width', _WIDTH)
this._svg.attr('height', _HEIGHT)
this._passedData = passedData
}
start() {
const self = this;
var _g = self._svg;
const graphWidth = _WIDTH - _MARGIN.left - _MARGIN.right
const graphHeight = _HEIGHT - _MARGIN.top - _MARGIN.bottom
const graph = _g.append('g')
.attr('width', graphWidth)
.attr('height', graphHeight)
.attr('transform', `translate(${_MARGIN.left + 20}, ${_MARGIN.top})`)
const xAxisGroup = graph.append('g')
.attr('transform', `translate(0,${graphHeight })`)
const yAxisGroup = graph.append('g')
const yScale = d3.scaleLinear()
.domain([0,d3.max(self._passedData, (d) => d.orders)])
.range([graphHeight,0])
const xScale = d3.scaleBand()
.domain(self._passedData.map((el) => el.name))
.range([0,500])
.paddingInner(0.2)
.paddingOuter(0.2)
const rects = graph.selectAll("rect").data(self._passedData);
rects
.attr("x", (d)=> xScale(d.name))
.attr("y", (d) => yScale( d.orders))
.attr("height", (d)=> graphHeight - yScale( d.orders))
.attr("width", xScale.bandwidth)
.attr('fill', 'blue')
rects
.enter()
.append("rect")
.attr("x", (d)=> xScale(d.name))
.attr("y", (d) => yScale( d.orders))
.attr("height", (d)=> graphHeight - yScale( d.orders ))
.attr("width", xScale.bandwidth)
.attr('fill', 'blue')
const xAxis = d3.axisBottom(xScale)
xAxisGroup.call(xAxis)
const yAxis = d3.axisLeft(yScale)
.ticks(5)
.tickFormat((d) => 'Orders ' +d )
yAxisGroup.call(yAxis)
xAxisGroup.selectAll('text')
.attr('transform', 'rotate(-40)' )
.attr('text-anchor', 'end')
} )
}
refresh() {}
}
I re-wrote your React class because you were doing many things that would be considered anti-pattern. In general, you want to shove as much as you can in this.state. Otherwise, you miss out on the main advantage of React - and that is optimally re-rendering the DOM when variables change. I think the main issue you're likely having is that you're updating the DOM from componentDidUpdate(), which will fire another update. It'll continue infinitely and crash. I would strongly recommend refactoring D3TableEngine into a React Component instead of a plain JS class. The challenge is that the way you have written the d3 component, it has to be destroyed and re-created for each render, which is a problem because React doesn't know what to do other than re-create it.
import React, { Component } from 'react';
class TableOfD3 extends Component {
constructor() {
super();
const firebaseConfig = {
//configuration ..
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
this.state = {
svgId: `SVG_${uuid()}`,
data: [],
db: db
};
}
componentDidMount() {
const response = await this.state.db.collection('db').get();
const data = response.docs.map(doc => doc.data());
this.setState({
data
});
}
componentDidUpdate() {
}
render() {
return (
<div>
<D3TableEngine
id={this.state.svgId}
data={this.state.data}
/>
</div>
);
}
}
UPDATE: I gave a shot at refactoring your d3 class into a React Component. The important pieces here are the ref, which let's you get a reference to the element so redraw can execute all the d3 code on the right svg element. Then, inside componentDidMount and componentDidUpdate, you must call redraw. However, I would refactor the redraw method to break out the parts that will change from the parts that will not change (eg: move the graph pieces into a different function and call that in componentDidUpdate). We do this so that React is performing as expected and only updating the elements in the DOM that have changed. If you need additional help, you may take a look at this jsfiddle example/medium article.
const MARGIN = 0;
const WIDTH = 0;
const HEIGHT = 0;
class D3TableEngine extends Component {
componentDidMount() {
redraw();
}
componentDidUpdate() {
redraw();
}
redraw = () => {
this.svg = d3.select(this.svg);
const graphWidth = WIDTH - MARGIN.left - MARGIN.right
const graphHeight = HEIGHT - MARGIN.top - MARGIN.bottom
const graph = this.svg.append('g')
.attr('width', graphWidth)
.attr('height', graphHeight)
.attr('transform', `translate(${_MARGIN.left + 20}, ${_MARGIN.top})`)
const xAxisGroup = graph.append('g')
.attr('transform', `translate(0,${graphHeight})`)
const yAxisGroup = graph.append('g')
const yScale = d3.scaleLinear()
.domain([0, d3.max(props.data, (d) => d.orders)])
.range([graphHeight, 0])
const xScale = d3.scaleBand()
.domain(props.data.map((el) => el.name))
.range([0, 500])
.paddingInner(0.2)
.paddingOuter(0.2)
const rects = graph.selectAll("rect").data(props.data);
rects
.attr("x", (d) => xScale(d.name))
.attr("y", (d) => yScale(d.orders))
.attr("height", (d) => graphHeight - yScale(d.orders))
.attr("width", xScale.bandwidth)
.attr('fill', 'blue')
rects
.enter()
.append("rect")
.attr("x", (d) => xScale(d.name))
.attr("y", (d) => yScale(d.orders))
.attr("height", (d) => graphHeight - yScale(d.orders))
.attr("width", xScale.bandwidth)
.attr('fill', 'blue')
const xAxis = d3.axisBottom(xScale)
xAxisGroup.call(xAxis)
const yAxis = d3.axisLeft(yScale)
.ticks(5)
.tickFormat((d) => 'Orders ' + d)
yAxisGroup.call(yAxis)
xAxisGroup.selectAll('text')
.attr('transform', 'rotate(-40)')
.attr('text-anchor', 'end')
}
render() {
return (
<svg
id={this.props.svgId}
width={WIDTH}
height={HEIGHT}
ref={el => (this.svg = d3.select(el))}
>
</svg>
);
}
}

Missing rects in bar chart

I'm new to react and d3. I'm trying our barChart but only see one overlapped rect. Examining each rect element, I see that x, y, height and width are expected. But I don't understand why the other 3 rect are not shown.
BarChart.js
import React, { Component } from 'react'
import { scaleLinear } from 'd3-scale';
import { max } from 'd3-array';
import { select } from 'd3-selection';
export default class BarChart extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
this.createBarChart()
}
componentDidUpdate() {
this.createBarChart()
}
createBarChart = () => {
const node = this.node
const dataMax = max(this.props.data)
const yScale = scaleLinear()
.domain([0, dataMax])
.range([0, this.props.size[1]])
select(node)
.selectAll('rect')
.data(this.props.data)
.enter() // placeholder selection
.append('rect') // return a selection of appended rects
select(node)
.selectAll('rect') // rect selection
.data(this.props.data) // update data in rect selection
.exit()
.remove() // exit and remove rects
select(node)
.selectAll('rect') // rect selection
.data(this.props.data) // join data with rect selection
.style('fill', '#fe9922')
.attr('x', (d, i) => i * 25)
.attr('y', d => this.props.size[1] - yScale(d))
.attr('height', d => yScale(d))
.attr('width', 25)
}
render() {
return (
// Pass a reference to the node for D3 to use
<svg ref={node => this.node = node}
width={this.props.width} height={this.props.height}
>
</svg>
)
}
}
With this answer, I've updated createBarChart() but still seeing the same odd rendering.
createBarChart = () => {
const node = this.node
const dataMax = max(this.props.data)
const yScale = scaleLinear()
.domain([0, dataMax])
.range([0, this.props.size[1]])
this.rects = select(node)
.selectAll('rect')
.data(this.props.data)
this.rects
.exit()
.remove()
this.rects = this.rects.enter()
.append('rect')
.merge(this.rects)
.style('fill', '#fe9922')
.attr('x', (d, i) => i * 25)
.attr('y', d => this.props.size[1] - yScale(d))
.attr('height', d => yScale(d))
.attr('width', 25)
}
App.js
<div>
<BarChart data={[50,10,11,13]} size={[500,500]}/>
</div>
Found the bug, I passed in wrong props, this.props.width and this.props.height doesn't exist in this case. As height of my rect overflows the svg box, I can only see one longest bar, but not the other shorter ones.
<svg ref={node => this.node = node}
width={this.props.size[0]} height={this.props.size[1]}
>
</svg>

Resources