-
I stumbled on an issue that I have a hard time working around, although it might be simply due to my partial understanding of Framework. I have a D3 graph layout inside a card, like shown below, where
The issue I face is that any update, either to My question is: should I simply get rid of the Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I agree that the resize helper is not of great help here. If the chart has an update() function attached to it, you might as well use it to also react to width updates. |
Beta Was this translation helpful? Give feedback.
-
You don’t have to create a new chart from scratch every time something changes; you can instead update an existing chart. But this tends to be more work than simply redrawing the chart from scratch: you need to track the dependencies from state to display explicitly so that when a certain bit of state changes, you update the corresponding part of the display. Here’s an example of using the The
The ```js
const svg = d3.create("svg").attr("fill", "currentColor");
const text = svg.append("text").attr("dy", "0.35em").text("wut");
function updateChart({width, height, value}) {
if (width !== undefined) {
svg.attr("width", width);
text.attr("x", width / 2);
}
if (height !== undefined) {
svg.attr("height", height);
text.attr("y", height / 2);
}
if (value !== undefined) {
text.text(value);
}
return svg.node();
}
updateChart({height: 400});
```
```js
const clicks = view(Inputs.button("Increment"));
```
<div class="card">
${resize((width) => updateChart({width}))}
</div>
```js
updateChart({value: clicks});
``` |
Beta Was this translation helpful? Give feedback.
You don’t have to create a new chart from scratch every time something changes; you can instead update an existing chart. But this tends to be more work than simply redrawing the chart from scratch: you need to track the dependencies from state to display explicitly so that when a certain bit of state changes, you update the corresponding part of the display.
Here’s an example of using the
resize
helper to update an existing chart rather than redraw it from scratch. When the page loads, a detached “skeleton” chart is initially drawn (in the selectionsvg
). Then there’s anupdateChart
method that you can call whenever you want to update the chart; it performs the minimal update necessary t…