1/*
2 *    Copyright 2015-2017 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17var ILinePlot = ( function() {
18
19   var graphs = new Array();
20   var syncObjs = new Array();
21
22    var purge = function() {
23        for (var div_name in graphs) {
24            if (document.getElementById(div_name) == null) {
25                delete graphs[div_name];
26            }
27        }
28    };
29
30    var sync = function(group) {
31
32        var syncGraphs = Array();
33        var xRange;
34        var yRange;
35        var syncZoom = true;
36
37        for (var div_name in graphs) {
38
39            if (graphs[div_name].group == group) {
40                syncGraphs.push(graphs[div_name].graph);
41                syncZoom = syncZoom & graphs[div_name].syncZoom;
42
43                var xR = graphs[div_name].graph.xAxisRange();
44                var yR = graphs[div_name].graph.yAxisRange();
45
46                if (xRange != undefined) {
47                    if (xR[0] < xRange[0])
48                        xRange[0] = xR[0];
49                    if (xR[1] > xRange[1])
50                        xRange[1] = xR[1];
51                } else
52                    xRange = xR;
53
54                if (yRange != undefined) {
55                    if (yR[0] < yRange[0])
56                        yRange[0] = yR[0];
57                    if (yR[1] > yRange[1])
58                        yRange[1] = yR[1];
59                } else
60                    yRange = yR;
61            }
62        }
63
64        if (syncGraphs.length >= 2) {
65            if (syncZoom) {
66                if (syncObjs[group] != undefined)
67                    syncObjs[group].detach();
68
69                syncObjs[group] = Dygraph.synchronize(syncGraphs, {
70                    zoom: true,
71                    selection: false,
72                    range: true
73                });
74            }
75
76            $.each(syncGraphs, function(g) {
77                var graph = syncGraphs[g];
78
79                graph.updateOptions({
80                    valueRange: yRange,
81                    dateWindow: xRange
82                });
83
84                if (graph.padFront_ == undefined) {
85                    graph.padFront_ = true;
86                    var _decoy_elem = new Array(graph.rawData_[0].length);
87                    graph.rawData_.unshift(_decoy_elem);
88                }
89                graph.rawData_[0][0] = xRange[0];
90
91                if (graph.padBack_ == undefined) {
92                    graph.padBack_ = true;
93                    var _decoy_elem = new Array(graph.rawData_[0].length);
94                    graph.rawData_.push(_decoy_elem);
95                }
96                graph.rawData_[graph.rawData_.length - 1][0] = xRange[1];
97            });
98        }
99    };
100
101    var generate = function(data, colors) {
102        create_graph(data, colors);
103        purge();
104        if (data.syncGroup != undefined)
105            sync(data.syncGroup);
106    };
107
108    var create_graph = function(t_info, colors) {
109        var tabular = t_info.data;
110
111        var options = {
112            legend: 'always',
113            title: t_info.title,
114            labels: tabular.labels,
115            labelsDivStyles: {
116                'textAlign': 'right'
117            },
118            rollPeriod: 1,
119            animatedZooms: true,
120            connectSeparatedPoints: true,
121            showRangeSelector: t_info.rangesel,
122            rangeSelectorHeight: 50,
123            stepPlot: t_info.step_plot,
124            logscale: t_info.logscale,
125            fillGraph: t_info.fill_graph,
126            labelsDiv: t_info.name + "_legend",
127            errorBars: false,
128            labelsSeparateLines: true,
129            valueRange: t_info.valueRange,
130            drawPoints: t_info.drawPoints,
131            strokeWidth: t_info.strokeWidth,
132            pointSize: t_info.pointSize,
133            dateWindow: t_info.dateWindow
134        };
135
136        if (typeof t_info.fill_alpha !== 'undefined')
137            options.fillAlpha = t_info.fill_alpha;
138
139        if (typeof colors !== 'undefined')
140            options["colors"] = colors;
141
142        var graph = new Dygraph(document.getElementById(t_info.name), tabular.data, options);
143
144        var width = $("#" + t_info.name)
145            .closest(".output_subarea").width() / t_info.per_line
146
147        /*
148         * Remove 3 pixels from width to avoid unnecessary horizontal scrollbar
149         */
150        graph.resize(width - 3, t_info.height);
151
152        $(window).on("resize." + t_info.name, function() {
153
154            var width = $("#" + t_info.name)
155                .closest(".output_subarea").width() / t_info.per_line
156
157            graph.resize(width, t_info.height);
158        });
159
160        graphs[t_info.name] =
161            {
162                graph: graph,
163                group: t_info.syncGroup,
164                syncZoom: t_info.syncZoom
165            };
166
167    };
168
169    return {
170        generate: generate
171    };
172
173}());
174