I think this pattern is very exciting. I have a few questions about it.
Question 1 - Why is data
so special?
With the constructor pattern, why is data
given such special treatment as a separate argument?
In particular, why is it
function LineChart(data, {x, y, width, height} = {}) {
and not
function LineChart({data, x, y, width, height} = {}) {
?
Question 2 - How to emit various different kinds of events?
I see that the pattern that’s compatible with viewof
is like this (from Line Chart, Index Chart):
svg
.property("value", date)
.dispatch("input", {bubbles: true}); // for viewof
First of all, why is {bubbles: true}
included?
Secondly, is there any difference between these two
svg.property("value", date)
svg.node().value = date;
?
The main question is: how might might one adapt this pattern so that the chart can emit various different types of events, such as “markClicked” or “markHovered”? Would the following make sense:
svg.property("value", datum).dispatch("markClicked");
svg.property("value", datum).dispatch("markHovered");
?
I guess the question is, is it really a best practice to mutate node.value
and expect the event listener to grab that synchronously like this
select(chart).on('markClicked', (event) => {
const datum = event.target.value;
console.log(datum);
});
?
Is there no alternative to this awkward mutation of the DOM value
property that would allow us to place the value directly inside the event? Or, is it done this way because this is how it has been done in the past for native interactive DOM elements like input sliders? I suppose that makes sense, because seeing event.target.value
is familiar to folks.
Question 3 - Any solid convention for the update
function?
So far, the chart.update
pattern seems pretty ad-hoc, as its arguments are totally distinct and disconnected from the arguments to the constructor. In particular I’m looking at the Bar Chart Transitions example.
In my view, the ideal pattern would allow invocation of chart.update
with exactly the same set of options as the constructor, with the additional feature that if an option is omitted, it should fall back to what it was set to previously.
Anyway, lots of pros and cons to consider and evaluate, but this pattern looks most promising as a general replacement for the Towards Reusable Charts pattern! Thank you for all the hart work that has gone into this initiative.