summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/tracing/tracing/ui/tracks/chart_track.html
blob: 58ef08d651c7b0d4a06b6621fdf7509f6de9e6ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/ui/base/heading.html">
<link rel="import" href="/tracing/ui/base/ui.html">
<link rel="import" href="/tracing/ui/tracks/chart_transform.html">
<link rel="import" href="/tracing/ui/tracks/track.html">

<style>
.chart-track {
  height: 30px;
  position: relative;
}
</style>

<script>
'use strict';

tr.exportTo('tr.ui.tracks', function() {
  /**
   * A track that displays a chart.
   *
   * @constructor
   * @extends {Track}
   */
  const ChartTrack =
      tr.ui.b.define('chart-track', tr.ui.tracks.Track);

  ChartTrack.prototype = {
    __proto__: tr.ui.tracks.Track.prototype,

    decorate(viewport) {
      tr.ui.tracks.Track.prototype.decorate.call(this, viewport);
      Polymer.dom(this).classList.add('chart-track');
      this.series_ = undefined;
      this.axes_ = undefined;

      // GUID -> {axis: ChartSeriesYAxis, series: [ChartSeries]}.
      this.axisGuidToAxisData_ = undefined;

      // The maximum top and bottom padding of all series.
      this.topPadding_ = undefined;
      this.bottomPadding_ = undefined;

      this.showYAxisLabels_ = undefined;
      this.showGridLines_ = undefined;

      this.heading_ = document.createElement('tr-ui-b-heading');
      Polymer.dom(this).appendChild(this.heading_);
    },

    set heading(heading) {
      this.heading_.heading = heading;
    },

    get heading() {
      return this.heading_.heading;
    },

    set tooltip(tooltip) {
      this.heading_.tooltip = tooltip;
    },

    get series() {
      return this.series_;
    },

    /**
     * Set the list of chart series to be displayed on this track. The list
     * is assumed to be sorted in increasing z-order (i.e. the last series in
     * the list will be drawn at the top).
     */
    set series(series) {
      this.series_ = series;
      this.calculateAxisDataAndPadding_();
      this.invalidateDrawingContainer();
    },

    get height() {
      return window.getComputedStyle(this).height;
    },

    set height(height) {
      this.style.height = height;
      this.invalidateDrawingContainer();
    },

    get showYAxisLabels() {
      return this.showYAxisLabels_;
    },

    set showYAxisLabels(showYAxisLabels) {
      this.showYAxisLabels_ = showYAxisLabels;
      this.invalidateDrawingContainer();
    },

    get showGridLines() {
      return this.showGridLines_;
    },

    set showGridLines(showGridLines) {
      this.showGridLines_ = showGridLines;
      this.invalidateDrawingContainer();
    },

    get hasVisibleContent() {
      return !!this.series && this.series.length > 0;
    },

    calculateAxisDataAndPadding_() {
      if (!this.series_) {
        this.axes_ = undefined;
        this.axisGuidToAxisData_ = undefined;
        this.topPadding_ = undefined;
        this.bottomPadding_ = undefined;
        return;
      }

      const axisGuidToAxisData = {};
      let topPadding = 0;
      let bottomPadding = 0;

      this.series_.forEach(function(series) {
        const seriesYAxis = series.seriesYAxis;
        const axisGuid = seriesYAxis.guid;
        if (!(axisGuid in axisGuidToAxisData)) {
          axisGuidToAxisData[axisGuid] = {
            axis: seriesYAxis,
            series: []
          };
          if (!this.axes_) this.axes_ = [];
          this.axes_.push(seriesYAxis);
        }
        axisGuidToAxisData[axisGuid].series.push(series);
        topPadding = Math.max(topPadding, series.topPadding);
        bottomPadding = Math.max(bottomPadding, series.bottomPadding);
      }, this);

      this.axisGuidToAxisData_ = axisGuidToAxisData;
      this.topPadding_ = topPadding;
      this.bottomPadding_ = bottomPadding;
    },

    draw(type, viewLWorld, viewRWorld, viewHeight) {
      switch (type) {
        case tr.ui.tracks.DrawType.GENERAL_EVENT:
          this.drawChart_(viewLWorld, viewRWorld);
          break;
      }
    },

    drawChart_(viewLWorld, viewRWorld) {
      if (!this.series_) return;

      const ctx = this.context();

      // Get track drawing parameters.
      const displayTransform = this.viewport.currentDisplayTransform;
      const pixelRatio = window.devicePixelRatio || 1;
      const bounds = this.getBoundingClientRect();
      const highDetails = this.viewport.highDetails;

      // Pre-multiply all device-independent pixel parameters with the pixel
      // ratio to avoid unnecessary recomputation in the performance-critical
      // drawing code.
      const width = bounds.width * pixelRatio;
      const height = bounds.height * pixelRatio;
      const topPadding = this.topPadding_ * pixelRatio;
      const bottomPadding = this.bottomPadding_ * pixelRatio;

      // Set up clipping.
      ctx.save();
      ctx.beginPath();
      ctx.rect(0, 0, width, height);
      ctx.clip();

      // TODO(aiolos): Add support for secondary y-axis on right side of chart.
      // https://github.com/catapult-project/catapult/issues/3008
      // Draw y-axis grid lines.
      if (this.axes_) {
        if ((this.showGridLines_ || this.showYAxisLabels_) &&
            this.axes_.length > 1) {
          throw new Error('Only one axis allowed when showing grid lines.');
        }
        for (const yAxis of this.axes_) {
          const chartTransform = new tr.ui.tracks.ChartTransform(
              displayTransform, yAxis, width, height,
              topPadding, bottomPadding, pixelRatio);
          yAxis.draw(
              ctx, chartTransform, this.showYAxisLabels_, this.showGridLines_);
        }
      }

      // Draw all series in the increasing z-order.
      for (const series of this.series) {
        const chartTransform = new tr.ui.tracks.ChartTransform(
            displayTransform, series.seriesYAxis, width, height, topPadding,
            bottomPadding, pixelRatio);
        series.draw(ctx, chartTransform, highDetails);
      }

      // Stop clipping.
      ctx.restore();
    },

    addEventsToTrackMap(eventToTrackMap) {
      // TODO(petrcermak): Consider adding the series to the track map instead
      // of the track (a potential performance optimization).
      this.series_.forEach(function(series) {
        series.points.forEach(function(point) {
          point.addToTrackMap(eventToTrackMap, this);
        }, this);
      }, this);
    },

    addIntersectingEventsInRangeToSelectionInWorldSpace(
        loWX, hiWX, viewPixWidthWorld, selection) {
      this.series_.forEach(function(series) {
        series.addIntersectingEventsInRangeToSelectionInWorldSpace(
            loWX, hiWX, viewPixWidthWorld, selection);
      }, this);
    },

    addEventNearToProvidedEventToSelection(event, offset, selection) {
      let foundItem = false;
      this.series_.forEach(function(series) {
        foundItem = foundItem || series.addEventNearToProvidedEventToSelection(
            event, offset, selection);
      }, this);
      return foundItem;
    },

    addAllEventsMatchingFilterToSelection(filter, selection) {
      // Do nothing.
    },

    addClosestEventToSelection(worldX, worldMaxDist, loY, hiY,
        selection) {
      this.series_.forEach(function(series) {
        series.addClosestEventToSelection(
            worldX, worldMaxDist, loY, hiY, selection);
      }, this);
    },

    /**
     * Automatically set the bounds of all axes on this track from the range of
     * values of all series (in this track) associated with each of them.
     *
     * See the description of ChartSeriesYAxis.autoSetFromRange for the optional
     * configuration argument flags.
     */
    autoSetAllAxes(opt_config) {
      for (const axisData of Object.values(this.axisGuidToAxisData_)) {
        const seriesYAxis = axisData.axis;
        const series = axisData.series;
        seriesYAxis.autoSetFromSeries(series, opt_config);
      }
    },

    /**
     * Automatically set the bounds of the provided axis from the range of
     * values of all series (in this track) associated with it.
     *
     * See the description of ChartSeriesYAxis.autoSetFromRange for the optional
     * configuration argument flags.
     */
    autoSetAxis(seriesYAxis, opt_config) {
      const series = this.axisGuidToAxisData_[seriesYAxis.guid].series;
      seriesYAxis.autoSetFromSeries(series, opt_config);
    }
  };

  return {
    ChartTrack,
  };
});
</script>