http://bost.ocks.org/mike/chart/
One possibility is that every D3 example becomes a de facto chart,
albeit with limited configurability. This might be become a new best
practice for examples, and encourage greater reusability. And, you'd
still be able to modify the chart implementation to do whatever you
like, and learn from its implementation.
Mike
I'm curious if you have any thoughts on maintainable and elegant patterns that combine persistence (to and from a JSON object) with the pattern you describe?
I like the method chaining with getter and setter methods but the charts I create are have a set of properties a user can interactively change (axis scale add/remove data points).
I could imagine making a method that return all the user changable properties in a JSON object, and a separate method that accepts a JSON object and calls the various setters to distribute the updated property values.
A simple, open-ended directive. This can be used for data models as well.
A useful pattern is to dispatch events when setters activate:
https://github.com/mbostock/d3/wiki/Internals#wiki-d3_dispatch
On Mon, Feb 27, 2012 at 11:36 PM, Stephen Bannasch
<stephen....@deanbrook.org> wrote:
> At 12:57 PM -0800 2/27/12, Mike Bostock wrote:
This library is great and very useful.
It is not *required* as it is my own iniciative.
28. únor 2012 15:55:27, Ariel Marcus napsal:
> A useful pattern is to dispatch events when setters activate:
Yep, similar to the brush component. I think there's further
convention to establish regarding how charts (or other reusable
visualization components) could keep state and dispatch events. The
brush component now uses an internal wrapper on top of d3.dispatch
that enforces some standardization regarding the use of d3.event and
preserving the `this`, `d` and `i` for callback:
https://github.com/mbostock/d3/blob/master/src/core/event.js#L14-49
Potentially as we start to build more reusable interactive components
outside of the core library, that helper could be cleaned up (if
needed) and made public.
Mike
Right, I've stopped doing this for new examples, and I've filed an
issue to update the old ones (though I expect I'll never be able to
update every existing example, since they exist in the wild):
https://github.com/mbostock/d3/issues/565
You'll note the tutorial had a CSV file, which is hopefully the most
accessible way of representing a dataset.
Mike
This sounds like a good approach. It feels like it might lead to more
of an ecosystem of lightweight components that borrow ideas from each
other while remaining configurable, rather than any kind of one-fits-all
heavyweight chart. Making at least some of the D3 examples configurable
could help kick-start the process. I look forward to seeing the
ecosystem flourish!
--
Jason Davies, http://www.jasondavies.com/
I am fairly new to d3 & javascript, so feedback is much appreciated!
Lars
--
Lars Grammel
PhD Candidate, The CHISEL Group, University of Victoria
http://larsgrammel.de
http://twitter.com/lgrammel
You can access it directly, http://bl.ocks.org/d/1935509/
Mike
I also adapted my simple grapher that combines zoom, pan, and axis re-scaling to the closure pattern Mike described:
http://lab.dev.concord.org/tests/simple-graphs-closures/one-graph.html
http://lab.dev.concord.org/tests/simple-graphs-closures/two-graphs.html
These have the same UI accordances in my earlier examples:
- Drag on the canvas to translate/pan the graph.
- double-click on the canvas to zoom in
- shift-double-click on the canvas to zoom out
- Drag on one of the X or Y axis numeric labels to re-scale that axis
- click on a data point to select it
- drag a selected data point up or down to change it's Y value
- enter the delete or backspace key to delete a selected data point
- hold the ALT/Option key down and click an empty area of the graph to add a data point
Here's the equivalent using a prototypical class-pattern.
I'm wondering if there are differences in performance or memory usage between the two styles of JavaScript programming.
The overhead of closures is minimal in modern JavaScript engines. It's
still there, but it should only matter if you were creating hundreds
of thousands of charts on the same page. But then you'd be rendering
hundreds of thousands of SVG elements, and you'd have bigger problems.
:) Also, you never know how the runtime will optimize the code, so
sometimes closures can be faster than branching.
If I am creating hundreds of thousands, or millions of things, I
generally create data (structs) rather than code (objects, in
object-oriented speak). The fastest things to create are array
literals, object literals, and primitives.
Mike
Heya,
Sorry, in that second paragraph, it should be *easily reusable* examples, since that's the key point with these new-style charts.
Cheers,
Victor
Are you referring to d3.svg.axis?
https://github.com/mbostock/d3/wiki/SVG-Axes
Mike