3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview DygraphOptions is responsible for parsing and returning
9 * information about options.
12 // TODO: remove this jshint directive & fix the warnings.
16 import * as utils from
'./dygraph-utils';
17 import DEFAULT_ATTRS from
'./dygraph-default-attrs';
18 import OPTIONS_REFERENCE from
'./dygraph-options-reference';
21 * Interesting member variables: (REMOVING THIS LIST AS I CLOSURIZE)
22 * global_ - global attributes (common among all graphs, AIUI)
23 * user - attributes set by the user
24 * series_ - { seriesName -> { idx, yAxis, options }}
28 * This parses attributes into an object that can be easily queried.
30 * It doesn't necessarily mean that all options are available, specifically
31 * if labels are not yet available, since those drive details of the per-series
32 * and per-axis options.
34 * @param {Dygraph} dygraph The chart to which these options belong.
37 var DygraphOptions
= function(dygraph
) {
42 this.dygraph_
= dygraph
;
45 * Array of axis index to { series : [ series names ] , options : { axis-specific options. }
46 * @type {Array.<{series : Array.<string>, options : Object}>} @private
51 * Contains x-axis specific options, which are stored in the options key.
52 * This matches the yAxes_ object structure (by being a dictionary with an
53 * options element) allowing for shared code.
54 * @type {options: Object} @private
59 // Once these two objects are initialized, you can call get();
60 this.global_
= this.dygraph_
.attrs_
;
61 this.user_
= this.dygraph_
.user_attrs_
|| {};
64 * A list of series in columnar order.
65 * @type {Array.<string>}
69 this.highlightSeries_
= this.get("highlightSeriesOpts") || {};
74 * Not optimal, but does the trick when you're only using two axes.
75 * If we move to more axes, this can just become a function.
77 * @type {Object.<number>}
80 DygraphOptions
.AXIS_STRING_MAPPINGS_
= {
90 * @param {string|number} axis
93 DygraphOptions
.axisToIndex_
= function(axis
) {
94 if (typeof(axis
) == "string") {
95 if (DygraphOptions
.AXIS_STRING_MAPPINGS_
.hasOwnProperty(axis
)) {
96 return DygraphOptions
.AXIS_STRING_MAPPINGS_
[axis
];
98 throw "Unknown axis : " + axis
;
100 if (typeof(axis
) == "number") {
101 if (axis
=== 0 || axis
=== 1) {
104 throw "Dygraphs only supports two y-axes, indexed from 0-1.";
107 throw "Unknown axis : " + axis
;
109 // No axis specification means axis 0.
114 * Reparses options that are all related to series. This typically occurs when
115 * options are either updated, or source data has been made available.
117 * TODO(konigsberg): The method name is kind of weak; fix.
119 DygraphOptions
.prototype.reparseSeries
= function() {
120 var labels
= this.get("labels");
122 return; // -- can't do more for now, will parse after getting the labels.
125 this.labels_
= labels
.slice(1);
127 this.yAxes_
= [ { series
: [], options
: {}} ]; // Always one axis at least.
128 this.xAxis_
= { options
: {} };
131 // Series are specified in the series element:
134 // labels: [ "X", "foo", "bar" ],
137 // foo : {}, // options
for foo
138 // bar : {} // options
for bar
142 // So, if series is found, it's expected to contain per-series data, otherwise set a
144 var seriesDict
= this.user_
.series
|| {};
145 for (var idx
= 0; idx
< this.labels_
.length
; idx
++) {
146 var seriesName
= this.labels_
[idx
];
147 var optionsForSeries
= seriesDict
[seriesName
] || {};
148 var yAxis
= DygraphOptions
.axisToIndex_(optionsForSeries
["axis"]);
150 this.series_
[seriesName
] = {
153 options
: optionsForSeries
};
155 if (!this.yAxes_
[yAxis
]) {
156 this.yAxes_
[yAxis
] = { series
: [ seriesName
], options
: {} };
158 this.yAxes_
[yAxis
].series
.push(seriesName
);
162 var axis_opts
= this.user_
["axes"] || {};
163 utils
.update(this.yAxes_
[0].options
, axis_opts
["y"] || {});
164 if (this.yAxes_
.length
> 1) {
165 utils
.update(this.yAxes_
[1].options
, axis_opts
["y2"] || {});
167 utils
.update(this.xAxis_
.options
, axis_opts
["x"] || {});
169 // For "production" code, this gets removed by uglifyjs.
170 if (process
.env
.NODE_ENV
!= 'production') {
171 this.validateOptions_();
176 * Get a global value.
178 * @param {string} name the name of the option.
180 DygraphOptions
.prototype.get
= function(name
) {
181 var result
= this.getGlobalUser_(name
);
182 if (result
!== null) {
185 return this.getGlobalDefault_(name
);
188 DygraphOptions
.prototype.getGlobalUser_
= function(name
) {
189 if (this.user_
.hasOwnProperty(name
)) {
190 return this.user_
[name
];
195 DygraphOptions
.prototype.getGlobalDefault_
= function(name
) {
196 if (this.global_
.hasOwnProperty(name
)) {
197 return this.global_
[name
];
199 if (DEFAULT_ATTRS
.hasOwnProperty(name
)) {
200 return DEFAULT_ATTRS
[name
];
206 * Get a value for a specific axis. If there is no specific value for the axis,
207 * the global value is returned.
209 * @param {string} name the name of the option.
210 * @param {string|number} axis the axis to search. Can be the string representation
211 * ("y", "y2") or the axis number (0, 1).
213 DygraphOptions
.prototype.getForAxis
= function(name
, axis
) {
217 // Since axis can be a number or a string, straighten everything out here.
218 if (typeof(axis
) == 'number') {
220 axisString
= axisIdx
=== 0 ? "y" : "y2";
222 if (axis
== "y1") { axis
= "y"; } // Standardize on 'y'. Is this bad? I think so.
225 } else if (axis
== "y2") {
227 } else if (axis
== "x") {
228 axisIdx
= -1; // simply a placeholder for below.
230 throw "Unknown axis " + axis
;
235 var userAxis
= (axisIdx
== -1) ? this.xAxis_
: this.yAxes_
[axisIdx
];
237 // Search the user-specified axis option first.
238 if (userAxis
) { // This condition could be removed if we always set up this.yAxes_ for y2.
239 var axisOptions
= userAxis
.options
;
240 if (axisOptions
.hasOwnProperty(name
)) {
241 return axisOptions
[name
];
245 // User-specified global options second.
246 // But, hack, ignore globally-specified 'logscale' for 'x' axis declaration.
247 if (!(axis
=== 'x' && name
=== 'logscale')) {
248 var result
= this.getGlobalUser_(name
);
249 if (result
!== null) {
253 // Default axis options third.
254 var defaultAxisOptions
= DEFAULT_ATTRS
.axes
[axisString
];
255 if (defaultAxisOptions
.hasOwnProperty(name
)) {
256 return defaultAxisOptions
[name
];
259 // Default global options last.
260 return this.getGlobalDefault_(name
);
264 * Get a value for a specific series. If there is no specific value for the series,
265 * the value for the axis is returned (and afterwards, the global value.)
267 * @param {string} name the name of the option.
268 * @param {string} series the series to search.
270 DygraphOptions
.prototype.getForSeries
= function(name
, series
) {
271 // Honors indexes as series.
272 if (series
=== this.dygraph_
.getHighlightSeries()) {
273 if (this.highlightSeries_
.hasOwnProperty(name
)) {
274 return this.highlightSeries_
[name
];
278 if (!this.series_
.hasOwnProperty(series
)) {
279 throw "Unknown series: " + series
;
282 var seriesObj
= this.series_
[series
];
283 var seriesOptions
= seriesObj
["options"];
284 if (seriesOptions
.hasOwnProperty(name
)) {
285 return seriesOptions
[name
];
288 return this.getForAxis(name
, seriesObj
["yAxis"]);
292 * Returns the number of y-axes on the chart.
293 * @return {number} the number of axes.
295 DygraphOptions
.prototype.numAxes
= function() {
296 return this.yAxes_
.length
;
300 * Return the y-axis for a given series, specified by name.
302 DygraphOptions
.prototype.axisForSeries
= function(series
) {
303 return this.series_
[series
].yAxis
;
307 * Returns the options for the specified axis.
309 // TODO(konigsberg): this is y-axis specific. Support the x axis.
310 DygraphOptions
.prototype.axisOptions
= function(yAxis
) {
311 return this.yAxes_
[yAxis
].options
;
315 * Return the series associated with an axis.
317 DygraphOptions
.prototype.seriesForAxis
= function(yAxis
) {
318 return this.yAxes_
[yAxis
].series
;
322 * Return the list of all series, in their columnar order.
324 DygraphOptions
.prototype.seriesNames
= function() {
328 // For "production" code, this gets removed by uglifyjs.
329 if (process
.env
.NODE_ENV
!= 'production') {
332 * Validate all options.
333 * This requires OPTIONS_REFERENCE, which is only available in debug builds.
336 DygraphOptions
.prototype.validateOptions_
= function() {
337 if (typeof OPTIONS_REFERENCE
=== 'undefined') {
338 throw 'Called validateOptions_ in prod build.';
342 var validateOption
= function(optionName
) {
343 if (!OPTIONS_REFERENCE
[optionName
]) {
344 that
.warnInvalidOption_(optionName
);
348 var optionsDicts
= [this.xAxis_
.options
,
349 this.yAxes_
[0].options
,
350 this.yAxes_
[1] && this.yAxes_
[1].options
,
353 this.highlightSeries_
];
354 var names
= this.seriesNames();
355 for (var i
= 0; i
< names
.length
; i
++) {
357 if (this.series_
.hasOwnProperty(name
)) {
358 optionsDicts
.push(this.series_
[name
].options
);
361 for (var i
= 0; i
< optionsDicts
.length
; i
++) {
362 var dict
= optionsDicts
[i
];
364 for (var optionName
in dict
) {
365 if (dict
.hasOwnProperty(optionName
)) {
366 validateOption(optionName
);
372 var WARNINGS
= {}; // Only show any particular warning once.
375 * Logs a warning about invalid options.
376 * TODO: make this throw for testing
379 DygraphOptions
.prototype.warnInvalidOption_
= function(optionName
) {
380 if (!WARNINGS
[optionName
]) {
381 WARNINGS
[optionName
] = true;
382 var isSeries
= (this.labels_
.indexOf(optionName
) >= 0);
384 console
.warn('Use new-style per-series options (saw ' + optionName
+ ' as top-level options key). See http://bit.ly/1tceaJs');
386 console
.warn('Unknown option ' + optionName
+ ' (full list of options at dygraphs.com/options.html');
387 throw "invalid option " + optionName
;
392 // Reset list of previously-shown warnings. Used for testing.
393 DygraphOptions
.resetWarnings_
= function() {
399 export default DygraphOptions
;