      1 'use strict';
      2
      3 /**
      4  * @license
      5  * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
      6  * MIT-licenced: https://opensource.org/licenses/MIT
      7  */
      8
      9 /**
     10  * @fileoverview A wrapper around the Dygraph class which implements the
     11  * interface for a GViz (aka Google Visualization API) visualization.
     12  * It is designed to be a drop-in replacement for Google's AnnotatedTimeline,
     13  * so the documentation at
     14  * http://code.google.com/apis/chart/interactive/docs/gallery/annotatedtimeline.html
     15  * translates over directly.
     16  *
     17  * For a full demo, see:
     18  * - http://dygraphs.com/tests/gviz.html
     19  * - http://dygraphs.com/tests/annotation-gviz.html
     20  */
     21
     22 /*global Dygraph:false */
     23
     24 import Dygraph from './dygraph';
     25
     26 /**
     27  * A wrapper around Dygraph that implements the gviz API.
     28  * @param {!HTMLDivElement} container The DOM object the visualization should
     29  *     live in.
     30  * @constructor
     31  */
     32 var GVizChart = function(container) {
     33   this.container = container;
     34 };
     35
     36 /**
     37  * @param {GVizDataTable} data
     38  * @param {Object.<*>} options
     39  */
     40 GVizChart.prototype.draw = function(data, options) {
     41   // Clear out any existing dygraph.
     42   // TODO(danvk): would it make more sense to simply redraw using the current
     43   // date_graph object?
     44   this.container.innerHTML = '';
     45   if (typeof(this.date_graph) != 'undefined') {
     46     this.date_graph.destroy();
     47   }
     48
     49   this.date_graph = new Dygraph(this.container, data, options);
     50 };
     51
     52 /**
     53  * Google charts compatible setSelection
     54  * Only row selection is supported, all points in the row will be highlighted
     55  * @param {Array.<{row:number}>} selection_array array of the selected cells
     56  * @public
     57  */
     58 GVizChart.prototype.setSelection = function(selection_array) {
     59   var row = false;
     60   if (selection_array.length) {
     61     row = selection_array[0].row;
     62   }
     63   this.date_graph.setSelection(row);
     64 };
     65
     66 /**
     67  * Google charts compatible getSelection implementation
     68  * @return {Array.<{row:number,column:number}>} array of the selected cells
     69  * @public
     70  */
     71 GVizChart.prototype.getSelection = function() {
     72   var selection = [];
     73
     74   var row = this.date_graph.getSelection();
     75
     76   if (row < 0) return selection;
     77
     78   var points = this.date_graph.layout_.points;
     79   for (var setIdx = 0; setIdx < points.length; ++setIdx) {
     80     selection.push({row: row, column: setIdx + 1});
     81   }
     82
     83   return selection;
     84 };
     85
     86 export default GVizChart;
     87