-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chart constructor support in API #76
Comments
Hey there @knuton, First off, thanks for your interest in d3.chart! You've clearly done your All three of your suggestions attempt to extend the "D3.js-like" APIs with Your intention ("Chart constructor support") is very well-appreciated, though. D3.js's API abstracts many of the details relating to classes and object Instead of trying to extend the current "D3.js-like" API with support for more var chartNs = {};
d3.chart.register = function(name, ChartCtor) {
return chartNs[name] = ChartCtor;
};
// d3.chart
// A factory for creating chart constructors and retrieving registered chart
// constructors.
d3.chart = function(name, protoProps, staticProps) {
var ChartCtor;
if (arguments.length === 0) {
return Chart;
} else if (arguments.length === 1) {
return chartNs[name];
}
ChartCtor = Chart.extend(protoProps, staticProps);
d3.chart.register(name, ChartCtor);
return ChartCtor;
};
// d3.Chart
// A constructor for an "empty" chart. Generally, you will want to define new
// Chart constructors using the `d3.Chart.exted` method.
d3.Chart = Chart; // Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
+ d3.chart.register(name, child);
- Chart[name] = child;
return child;
}; This is the practical effect on your first two use cases: // #1 - constructor registration
+ d3.chart.register("MyChart", MyChart);
- d3.chart("MyChart", MyChart);
// #2 - Chart instantiation
+ new MyChart(d3.select("body"), { option: 23 });
- d3.select("body").chart(MyChart, { option: 23 }); Unfortunately, the separation of "d3-like" API and "plain JavaScript" API is Thoughts? |
Of course I was a little proud of having sneaked everything nicely into the existing API. But I think you are right, it should be easier for library users if d3.chart supports both paradigms separately and everything has its place. I think your suggestion elegantly achieves that. As for the case of One more thought. After your proposed change there are both Thanks! |
How should we go on, should I prepare a patch for it so we can see it in action? |
It seems that there are many approaches to this problem. Not all my charts, Thanks, @iros On Feb 23, 2014, at 5:56 AM, Johannes Emerich [email protected] How should we go on, should I prepare a patch for it so we can see it in Reply to this email directly or view it on |
Yes. Apologies. Replying via email without thread context is clearly a bad On Sunday, February 23, 2014, Johannes Emerich [email protected]
Excuse my brevity, typed on the go. |
Let's formalize the open questions:
@knuton @iros I'm still not sure about |
|
I'd like to propose a couple of non-breaking API changes for v0.2(.x). Both concern the handling of chart constructors.
As expressed in #62, one of the goals for v0.2 is committing to constructors. It's great to have a formal commitment already, but I think the API could be easily adapted to further improve support for them. These changes should address some of the issues discussed in #41.
I propose the following changes:
Allow registration of chart constructors via
d3.chart
:This allows to make readymade charts available under a name that makes sense to the respective project. In practice this is already possible through
d3.chart()['SpecialChart'] = SpecialChart
, but official API support would be nice.Accept chart constructor references in
d3.selection.prototype.chart
:This allows to conveniently use chart components from the d3 API without storing everything in the
d3
object. It is just as concise as using a chart name.Make chart name optional in
Chart.extend
, i.e. if first argument (name
) is an object instead of a string, shift arguments accordingly and simply don't store a reference to the new chart inChart[name]
. This would allow the convenient creation of not globally registered chart constructors. @jugglinmike, I saw in Specifying charts without using a name #41 that you were worried about library users creating anonymous charts directly viad3.selection.prototype.chart
, which I think is a valid concern. So I would prohibit that, and only allow creation of anonymous charts viad3.chart
andChart.extend
.What do you think? Any oversights?
The text was updated successfully, but these errors were encountered: