Debian Codegraph @

closure-library/closure/goog/ui/serverchart.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 /**
17 * @fileoverview Component for generating chart PNGs using Google Chart Server.
18 *
19 * @deprecated Google Chart Images service (the server-side component of this
20 * class) has been deprecated. See
21 * https://developers.google.com/chart/ for alternatives.
22 *
23 * @see ../demos/serverchart.html
24 */
25
26
27 /**
28 * Namespace for chart functions
29 */
30 goog.provide('goog.ui.ServerChart');
31 goog.provide('goog.ui.ServerChart.AxisDisplayType');
32 goog.provide('goog.ui.ServerChart.ChartType');
33 goog.provide('goog.ui.ServerChart.EncodingType');
34 goog.provide('goog.ui.ServerChart.Event');
35 goog.provide('goog.ui.ServerChart.LegendPosition');
36 goog.provide('goog.ui.ServerChart.MaximumValue');
37 goog.provide('goog.ui.ServerChart.MultiAxisAlignment');
38 goog.provide('goog.ui.ServerChart.MultiAxisType');
39 goog.provide('goog.ui.ServerChart.UriParam');
40 goog.provide('goog.ui.ServerChart.UriTooLongEvent');
41
42 goog.require('goog.Uri');
43 goog.require('goog.array');
44 goog.require('goog.asserts');
45 goog.require('goog.dom.TagName');
46 goog.require('goog.dom.safe');
47 goog.require('goog.events.Event');
48 goog.require('goog.string');
49 goog.require('goog.ui.Component');
50
51
52
53 /**
54 * Will construct a chart using Google's chartserver.
55 *
56 * @param {goog.ui.ServerChart.ChartType} type The chart type.
57 * @param {number=} opt_width The width of the chart.
58 * @param {number=} opt_height The height of the chart.
59 * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM Helper.
60 * @param {string=} opt_uri Optional uri used to connect to the chart server, if
61 * different than goog.ui.ServerChart.CHART_SERVER_SCHEME_INDEPENDENT_URI.
62 * @constructor
63 * @extends {goog.ui.Component}
64 *
65 * @deprecated Google Chart Server has been deprecated. See
66 * https://developers.google.com/chart/image/ for details.
67 * @final
68 */
69 goog.ui.ServerChart = function(
70 type, opt_width, opt_height, opt_domHelper, opt_uri) {
71 goog.ui.Component.call(this, opt_domHelper);
72
73 /**
74 * Image URI.
75 * @type {goog.Uri}
76 * @private
77 */
78 this.uri_ = new goog.Uri(
79 opt_uri || goog.ui.ServerChart.CHART_SERVER_SCHEME_INDEPENDENT_URI);
80
81 /**
82 * Encoding method for the URI data format.
83 * @type {goog.ui.ServerChart.EncodingType}
84 * @private
85 */
86 this.encodingType_ = goog.ui.ServerChart.EncodingType.AUTOMATIC;
87
88 /**
89 * Two-dimensional array of the data sets on the chart.
90 * @type {Array<Array<number>>}
91 * @private
92 */
93 this.dataSets_ = [];
94
95 /**
96 * Colors for each data set.
97 * @type {Array<string>}
98 * @private
99 */
100 this.setColors_ = [];
101
102 /**
103 * Legend texts for each data set.
104 * @type {Array<string>}
105 * @private
106 */
107 this.setLegendTexts_ = [];
108
109 /**
110 * Labels on the X-axis.
111 * @type {Array<string>}
112 * @private
113 */
114 this.xLabels_ = [];
115
116 /**
117 * Labels on the left along the Y-axis.
118 * @type {Array<string>}
119 * @private
120 */
121 this.leftLabels_ = [];
122
123 /**
124 * Labels on the right along the Y-axis.
125 * @type {Array<string>}
126 * @private
127 */
128 this.rightLabels_ = [];
129
130 /**
131 * Axis type for each multi-axis in the chart. The indices into this array
132 * also work as the reference index for all other multi-axis properties.
133 * @type {Array<goog.ui.ServerChart.MultiAxisType>}
134 * @private
135 */
136 this.multiAxisType_ = [];
137
138 /**
139 * Axis text for each multi-axis in the chart, indexed by the indices from
140 * multiAxisType_ in a sparse array.
141 * @type {Object}
142 * @private
143 */
144 this.multiAxisLabelText_ = {};
145
146
147 /**
148 * Axis position for each multi-axis in the chart, indexed by the indices
149 * from multiAxisType_ in a sparse array.
150 * @type {Object}
151 * @private
152 */
153 this.multiAxisLabelPosition_ = {};
154
155 /**
156 * Axis range for each multi-axis in the chart, indexed by the indices from
157 * multiAxisType_ in a sparse array.
158 * @type {Object}
159 * @private
160 */
161 this.multiAxisRange_ = {};
162
163 /**
164 * Axis style for each multi-axis in the chart, indexed by the indices from
165 * multiAxisType_ in a sparse array.
166 * @type {Object}
167 * @private
168 */
169 this.multiAxisLabelStyle_ = {};
170
171 this.setType(type);
172 this.setSize(opt_width, opt_height);
173
174 /**
175 * Minimum value for the chart (used for normalization). By default,
176 * this is set to infinity, and is eventually updated to the lowest given
177 * value in the data. The minimum value is then subtracted from all other
178 * values. For a pie chart, subtracting the minimum value does not make
179 * sense, so minValue_ is set to zero because 0 is the additive identity.
180 * @type {number}
181 * @private
182 */
183 this.minValue_ = this.isPieChart() ? 0 : Infinity;
184 };
185 goog.inherits(goog.ui.ServerChart, goog.ui.Component);
186
187
188 /**
189 * Base scheme-independent URI for the chart renderer.
190 * @type {string}
191 */
192 goog.ui.ServerChart.CHART_SERVER_SCHEME_INDEPENDENT_URI =
193 '//chart.googleapis.com/chart';
194
195
196 /**
197 * Base HTTP URI for the chart renderer.
198 * @type {string}
199 */
200 goog.ui.ServerChart.CHART_SERVER_HTTP_URI = 'http://chart.googleapis.com/chart';
201
202
203 /**
204 * Base HTTPS URI for the chart renderer.
205 * @type {string}
206 */
207 goog.ui.ServerChart.CHART_SERVER_HTTPS_URI =
208 'https://chart.googleapis.com/chart';
209
210
211 /**
212 * Base URI for the chart renderer.
213 * @type {string}
214 * @deprecated Use
215 * {@link goog.ui.ServerChart.CHART_SERVER_SCHEME_INDEPENDENT_URI},
216 * {@link goog.ui.ServerChart.CHART_SERVER_HTTP_URI} or
217 * {@link goog.ui.ServerChart.CHART_SERVER_HTTPS_URI} instead.
218 */
219 goog.ui.ServerChart.CHART_SERVER_URI =
220 goog.ui.ServerChart.CHART_SERVER_HTTP_URI;
221
222
223 /**
224 * The 0 - 1.0 ("fraction of the range") value to use when getMinValue() ==
225 * getMaxValue(). This determines, for example, the vertical position
226 * of the line in a flat line-chart.
227 * @type {number}
228 */
229 goog.ui.ServerChart.DEFAULT_NORMALIZATION = 0.5;
230
231
232 /**
233 * The upper limit on the length of the chart image URI, after encoding.
234 * If the URI's length equals or exceeds it, goog.ui.ServerChart.UriTooLongEvent
235 * is dispatched on the goog.ui.ServerChart object.
236 * @type {number}
237 * @private
238 */
239 goog.ui.ServerChart.prototype.uriLengthLimit_ = 2048;
240
241
242 /**
243 * Number of gridlines along the X-axis.
244 * @type {number}
245 * @private
246 */
247 goog.ui.ServerChart.prototype.gridX_ = 0;
248
249
250 /**
251 * Number of gridlines along the Y-axis.
252 * @type {number}
253 * @private
254 */
255 goog.ui.ServerChart.prototype.gridY_ = 0;
256
257
258 /**
259 * Maximum value for the chart (used for normalization). The minimum is
260 * declared in the constructor.
261 * @type {number}
262 * @private
263 */
264 goog.ui.ServerChart.prototype.maxValue_ = -Infinity;
265
266
267 /**
268 * Chart title.
269 * @type {?string}
270 * @private
271 */
272 goog.ui.ServerChart.prototype.title_ = null;
273
274
275 /**
276 * Chart title size.
277 * @type {number}
278 * @private
279 */
280 goog.ui.ServerChart.prototype.titleSize_ = 13.5;
281
282
283 /**
284 * Chart title color.
285 * @type {string}
286 * @private
287 */
288 goog.ui.ServerChart.prototype.titleColor_ = '333333';
289
290
291 /**
292 * Chart legend.
293 * @type {Array<string>?}
294 * @private
295 */
296 goog.ui.ServerChart.prototype.legend_ = null;
297
298
299 /**
300 * ChartServer supports using data sets to position markers. A data set
301 * that is being used for positioning only can be made "invisible", in other
302 * words, the caller can indicate to ChartServer that ordinary chart elements
303 * (e.g. bars in a bar chart) should not be drawn on the data points of the
304 * invisible data set. Such data sets must be provided at the end of the
305 * chd parameter, and if invisible data sets are being used, the chd
306 * parameter must indicate the number of visible data sets.
307 * @type {?number}
308 * @private
309 */
310 goog.ui.ServerChart.prototype.numVisibleDataSets_ = null;
311
312
313 /**
314 * Creates the DOM node (image) needed for the Chart
315 * @override
316 */
317 goog.ui.ServerChart.prototype.createDom = function() {
318 var size = this.getSize();
319 this.setElementInternal(this.getDomHelper().createDom(goog.dom.TagName.IMG, {
320 'src': this.getUri(),
321 'class': goog.getCssName('goog-serverchart-image'),
322 'width': size[0],
323 'height': size[1]
324 }));
325 };
326
327
328 /**
329 * Decorate an image already in the DOM.
330 * Expects the following structure:
331 * <pre>
332 * - img
333 * </pre>
334 *
335 * @param {Element} img Image to decorate.
336 * @override
337 */
338 goog.ui.ServerChart.prototype.decorateInternal = function(img) {
339 goog.dom.safe.setImageSrc(
340 /** @type {!HTMLImageElement} */ (img), this.getUri().toString());
341 this.setElementInternal(img);
342 };
343
344
345 /**
346 * Updates the image if any of the data or settings have changed.
347 */
348 goog.ui.ServerChart.prototype.updateChart = function() {
349 if (this.getElement()) {
350 goog.dom.safe.setImageSrc(
351 /** @type {!HTMLImageElement} */ (this.getElement()),
352 this.getUri().toString());
353 }
354 };
355
356
357 /**
358 * Sets the URI of the chart.
359 *
360 * @param {goog.Uri} uri The chart URI.
361 */
362 goog.ui.ServerChart.prototype.setUri = function(uri) {
363 this.uri_ = uri;
364 };
365
366
367 /**
368 * Returns the URI of the chart.
369 *
370 * @return {goog.Uri} The chart URI.
371 */
372 goog.ui.ServerChart.prototype.getUri = function() {
373 this.computeDataString_();
374 return this.uri_;
375 };
376
377
378 /**
379 * Returns the upper limit on the length of the chart image URI, after encoding.
380 * If the URI's length equals or exceeds it, goog.ui.ServerChart.UriTooLongEvent
381 * is dispatched on the goog.ui.ServerChart object.
382 *
383 * @return {number} The chart URI length limit.
384 */
385 goog.ui.ServerChart.prototype.getUriLengthLimit = function() {
386 return this.uriLengthLimit_;
387 };
388
389
390 /**
391 * Sets the upper limit on the length of the chart image URI, after encoding.
392 * If the URI's length equals or exceeds it, goog.ui.ServerChart.UriTooLongEvent
393 * is dispatched on the goog.ui.ServerChart object.
394 *
395 * @param {number} uriLengthLimit The chart URI length limit.
396 */
397 goog.ui.ServerChart.prototype.setUriLengthLimit = function(uriLengthLimit) {
398 this.uriLengthLimit_ = uriLengthLimit;
399 };
400
401
402 /**
403 * Sets the 'chg' parameter of the chart Uri.
404 * This is used by various types of charts to specify Grids.
405 *
406 * @param {string} value Value for the 'chg' parameter in the chart Uri.
407 */
408 goog.ui.ServerChart.prototype.setGridParameter = function(value) {
409 this.uri_.setParameterValue(goog.ui.ServerChart.UriParam.GRID, value);
410 };
411
412
413 /**
414 * Returns the 'chg' parameter of the chart Uri.
415 * This is used by various types of charts to specify Grids.
416 *
417 * @return {string|undefined} The 'chg' parameter of the chart Uri.
418 */
419 goog.ui.ServerChart.prototype.getGridParameter = function() {
420 return /** @type {string} */ (
421 this.uri_.getParameterValue(goog.ui.ServerChart.UriParam.GRID));
422 };
423
424
425 /**
426 * Sets the 'chm' parameter of the chart Uri.
427 * This is used by various types of charts to specify Markers.
428 *
429 * @param {string} value Value for the 'chm' parameter in the chart Uri.
430 */
431 goog.ui.ServerChart.prototype.setMarkerParameter = function(value) {
432 this.uri_.setParameterValue(goog.ui.ServerChart.UriParam.MARKERS, value);
433 };
434
435
436 /**
437 * Returns the 'chm' parameter of the chart Uri.
438 * This is used by various types of charts to specify Markers.
439 *
440 * @return {string|undefined} The 'chm' parameter of the chart Uri.
441 */
442 goog.ui.ServerChart.prototype.getMarkerParameter = function() {
443 return /** @type {string} */ (
444 this.uri_.getParameterValue(goog.ui.ServerChart.UriParam.MARKERS));
445 };
446
447
448 /**
449 * Sets the 'chp' parameter of the chart Uri.
450 * This is used by various types of charts to specify certain options.
451 * e.g., finance charts use this to designate which line is the 0 axis.
452 *
453 * @param {string|number} value Value for the 'chp' parameter in the chart Uri.
454 */
455 goog.ui.ServerChart.prototype.setMiscParameter = function(value) {
456 this.uri_.setParameterValue(
457 goog.ui.ServerChart.UriParam.MISC_PARAMS, String(value));
458 };
459
460
461 /**
462 * Returns the 'chp' parameter of the chart Uri.
463 * This is used by various types of charts to specify certain options.
464 * e.g., finance charts use this to designate which line is the 0 axis.
465 *
466 * @return {string|undefined} The 'chp' parameter of the chart Uri.
467 */
468 goog.ui.ServerChart.prototype.getMiscParameter = function() {
469 return /** @type {string} */ (
470 this.uri_.getParameterValue(goog.ui.ServerChart.UriParam.MISC_PARAMS));
471 };
472
473
474 /**
475 * Enum of chart data encoding types
476 *
477 * @enum {string}
478 */
479 goog.ui.ServerChart.EncodingType = {
480 AUTOMATIC: '',
481 EXTENDED: 'e',
482 SIMPLE: 's',
483 TEXT: 't'
484 };
485
486
487 /**
488 * Enum of chart types with their short names used by the chartserver.
489 *
490 * @enum {string}
491 */
492 goog.ui.ServerChart.ChartType = {
493 BAR: 'br',
494 CLOCK: 'cf',
495 CONCENTRIC_PIE: 'pc',
496 FILLEDLINE: 'lr',
497 FINANCE: 'lfi',
498 GOOGLEOMETER: 'gom',
499 HORIZONTAL_GROUPED_BAR: 'bhg',
500 HORIZONTAL_STACKED_BAR: 'bhs',
501 LINE: 'lc',
502 MAP: 't',
503 MAPUSA: 'tuss',
504 MAPWORLD: 'twoc',
505 PIE: 'p',
506 PIE3D: 'p3',
507 RADAR: 'rs',
508 SCATTER: 's',
509 SPARKLINE: 'ls',
510 VENN: 'v',
511 VERTICAL_GROUPED_BAR: 'bvg',
512 VERTICAL_STACKED_BAR: 'bvs',
513 XYLINE: 'lxy'
514 };
515
516
517 /**
518 * Enum of multi-axis types.
519 *
520 * @enum {string}
521 */
522 goog.ui.ServerChart.MultiAxisType = {
523 X_AXIS: 'x',
524 LEFT_Y_AXIS: 'y',
525 RIGHT_Y_AXIS: 'r',
526 TOP_AXIS: 't'
527 };
528
529
530 /**
531 * Enum of multi-axis alignments.
532 *
533 * @enum {number}
534 */
535 goog.ui.ServerChart.MultiAxisAlignment = {
536 ALIGN_LEFT: -1,
537 ALIGN_CENTER: 0,
538 ALIGN_RIGHT: 1
539 };
540
541
542 /**
543 * Enum of legend positions.
544 *
545 * @enum {string}
546 */
547 goog.ui.ServerChart.LegendPosition = {
548 TOP: 't',
549 BOTTOM: 'b',
550 LEFT: 'l',
551 RIGHT: 'r'
552 };
553
554
555 /**
556 * Enum of line and tick options for an axis.
557 *
558 * @enum {string}
559 */
560 goog.ui.ServerChart.AxisDisplayType = {
561 LINE_AND_TICKS: 'lt',
562 LINE: 'l',
563 TICKS: 't'
564 };
565
566
567 /**
568 * Enum of chart maximum values in pixels, as listed at:
569 * http://code.google.com/apis/chart/basics.html
570 *
571 * @enum {number}
572 */
573 goog.ui.ServerChart.MaximumValue = {
574 WIDTH: 1000,
575 HEIGHT: 1000,
576 MAP_WIDTH: 440,
577 MAP_HEIGHT: 220,
578 TOTAL_AREA: 300000
579 };
580
581
582 /**
583 * Enum of ChartServer URI parameters.
584 *
585 * @enum {string}
586 */
587 goog.ui.ServerChart.UriParam = {
588 BACKGROUND_FILL: 'chf',
589 BAR_HEIGHT: 'chbh',
590 DATA: 'chd',
591 DATA_COLORS: 'chco',
592 DATA_LABELS: 'chld',
593 DATA_SCALING: 'chds',
594 DIGITAL_SIGNATURE: 'sig',
595 GEOGRAPHICAL_REGION: 'chtm',
596 GRID: 'chg',
597 LABEL_COLORS: 'chlc',
598 LEFT_Y_LABELS: 'chly',
599 LEGEND: 'chdl',
600 LEGEND_POSITION: 'chdlp',
601 LEGEND_TEXTS: 'chdl',
602 LINE_STYLES: 'chls',
603 MARGINS: 'chma',
604 MARKERS: 'chm',
605 MISC_PARAMS: 'chp',
606 MULTI_AXIS_LABEL_POSITION: 'chxp',
607 MULTI_AXIS_LABEL_TEXT: 'chxl',
608 MULTI_AXIS_RANGE: 'chxr',
609 MULTI_AXIS_STYLE: 'chxs',
610 MULTI_AXIS_TYPES: 'chxt',
611 RIGHT_LABELS: 'chlr',
612 RIGHT_LABEL_POSITIONS: 'chlrp',
613 SIZE: 'chs',
614 TITLE: 'chtt',
615 TITLE_FORMAT: 'chts',
616 TYPE: 'cht',
617 X_AXIS_STYLE: 'chx',
618 X_LABELS: 'chl'
619 };
620
621
622 /**
623 * Sets the background fill.
624 *
625 * @param {Array<Object>} fill An array of background fill specification
626 * objects. Each object may have the following properties:
627 * {string} area The area to fill, either 'bg' for background or 'c' for
628 * chart area. The default is 'bg'.
629 * {string} color (required) The color of the background fill.
630 * // TODO(user): Add support for gradient/stripes, which requires
631 * // a different object structure.
632 */
633 goog.ui.ServerChart.prototype.setBackgroundFill = function(fill) {
634 var value = [];
635 goog.array.forEach(fill, function(spec) {
636 spec.area = spec.area || 'bg';
637 spec.effect = spec.effect || 's';
638 value.push([spec.area, spec.effect, spec.color].join(','));
639 });
640 value = value.join('|');
641 this.setParameterValue(goog.ui.ServerChart.UriParam.BACKGROUND_FILL, value);
642 };
643
644
645 /**
646 * Returns the background fill.
647 *
648 * @return {!Array<Object>} An array of background fill specifications.
649 * If the fill specification string is in an unsupported format, the method
650 * returns an empty array.
651 */
652 goog.ui.ServerChart.prototype.getBackgroundFill = function() {
653 var value =
654 this.uri_.getParameterValue(goog.ui.ServerChart.UriParam.BACKGROUND_FILL);
655 var result = [];
656 if (goog.isDefAndNotNull(value)) {
657 var fillSpecifications = value.split('|');
658 var valid = true;
659 goog.array.forEach(fillSpecifications, function(spec) {
660 var parts = spec.split(',');
661 if (valid && parts[1] == 's') {
662 result.push({area: parts[0], effect: parts[1], color: parts[2]});
663 } else {
664 // If the format is unsupported, return an empty array.
665 result = [];
666 valid = false;
667 }
668 });
669 }
670 return result;
671 };
672
673
674 /**
675 * Sets the encoding type.
676 *
677 * @param {goog.ui.ServerChart.EncodingType} type Desired data encoding type.
678 */
679 goog.ui.ServerChart.prototype.setEncodingType = function(type) {
680 this.encodingType_ = type;
681 };
682
683
684 /**
685 * Gets the encoding type.
686 *
687 * @return {goog.ui.ServerChart.EncodingType} The encoding type.
688 */
689 goog.ui.ServerChart.prototype.getEncodingType = function() {
690 return this.encodingType_;
691 };
692
693
694 /**
695 * Sets the chart type.
696 *
697 * @param {goog.ui.ServerChart.ChartType} type The desired chart type.
698 */
699 goog.ui.ServerChart.prototype.setType = function(type) {
700 this.uri_.setParameterValue(goog.ui.ServerChart.UriParam.TYPE, type);
701 };
702
703
704 /**
705 * Returns the chart type.
706 *
707 * @return {goog.ui.ServerChart.ChartType} The chart type.
708 */
709 goog.ui.ServerChart.prototype.getType = function() {
710 return /** @type {goog.ui.ServerChart.ChartType} */ (
711 this.uri_.getParameterValue(goog.ui.ServerChart.UriParam.TYPE));
712 };
713
714
715 /**
716 * Sets the chart size.
717 *
718 * @param {number=} opt_width Optional chart width, defaults to 300.
719 * @param {number=} opt_height Optional chart height, defaults to 150.
720 */
721 goog.ui.ServerChart.prototype.setSize = function(opt_width, opt_height) {
722 var sizeString = [opt_width || 300, opt_height || 150].join('x');
723 this.uri_.setParameterValue(goog.ui.ServerChart.UriParam.SIZE, sizeString);
724 };
725
726
727 /**
728 * Returns the chart size.
729 *
730 * @return {!Array<string>} [Width, Height].
731 */
732 goog.ui.ServerChart.prototype.getSize = function() {
733 var sizeStr = this.uri_.getParameterValue(goog.ui.ServerChart.UriParam.SIZE);
734 return sizeStr.split('x');
735 };
736
737
738 /**
739 * Sets the minimum value of the chart.
740 *
741 * @param {number} minValue The minimum value of the chart.
742 */
743 goog.ui.ServerChart.prototype.setMinValue = function(minValue) {
744 this.minValue_ = minValue;
745 };
746
747
748 /**
749 * @return {number} The minimum value of the chart.
750 */
751 goog.ui.ServerChart.prototype.getMinValue = function() {
752 return this.minValue_;
753 };
754
755
756 /**
757 * Sets the maximum value of the chart.
758 *
759 * @param {number} maxValue The maximum value of the chart.
760 */
761 goog.ui.ServerChart.prototype.setMaxValue = function(maxValue) {
762 this.maxValue_ = maxValue;
763 };
764
765
766 /**
767 * @return {number} The maximum value of the chart.
768 */
769 goog.ui.ServerChart.prototype.getMaxValue = function() {
770 return this.maxValue_;
771 };
772
773
774 /**
775 * Sets the chart margins.
776 *
777 * @param {number} leftMargin The size in pixels of the left margin.
778 * @param {number} rightMargin The size in pixels of the right margin.
779 * @param {number} topMargin The size in pixels of the top margin.
780 * @param {number} bottomMargin The size in pixels of the bottom margin.
781 */
782 goog.ui.ServerChart.prototype.setMargins = function(
783 leftMargin, rightMargin, topMargin, bottomMargin) {
784 var margins = [leftMargin, rightMargin, topMargin, bottomMargin].join(',');
785 var UriParam = goog.ui.ServerChart.UriParam;
786 this.uri_.setParameterValue(UriParam.MARGINS, margins);
787 };
788
789
790 /**
791 * Sets the number of grid lines along the X-axis.
792 *
793 * @param {number} gridlines The number of X-axis grid lines.
794 */
795 goog.ui.ServerChart.prototype.setGridX = function(gridlines) {
796 // Need data for this to work.
797 this.gridX_ = gridlines;
798 this.setGrids_(this.gridX_, this.gridY_);
799 };
800
801
802 /**
803 * @return {number} The number of gridlines along the X-axis.
804 */
805 goog.ui.ServerChart.prototype.getGridX = function() {
806 return this.gridX_;
807 };
808
809
810 /**
811 * Sets the number of grid lines along the Y-axis.
812 *
813 * @param {number} gridlines The number of Y-axis grid lines.
814 */
815 goog.ui.ServerChart.prototype.setGridY = function(gridlines) {
816 // Need data for this to work.
817 this.gridY_ = gridlines;
818 this.setGrids_(this.gridX_, this.gridY_);
819 };
820
821
822 /**
823 * @return {number} The number of gridlines along the Y-axis.
824 */
825 goog.ui.ServerChart.prototype.getGridY = function() {
826 return this.gridY_;
827 };
828
829
830 /**
831 * Sets the grids for the chart
832 *
833 * @private
834 * @param {number} x The number of grid lines along the x-axis.
835 * @param {number} y The number of grid lines along the y-axis.
836 */
837 goog.ui.ServerChart.prototype.setGrids_ = function(x, y) {
838 var gridArray = [x == 0 ? 0 : 100 / x, y == 0 ? 0 : 100 / y];
839 this.uri_.setParameterValue(
840 goog.ui.ServerChart.UriParam.GRID, gridArray.join(','));
841 };
842
843
844 /**
845 * Sets the X Labels for the chart.
846 *
847 * @param {Array<string>} labels The X Labels for the chart.
848 */
849 goog.ui.ServerChart.prototype.setXLabels = function(labels) {
850 this.xLabels_ = labels;
851 this.uri_.setParameterValue(
852 goog.ui.ServerChart.UriParam.X_LABELS, this.xLabels_.join('|'));
853 };
854
855
856 /**
857 * @return {Array<string>} The X Labels for the chart.
858 */
859 goog.ui.ServerChart.prototype.getXLabels = function() {
860 return this.xLabels_;
861 };
862
863
864 /**
865 * @return {boolean} Whether the chart is a bar chart.
866 */
867 goog.ui.ServerChart.prototype.isBarChart = function() {
868 var type = this.getType();
869 return type == goog.ui.ServerChart.ChartType.BAR ||
870 type == goog.ui.ServerChart.ChartType.HORIZONTAL_GROUPED_BAR ||
871 type == goog.ui.ServerChart.ChartType.HORIZONTAL_STACKED_BAR ||
872 type == goog.ui.ServerChart.ChartType.VERTICAL_GROUPED_BAR ||
873 type == goog.ui.ServerChart.ChartType.VERTICAL_STACKED_BAR;
874 };
875
876
877 /**
878 * @return {boolean} Whether the chart is a pie chart.
879 */
880 goog.ui.ServerChart.prototype.isPieChart = function() {
881 var type = this.getType();
882 return type == goog.ui.ServerChart.ChartType.PIE ||
883 type == goog.ui.ServerChart.ChartType.PIE3D ||
884 type == goog.ui.ServerChart.ChartType.CONCENTRIC_PIE;
885 };
886
887
888 /**
889 * @return {boolean} Whether the chart is a grouped bar chart.
890 */
891 goog.ui.ServerChart.prototype.isGroupedBarChart = function() {
892 var type = this.getType();
893 return type == goog.ui.ServerChart.ChartType.HORIZONTAL_GROUPED_BAR ||
894 type == goog.ui.ServerChart.ChartType.VERTICAL_GROUPED_BAR;
895 };
896
897
898 /**
899 * @return {boolean} Whether the chart is a horizontal bar chart.
900 */
901 goog.ui.ServerChart.prototype.isHorizontalBarChart = function() {
902 var type = this.getType();
903 return type == goog.ui.ServerChart.ChartType.BAR ||
904 type == goog.ui.ServerChart.ChartType.HORIZONTAL_GROUPED_BAR ||
905 type == goog.ui.ServerChart.ChartType.HORIZONTAL_STACKED_BAR;
906 };
907
908
909 /**
910 * @return {boolean} Whether the chart is a line chart.
911 */
912 goog.ui.ServerChart.prototype.isLineChart = function() {
913 var type = this.getType();
914 return type == goog.ui.ServerChart.ChartType.FILLEDLINE ||
915 type == goog.ui.ServerChart.ChartType.LINE ||
916 type == goog.ui.ServerChart.ChartType.SPARKLINE ||
917 type == goog.ui.ServerChart.ChartType.XYLINE;
918 };
919
920
921 /**
922 * @return {boolean} Whether the chart is a map.
923 */
924 goog.ui.ServerChart.prototype.isMap = function() {
925 var type = this.getType();
926 return type == goog.ui.ServerChart.ChartType.MAP ||
927 type == goog.ui.ServerChart.ChartType.MAPUSA ||
928 type == goog.ui.ServerChart.ChartType.MAPWORLD;
929 };
930
931
932 /**
933 * @return {boolean} Whether the chart is a stacked bar chart.
934 */
935 goog.ui.ServerChart.prototype.isStackedBarChart = function() {
936 var type = this.getType();
937 return type == goog.ui.ServerChart.ChartType.BAR ||
938 type == goog.ui.ServerChart.ChartType.HORIZONTAL_STACKED_BAR ||
939 type == goog.ui.ServerChart.ChartType.VERTICAL_STACKED_BAR;
940 };
941
942
943 /**
944 * @return {boolean} Whether the chart is a vertical bar chart.
945 */
946 goog.ui.ServerChart.prototype.isVerticalBarChart = function() {
947 var type = this.getType();
948 return type == goog.ui.ServerChart.ChartType.VERTICAL_GROUPED_BAR ||
949 type == goog.ui.ServerChart.ChartType.VERTICAL_STACKED_BAR;
950 };
951
952
953 /**
954 * Sets the Left Labels for the chart.
955 * NOTE: The array should start with the lowest value, and then
956 * move progessively up the axis. So if you want labels
957 * from 0 to 100 with 0 at bottom of the graph, then you would
958 * want to pass something like [0,25,50,75,100].
959 *
960 * @param {Array<string>} labels The Left Labels for the chart.
961 */
962 goog.ui.ServerChart.prototype.setLeftLabels = function(labels) {
963 this.leftLabels_ = labels;
964 this.uri_.setParameterValue(
965 goog.ui.ServerChart.UriParam.LEFT_Y_LABELS,
966 this.leftLabels_.reverse().join('|'));
967 };
968
969
970 /**
971 * @return {Array<string>} The Left Labels for the chart.
972 */
973 goog.ui.ServerChart.prototype.getLeftLabels = function() {
974 return this.leftLabels_;
975 };
976
977
978 /**
979 * Sets the given ChartServer parameter.
980 *
981 * @param {goog.ui.ServerChart.UriParam} key The ChartServer parameter to set.
982 * @param {string} value The value to set for the ChartServer parameter.
983 */
984 goog.ui.ServerChart.prototype.setParameterValue = function(key, value) {
985 this.uri_.setParameterValue(key, value);
986 };
987
988
989 /**
990 * Removes the given ChartServer parameter.
991 *
992 * @param {goog.ui.ServerChart.UriParam} key The ChartServer parameter to
993 * remove.
994 */
995 goog.ui.ServerChart.prototype.removeParameter = function(key) {
996 this.uri_.removeParameter(key);
997 };
998
999
1000 /**
1001 * Sets the Right Labels for the chart.
1002 * NOTE: The array should start with the lowest value, and then
1003 * move progessively up the axis. So if you want labels
1004 * from 0 to 100 with 0 at bottom of the graph, then you would
1005 * want to pass something like [0,25,50,75,100].
1006 *
1007 * @param {Array<string>} labels The Right Labels for the chart.
1008 */
1009 goog.ui.ServerChart.prototype.setRightLabels = function(labels) {
1010 this.rightLabels_ = labels;
1011 this.uri_.setParameterValue(
1012 goog.ui.ServerChart.UriParam.RIGHT_LABELS,
1013 this.rightLabels_.reverse().join('|'));
1014 };
1015
1016
1017 /**
1018 * @return {Array<string>} The Right Labels for the chart.
1019 */
1020 goog.ui.ServerChart.prototype.getRightLabels = function() {
1021 return this.rightLabels_;
1022 };
1023
1024
1025 /**
1026 * Sets the position relative to the chart where the legend is to be displayed.
1027 *
1028 * @param {goog.ui.ServerChart.LegendPosition} value Legend position.
1029 */
1030 goog.ui.ServerChart.prototype.setLegendPosition = function(value) {
1031 this.uri_.setParameterValue(
1032 goog.ui.ServerChart.UriParam.LEGEND_POSITION, value);
1033 };
1034
1035
1036 /**
1037 * Returns the position relative to the chart where the legend is to be
1038 * displayed.
1039 *
1040 * @return {goog.ui.ServerChart.LegendPosition} Legend position.
1041 */
1042 goog.ui.ServerChart.prototype.getLegendPosition = function() {
1043 return /** @type {goog.ui.ServerChart.LegendPosition} */ (
1044 this.uri_.getParameterValue(
1045 goog.ui.ServerChart.UriParam.LEGEND_POSITION));
1046 };
1047
1048
1049 /**
1050 * Sets the number of "visible" data sets. All data sets that come after
1051 * the visible data set are not drawn as part of the chart. Instead, they
1052 * are available for positioning markers.
1053
1054 * @param {?number} n The number of visible data sets, or null if all data
1055 * sets are to be visible.
1056 */
1057 goog.ui.ServerChart.prototype.setNumVisibleDataSets = function(n) {
1058 this.numVisibleDataSets_ = n;
1059 };
1060
1061
1062 /**
1063 * Returns the number of "visible" data sets. All data sets that come after
1064 * the visible data set are not drawn as part of the chart. Instead, they
1065 * are available for positioning markers.
1066 *
1067 * @return {?number} The number of visible data sets, or null if all data
1068 * sets are visible.
1069 */
1070 goog.ui.ServerChart.prototype.getNumVisibleDataSets = function() {
1071 return this.numVisibleDataSets_;
1072 };
1073
1074
1075 /**
1076 * Sets the weight function for a Venn Diagram along with the associated
1077 * colors and legend text. Weights are assigned as follows:
1078 * weights[0] is relative area of circle A.
1079 * weights[1] is relative area of circle B.
1080 * weights[2] is relative area of circle C.
1081 * weights[3] is relative area of overlap of circles A and B.
1082 * weights[4] is relative area of overlap of circles A and C.
1083 * weights[5] is relative area of overlap of circles B and C.
1084 * weights[6] is relative area of overlap of circles A, B and C.
1085 * For a two circle Venn Diagram the weights are assigned as follows:
1086 * weights[0] is relative area of circle A.
1087 * weights[1] is relative area of circle B.
1088 * weights[2] is relative area of overlap of circles A and B.
1089 *
1090 * @param {Array<number>} weights The relative weights of the circles.
1091 * @param {Array<string>=} opt_legendText The legend labels for the circles.
1092 * @param {Array<string>=} opt_colors The colors for the circles.
1093 */
1094 goog.ui.ServerChart.prototype.setVennSeries = function(
1095 weights, opt_legendText, opt_colors) {
1096 if (this.getType() != goog.ui.ServerChart.ChartType.VENN) {
1097 throw new Error('Can only set a weight function for a Venn diagram.');
1098 }
1099 var dataMin = this.arrayMin_(weights);
1100 if (dataMin < this.minValue_) {
1101 this.minValue_ = dataMin;
1102 }
1103 var dataMax = this.arrayMax_(weights);
1104 if (dataMax > this.maxValue_) {
1105 this.maxValue_ = dataMax;
1106 }
1107 if (goog.isDef(opt_legendText)) {
1108 goog.array.forEach(opt_legendText, goog.bind(function(legend) {
1109 this.setLegendTexts_.push(legend);
1110 }, this));
1111 this.uri_.setParameterValue(
1112 goog.ui.ServerChart.UriParam.LEGEND_TEXTS,
1113 this.setLegendTexts_.join('|'));
1114 }
1115 // If the caller only gave three weights, then they wanted a two circle
1116 // Venn Diagram. Create a 3 circle weight function where circle C has
1117 // area zero.
1118 if (weights.length == 3) {
1119 weights[3] = weights[2];
1120 weights[2] = 0.0;
1121 }
1122 this.dataSets_.push(weights);
1123 if (goog.isDef(opt_colors)) {
1124 goog.array.forEach(opt_colors, goog.bind(function(color) {
1125 this.setColors_.push(color);
1126 }, this));
1127 this.uri_.setParameterValue(
1128 goog.ui.ServerChart.UriParam.DATA_COLORS, this.setColors_.join(','));
1129 }
1130 };
1131
1132
1133 /**
1134 * Sets the title of the chart.
1135 *
1136 * @param {string} title The chart title.
1137 */
1138 goog.ui.ServerChart.prototype.setTitle = function(title) {
1139 this.title_ = title;
1140 this.uri_.setParameterValue(
1141 goog.ui.ServerChart.UriParam.TITLE, this.title_.replace(/\n/g, '|'));
1142 };
1143
1144
1145 /**
1146 * Sets the size of the chart title.
1147 *
1148 * @param {number} size The title size, in points.
1149 */
1150 goog.ui.ServerChart.prototype.setTitleSize = function(size) {
1151 this.titleSize_ = size;
1152 this.uri_.setParameterValue(
1153 goog.ui.ServerChart.UriParam.TITLE_FORMAT,
1154 this.titleColor_ + ',' + this.titleSize_);
1155 };
1156
1157
1158 /**
1159 * @return {number} size The title size, in points.
1160 */
1161 goog.ui.ServerChart.prototype.getTitleSize = function() {
1162 return this.titleSize_;
1163 };
1164
1165
1166 /**
1167 * Sets the color of the chart title.
1168 *
1169 * NOTE: The color string should NOT have a '#' at the beginning of it.
1170 *
1171 * @param {string} color The hex value for the title color.
1172 */
1173 goog.ui.ServerChart.prototype.setTitleColor = function(color) {
1174 this.titleColor_ = color;
1175 this.uri_.setParameterValue(
1176 goog.ui.ServerChart.UriParam.TITLE_FORMAT,
1177 this.titleColor_ + ',' + this.titleSize_);
1178 };
1179
1180
1181 /**
1182 * @return {string} color The hex value for the title color.
1183 */
1184 goog.ui.ServerChart.prototype.getTitleColor = function() {
1185 return this.titleColor_;
1186 };
1187
1188
1189 /**
1190 * Adds a legend to the chart.
1191 *
1192 * @param {Array<string>} legend The legend to add.
1193 */
1194 goog.ui.ServerChart.prototype.setLegend = function(legend) {
1195 this.legend_ = legend;
1196 this.uri_.setParameterValue(
1197 goog.ui.ServerChart.UriParam.LEGEND, this.legend_.join('|'));
1198 };
1199
1200
1201 /**
1202 * Sets the data scaling.
1203 * NOTE: This also changes the encoding type because data scaling will
1204 * only work with {@code goog.ui.ServerChart.EncodingType.TEXT}
1205 * encoding.
1206 * @param {number} minimum The lowest number to apply to the data.
1207 * @param {number} maximum The highest number to apply to the data.
1208 */
1209 goog.ui.ServerChart.prototype.setDataScaling = function(minimum, maximum) {
1210 this.encodingType_ = goog.ui.ServerChart.EncodingType.TEXT;
1211 this.uri_.setParameterValue(
1212 goog.ui.ServerChart.UriParam.DATA_SCALING, minimum + ',' + maximum);
1213 };
1214
1215
1216 /**
1217 * Sets the widths of the bars and the spaces between the bars in a bar
1218 * chart.
1219 * NOTE: If the space between groups is specified but the space between
1220 * bars is left undefined, the space between groups will be interpreted
1221 * as the space between bars because this is the behavior exposed
1222 * in the external developers guide.
1223 * @param {number} barWidth The width of a bar in pixels.
1224 * @param {number=} opt_spaceBars The width of the space between
1225 * bars in a group in pixels.
1226 * @param {number=} opt_spaceGroups The width of the space between
1227 * groups.
1228 */
1229 goog.ui.ServerChart.prototype.setBarSpaceWidths = function(
1230 barWidth, opt_spaceBars, opt_spaceGroups) {
1231 var widths = [barWidth];
1232 if (goog.isDef(opt_spaceBars)) {
1233 widths.push(opt_spaceBars);
1234 }
1235 if (goog.isDef(opt_spaceGroups)) {
1236 widths.push(opt_spaceGroups);
1237 }
1238 this.uri_.setParameterValue(
1239 goog.ui.ServerChart.UriParam.BAR_HEIGHT, widths.join(','));
1240 };
1241
1242
1243 /**
1244 * Specifies that the bar width in a bar chart should be calculated
1245 * automatically given the space available in the chart, while optionally
1246 * setting the spaces between the bars.
1247 * NOTE: If the space between groups is specified but the space between
1248 * bars is left undefined, the space between groups will be interpreted
1249 * as the space between bars because this is the behavior exposed
1250 * in the external developers guide.
1251 * @param {number=} opt_spaceBars The width of the space between
1252 * bars in a group in pixels.
1253 * @param {number=} opt_spaceGroups The width of the space between
1254 * groups.
1255 */
1256 goog.ui.ServerChart.prototype.setAutomaticBarWidth = function(
1257 opt_spaceBars, opt_spaceGroups) {
1258 var widths = ['a'];
1259 if (goog.isDef(opt_spaceBars)) {
1260 widths.push(opt_spaceBars);
1261 }
1262 if (goog.isDef(opt_spaceGroups)) {
1263 widths.push(opt_spaceGroups);
1264 }
1265 this.uri_.setParameterValue(
1266 goog.ui.ServerChart.UriParam.BAR_HEIGHT, widths.join(','));
1267 };
1268
1269
1270 /**
1271 * Adds a multi-axis to the chart, and sets its type. Multiple axes of the same
1272 * type can be added.
1273 *
1274 * @param {goog.ui.ServerChart.MultiAxisType} axisType The desired axis type.
1275 * @return {number} The index of the newly inserted axis, suitable for feeding
1276 * to the setMultiAxis*() functions.
1277 */
1278 goog.ui.ServerChart.prototype.addMultiAxis = function(axisType) {
1279 this.multiAxisType_.push(axisType);
1280 this.uri_.setParameterValue(
1281 goog.ui.ServerChart.UriParam.MULTI_AXIS_TYPES,
1282 this.multiAxisType_.join(','));
1283 return this.multiAxisType_.length - 1;
1284 };
1285
1286
1287 /**
1288 * Returns the axis type for the given axis, or all of them in an array if the
1289 * axis number is not given.
1290 *
1291 * @param {number=} opt_axisNumber The axis index, as returned by addMultiAxis.
1292 * @return {goog.ui.ServerChart.MultiAxisType|
1293 * Array<goog.ui.ServerChart.MultiAxisType>}
1294 * The axis type for the given axis, or all of them in an array if the
1295 * axis number is not given.
1296 */
1297 goog.ui.ServerChart.prototype.getMultiAxisType = function(opt_axisNumber) {
1298 if (goog.isDef(opt_axisNumber)) {
1299 return this.multiAxisType_[opt_axisNumber];
1300 }
1301 return this.multiAxisType_;
1302 };
1303
1304
1305 /**
1306 * Sets the label text (usually multiple values) for a given axis, overwriting
1307 * any existing values.
1308 *
1309 * @param {number} axisNumber The axis index, as returned by addMultiAxis.
1310 * @param {Array<string>} labelText The actual label text to be added.
1311 */
1312 goog.ui.ServerChart.prototype.setMultiAxisLabelText = function(
1313 axisNumber, labelText) {
1314 this.multiAxisLabelText_[axisNumber] = labelText;
1315
1316 var axisString = this.computeMultiAxisDataString_(
1317 this.multiAxisLabelText_, ':|', '|', '|');
1318 this.uri_.setParameterValue(
1319 goog.ui.ServerChart.UriParam.MULTI_AXIS_LABEL_TEXT, axisString);
1320 };
1321
1322
1323 /**
1324 * Returns the label text, or all of them in a two-dimensional array if the
1325 * axis number is not given.
1326 *
1327 * @param {number=} opt_axisNumber The axis index, as returned by addMultiAxis.
1328 * @return {Object|Array<string>} The label text, or all of them in a
1329 * two-dimensional array if the axis number is not given.
1330 */
1331 goog.ui.ServerChart.prototype.getMultiAxisLabelText = function(opt_axisNumber) {
1332 if (goog.isDef(opt_axisNumber)) {
1333 return this.multiAxisLabelText_[opt_axisNumber];
1334 }
1335 return this.multiAxisLabelText_;
1336 };
1337
1338
1339 /**
1340 * Sets the label positions for a given axis, overwriting any existing values.
1341 * The label positions are assumed to be floating-point numbers within the
1342 * range of the axis.
1343 *
1344 * @param {number} axisNumber The axis index, as returned by addMultiAxis.
1345 * @param {Array<number>} labelPosition The actual label positions to be added.
1346 */
1347 goog.ui.ServerChart.prototype.setMultiAxisLabelPosition = function(
1348 axisNumber, labelPosition) {
1349 this.multiAxisLabelPosition_[axisNumber] = labelPosition;
1350
1351 var positionString = this.computeMultiAxisDataString_(
1352 this.multiAxisLabelPosition_, ',', ',', '|');
1353 this.uri_.setParameterValue(
1354 goog.ui.ServerChart.UriParam.MULTI_AXIS_LABEL_POSITION, positionString);
1355 };
1356
1357
1358 /**
1359 * Returns the label positions for a given axis number, or all of them in a
1360 * two-dimensional array if the axis number is not given.
1361 *
1362 * @param {number=} opt_axisNumber The axis index, as returned by addMultiAxis.
1363 * @return {Object|Array<number>} The label positions for a given axis number,
1364 * or all of them in a two-dimensional array if the axis number is not
1365 * given.
1366 */
1367 goog.ui.ServerChart.prototype.getMultiAxisLabelPosition = function(
1368 opt_axisNumber) {
1369 if (goog.isDef(opt_axisNumber)) {
1370 return this.multiAxisLabelPosition_[opt_axisNumber];
1371 }
1372 return this.multiAxisLabelPosition_;
1373 };
1374
1375
1376 /**
1377 * Sets the label range for a given axis, overwriting any existing range.
1378 * The default range is from 0 to 100. If the start value is larger than the
1379 * end value, the axis direction is reversed. rangeStart and rangeEnd must
1380 * be two different finite numbers.
1381 *
1382 * @param {number} axisNumber The axis index, as returned by addMultiAxis.
1383 * @param {number} rangeStart The new start of the range.
1384 * @param {number} rangeEnd The new end of the range.
1385 * @param {number=} opt_interval The interval between axis labels.
1386 */
1387 goog.ui.ServerChart.prototype.setMultiAxisRange = function(
1388 axisNumber, rangeStart, rangeEnd, opt_interval) {
1389 goog.asserts.assert(
1390 rangeStart != rangeEnd, 'Range start and end cannot be the same value.');
1391 goog.asserts.assert(
1392 isFinite(rangeStart) && isFinite(rangeEnd),
1393 'Range start and end must be finite numbers.');
1394 this.multiAxisRange_[axisNumber] = [rangeStart, rangeEnd];
1395 if (goog.isDef(opt_interval)) {
1396 this.multiAxisRange_[axisNumber].push(opt_interval);
1397 }
1398 var rangeString =
1399 this.computeMultiAxisDataString_(this.multiAxisRange_, ',', ',', '|');
1400 this.uri_.setParameterValue(
1401 goog.ui.ServerChart.UriParam.MULTI_AXIS_RANGE, rangeString);
1402 };
1403
1404
1405 /**
1406 * Returns the label range for a given axis number as a two-element array of
1407 * (range start, range end), or all of them in a two-dimensional array if the
1408 * axis number is not given.
1409 *
1410 * @param {number=} opt_axisNumber The axis index, as returned by addMultiAxis.
1411 * @return {Object|Array<number>} The label range for a given axis number as a
1412 * two-element array of (range start, range end), or all of them in a
1413 * two-dimensional array if the axis number is not given.
1414 */
1415 goog.ui.ServerChart.prototype.getMultiAxisRange = function(opt_axisNumber) {
1416 if (goog.isDef(opt_axisNumber)) {
1417 return this.multiAxisRange_[opt_axisNumber];
1418 }
1419 return this.multiAxisRange_;
1420 };
1421
1422
1423 /**
1424 * Sets the label style for a given axis, overwriting any existing style.
1425 * The default style is as follows: Default is x-axis labels are centered, left
1426 * hand y-axis labels are right aligned, right hand y-axis labels are left
1427 * aligned. The font size and alignment are optional parameters.
1428 *
1429 * NOTE: The color string should NOT have a '#' at the beginning of it.
1430 *
1431 * @param {number} axisNumber The axis index, as returned by addMultiAxis.
1432 * @param {string} color The hex value for this label's color.
1433 * @param {number=} opt_fontSize The label font size, in pixels.
1434 * @param {goog.ui.ServerChart.MultiAxisAlignment=} opt_alignment The label
1435 * alignment.
1436 * @param {goog.ui.ServerChart.AxisDisplayType=} opt_axisDisplay The axis
1437 * line and ticks.
1438 */
1439 goog.ui.ServerChart.prototype.setMultiAxisLabelStyle = function(
1440 axisNumber, color, opt_fontSize, opt_alignment, opt_axisDisplay) {
1441 var style = [color];
1442 if (goog.isDef(opt_fontSize) || goog.isDef(opt_alignment)) {
1443 style.push(opt_fontSize || '');
1444 }
1445 if (goog.isDef(opt_alignment)) {
1446 style.push(opt_alignment);
1447 }
1448 if (opt_axisDisplay) {
1449 style.push(opt_axisDisplay);
1450 }
1451 this.multiAxisLabelStyle_[axisNumber] = style;
1452 var styleString = this.computeMultiAxisDataString_(
1453 this.multiAxisLabelStyle_, ',', ',', '|');
1454 this.uri_.setParameterValue(
1455 goog.ui.ServerChart.UriParam.MULTI_AXIS_STYLE, styleString);
1456 };
1457
1458
1459 /**
1460 * Returns the label style for a given axis number as a one- to three-element
1461 * array, or all of them in a two-dimensional array if the axis number is not
1462 * given.
1463 *
1464 * @param {number=} opt_axisNumber The axis index, as returned by addMultiAxis.
1465 * @return {Object|Array<number>} The label style for a given axis number as a
1466 * one- to three-element array, or all of them in a two-dimensional array if
1467 * the axis number is not given.
1468 */
1469 goog.ui.ServerChart.prototype.getMultiAxisLabelStyle = function(
1470 opt_axisNumber) {
1471 if (goog.isDef(opt_axisNumber)) {
1472 return this.multiAxisLabelStyle_[opt_axisNumber];
1473 }
1474 return this.multiAxisLabelStyle_;
1475 };
1476
1477
1478 /**
1479 * Adds a data set.
1480 * NOTE: The color string should NOT have a '#' at the beginning of it.
1481 *
1482 * @param {Array<?number>} data An array of numbers (values can be
1483 * NaN or null).
1484 * @param {string} color The hex value for this data set's color.
1485 * @param {string=} opt_legendText The legend text, if any, for this data
1486 * series. NOTE: If specified, all previously added data sets must also
1487 * have a legend text.
1488 */
1489 goog.ui.ServerChart.prototype.addDataSet = function(
1490 data, color, opt_legendText) {
1491 var dataMin = this.arrayMin_(data);
1492 if (dataMin < this.minValue_) {
1493 this.minValue_ = dataMin;
1494 }
1495
1496 var dataMax = this.arrayMax_(data);
1497 if (dataMax > this.maxValue_) {
1498 this.maxValue_ = dataMax;
1499 }
1500
1501 if (goog.isDef(opt_legendText)) {
1502 if (this.setLegendTexts_.length < this.dataSets_.length) {
1503 throw new Error('Cannot start adding legends text after first element.');
1504 }
1505 this.setLegendTexts_.push(opt_legendText);
1506 this.uri_.setParameterValue(
1507 goog.ui.ServerChart.UriParam.LEGEND_TEXTS,
1508 this.setLegendTexts_.join('|'));
1509 }
1510
1511 this.dataSets_.push(data);
1512 this.setColors_.push(color);
1513
1514 this.uri_.setParameterValue(
1515 goog.ui.ServerChart.UriParam.DATA_COLORS, this.setColors_.join(','));
1516 };
1517
1518
1519 /**
1520 * Clears the data sets from the graph. All data, including the colors and
1521 * legend text, is cleared.
1522 */
1523 goog.ui.ServerChart.prototype.clearDataSets = function() {
1524 var queryData = this.uri_.getQueryData();
1525 queryData.remove(goog.ui.ServerChart.UriParam.LEGEND_TEXTS);
1526 queryData.remove(goog.ui.ServerChart.UriParam.DATA_COLORS);
1527 queryData.remove(goog.ui.ServerChart.UriParam.DATA);
1528 this.setLegendTexts_.length = 0;
1529 this.setColors_.length = 0;
1530 this.dataSets_.length = 0;
1531 };
1532
1533
1534 /**
1535 * Returns the given data set or all of them in a two-dimensional array if
1536 * the set number is not given.
1537 *
1538 * @param {number=} opt_setNumber Optional data set number to get.
1539 * @return {Array<?>} The given data set or all of them in a two-dimensional
1540 * array if the set number is not given.
1541 */
1542 goog.ui.ServerChart.prototype.getData = function(opt_setNumber) {
1543 if (goog.isDef(opt_setNumber)) {
1544 return this.dataSets_[opt_setNumber];
1545 }
1546 return this.dataSets_;
1547 };
1548
1549
1550 /**
1551 * Computes the data string using the data in this.dataSets_ and sets
1552 * the object's URI accordingly. If the URI's length equals or exceeds the
1553 * limit, goog.ui.ServerChart.UriTooLongEvent is dispatched on the
1554 * goog.ui.ServerChart object.
1555 * @private
1556 */
1557 goog.ui.ServerChart.prototype.computeDataString_ = function() {
1558 var ok;
1559 if (this.encodingType_ != goog.ui.ServerChart.EncodingType.AUTOMATIC) {
1560 ok = this.computeDataStringForEncoding_(this.encodingType_);
1561 } else {
1562 ok = this.computeDataStringForEncoding_(
1563 goog.ui.ServerChart.EncodingType.EXTENDED);
1564 if (!ok) {
1565 ok = this.computeDataStringForEncoding_(
1566 goog.ui.ServerChart.EncodingType.SIMPLE);
1567 }
1568 }
1569 if (!ok) {
1570 this.dispatchEvent(
1571 new goog.ui.ServerChart.UriTooLongEvent(this.uri_.toString()));
1572 }
1573 };
1574
1575
1576 /**
1577 * Computes the data string using the data in this.dataSets_ and the encoding
1578 * specified by the encoding parameter, which must not be AUTOMATIC, and sets
1579 * the object's URI accordingly.
1580 * @param {goog.ui.ServerChart.EncodingType} encoding The data encoding to use;
1581 * must not be AUTOMATIC.
1582 * @return {boolean} False if the resulting URI is too long.
1583 * @private
1584 */
1585 goog.ui.ServerChart.prototype.computeDataStringForEncoding_ = function(
1586 encoding) {
1587 var dataStrings = [];
1588 for (var i = 0, setLen = this.dataSets_.length; i < setLen; ++i) {
1589 dataStrings[i] = this.getChartServerValues_(
1590 this.dataSets_[i], this.minValue_, this.maxValue_, encoding);
1591 }
1592 var delimiter = encoding == goog.ui.ServerChart.EncodingType.TEXT ? '|' : ',';
1593 dataStrings = dataStrings.join(delimiter);
1594 var data;
1595 if (this.numVisibleDataSets_ == null) {
1596 data = goog.string.buildString(encoding, ':', dataStrings);
1597 } else {
1598 data = goog.string.buildString(
1599 encoding, this.numVisibleDataSets_, ':', dataStrings);
1600 }
1601 this.uri_.setParameterValue(goog.ui.ServerChart.UriParam.DATA, data);
1602 return this.uri_.toString().length < this.uriLengthLimit_;
1603 };
1604
1605
1606 /**
1607 * Computes a multi-axis data string from the given data and separators. The
1608 * general data format for each index/element in the array will be
1609 * "<arrayIndex><indexSeparator><arrayElement.join(elementSeparator)>", with
1610 * axisSeparator used between multiple elements.
1611 * @param {Object} data The data to compute the data string for, as a
1612 * sparse array of arrays. NOTE: The function uses the length of
1613 * multiAxisType_ to determine the upper bound for the outer array.
1614 * @param {string} indexSeparator The separator string inserted between each
1615 * index and the data itself, commonly a comma (,).
1616 * @param {string} elementSeparator The separator string inserted between each
1617 * element inside each sub-array in the data, if there are more than one;
1618 * commonly a comma (,).
1619 * @param {string} axisSeparator The separator string inserted between each
1620 * axis specification, if there are more than one; usually a pipe sign (|).
1621 * @return {string} The multi-axis data string.
1622 * @private
1623 */
1624 goog.ui.ServerChart.prototype.computeMultiAxisDataString_ = function(
1625 data, indexSeparator, elementSeparator, axisSeparator) {
1626 var elementStrings = [];
1627 for (var i = 0, setLen = this.multiAxisType_.length; i < setLen; ++i) {
1628 if (data[i]) {
1629 elementStrings.push(i + indexSeparator + data[i].join(elementSeparator));
1630 }
1631 }
1632 return elementStrings.join(axisSeparator);
1633 };
1634
1635
1636 /**
1637 * Array of possible ChartServer data values
1638 * @type {string}
1639 */
1640 goog.ui.ServerChart.CHART_VALUES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
1641 'abcdefghijklmnopqrstuvwxyz' +
1642 '0123456789';
1643
1644
1645 /**
1646 * Array of extended ChartServer data values
1647 * @type {string}
1648 */
1649 goog.ui.ServerChart.CHART_VALUES_EXTENDED =
1650 goog.ui.ServerChart.CHART_VALUES + '-.';
1651
1652
1653 /**
1654 * Upper bound for extended values
1655 */
1656 goog.ui.ServerChart.EXTENDED_UPPER_BOUND =
1657 Math.pow(goog.ui.ServerChart.CHART_VALUES_EXTENDED.length, 2) - 1;
1658
1659
1660 /**
1661 * Converts a single number to an encoded data value suitable for ChartServer.
1662 * The TEXT encoding is the number in decimal; the SIMPLE encoding is a single
1663 * character, and the EXTENDED encoding is two characters. See
1664 * https://developers.google.com/chart/image/docs/data_formats for the detailed
1665 * specification of these encoding formats.
1666 *
1667 * @private
1668 * @param {?number} value The value to convert (null for a missing data point).
1669 * @param {number} minValue The minimum value (used for normalization).
1670 * @param {number} maxValue The maximum value (used for normalization).
1671 * @param {goog.ui.ServerChart.EncodingType} encoding The data encoding to use;
1672 * must not be AUTOMATIC.
1673 * @return {string} The encoded data value.
1674 */
1675 goog.ui.ServerChart.prototype.getConvertedValue_ = function(
1676 value, minValue, maxValue, encoding) {
1677 goog.asserts.assert(
1678 minValue <= maxValue,
1679 'minValue should be less than or equal to maxValue');
1680 var isExtended = (encoding == goog.ui.ServerChart.EncodingType.EXTENDED);
1681
1682 if (goog.isNull(value) || !goog.isDef(value) || isNaN(value) ||
1683 value < minValue || value > maxValue) {
1684 return isExtended ? '__' : '_';
1685 }
1686
1687 if (encoding == goog.ui.ServerChart.EncodingType.TEXT) {
1688 return String(value);
1689 }
1690
1691 var frac = goog.ui.ServerChart.DEFAULT_NORMALIZATION;
1692 if (maxValue > minValue) {
1693 frac = (value - minValue) / (maxValue - minValue);
1694 // Previous checks of value ensure that 0 <= frac <= 1 at this point.
1695 }
1696
1697 if (isExtended) {
1698 var maxIndex = goog.ui.ServerChart.CHART_VALUES_EXTENDED.length;
1699 var upperBound = goog.ui.ServerChart.EXTENDED_UPPER_BOUND;
1700 var index1 = Math.floor(frac * upperBound / maxIndex);
1701 var index2 = Math.floor((frac * upperBound) % maxIndex);
1702 var extendedVals = goog.ui.ServerChart.CHART_VALUES_EXTENDED;
1703 return extendedVals.charAt(index1) + extendedVals.charAt(index2);
1704 }
1705
1706 var index = Math.round(frac * (goog.ui.ServerChart.CHART_VALUES.length - 1));
1707 return goog.ui.ServerChart.CHART_VALUES.charAt(index);
1708 };
1709
1710
1711 /**
1712 * Creates the chd string for chartserver.
1713 *
1714 * @private
1715 * @param {Array<number>} values An array of numbers to graph.
1716 * @param {number} minValue The minimum value (used for normalization).
1717 * @param {number} maxValue The maximum value (used for normalization).
1718 * @param {goog.ui.ServerChart.EncodingType} encoding The data encoding to use;
1719 * must not be AUTOMATIC.
1720 * @return {string} The chd string for chartserver.
1721 */
1722 goog.ui.ServerChart.prototype.getChartServerValues_ = function(
1723 values, minValue, maxValue, encoding) {
1724 var s = [];
1725 for (var i = 0, valuesLen = values.length; i < valuesLen; ++i) {
1726 s.push(this.getConvertedValue_(values[i], minValue, maxValue, encoding));
1727 }
1728 return s.join(
1729 this.encodingType_ == goog.ui.ServerChart.EncodingType.TEXT ? ',' : '');
1730 };
1731
1732
1733 /**
1734 * Finds the minimum value in an array and returns it.
1735 * Needed because Math.min does not handle sparse arrays the way we want.
1736 *
1737 * @param {Array<number?>} ary An array of values.
1738 * @return {number} The minimum value.
1739 * @private
1740 */
1741 goog.ui.ServerChart.prototype.arrayMin_ = function(ary) {
1742 var min = Infinity;
1743 for (var i = 0, aryLen = ary.length; i < aryLen; ++i) {
1744 var value = ary[i];
1745 if (value != null && value < min) {
1746 min = value;
1747 }
1748 }
1749 return min;
1750 };
1751
1752
1753 /**
1754 * Finds the maximum value in an array and returns it.
1755 * Needed because Math.max does not handle sparse arrays the way we want.
1756 *
1757 * @param {Array<number?>} ary An array of values.
1758 * @return {number} The maximum value.
1759 * @private
1760 */
1761 goog.ui.ServerChart.prototype.arrayMax_ = function(ary) {
1762 var max = -Infinity;
1763 for (var i = 0, aryLen = ary.length; i < aryLen; ++i) {
1764 var value = ary[i];
1765 if (value != null && value > max) {
1766 max = value;
1767 }
1768 }
1769 return max;
1770 };
1771
1772
1773 /** @override */
1774 goog.ui.ServerChart.prototype.disposeInternal = function() {
1775 goog.ui.ServerChart.superClass_.disposeInternal.call(this);
1776 delete this.xLabels_;
1777 delete this.leftLabels_;
1778 delete this.rightLabels_;
1779 delete this.gridX_;
1780 delete this.gridY_;
1781 delete this.setColors_;
1782 delete this.setLegendTexts_;
1783 delete this.dataSets_;
1784 this.uri_ = null;
1785 delete this.minValue_;
1786 delete this.maxValue_;
1787 this.title_ = null;
1788 delete this.multiAxisType_;
1789 delete this.multiAxisLabelText_;
1790 delete this.multiAxisLabelPosition_;
1791 delete this.multiAxisRange_;
1792 delete this.multiAxisLabelStyle_;
1793 this.legend_ = null;
1794 };
1795
1796
1797 /**
1798 * Event types dispatched by the ServerChart object
1799 * @enum {string}
1800 */
1801 goog.ui.ServerChart.Event = {
1802 /**
1803 * Dispatched when the resulting URI reaches or exceeds the URI length limit.
1804 */
1805 URI_TOO_LONG: 'uritoolong'
1806 };
1807
1808
1809
1810 /**
1811 * Class for the event dispatched on the ServerChart when the resulting URI
1812 * exceeds the URI length limit.
1813 * @constructor
1814 * @param {string} uri The overly-long URI string.
1815 * @extends {goog.events.Event}
1816 * @final
1817 */
1818 goog.ui.ServerChart.UriTooLongEvent = function(uri) {
1819 goog.events.Event.call(this, goog.ui.ServerChart.Event.URI_TOO_LONG);
1820
1821 /**
1822 * The overly-long URI string.
1823 * @type {string}
1824 */
1825 this.uri = uri;
1826 };
1827 goog.inherits(goog.ui.ServerChart.UriTooLongEvent, goog.events.Event);