1<!DOCTYPE html>
2<!--
3Copyright (c) 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7<link rel="import" href="/tracing/base/statistics.html">
8<script>
9'use strict';
10tr.exportTo('tr.v.ui', function() {
11  var ArrayOfNumbersSummaryModes = {
12    AVERAGE_MODE: 'average-mode',
13    TOTAL_MODE: 'total-mode'
14  };
15  return {
16    ArrayOfNumbersSummaryModes: ArrayOfNumbersSummaryModes
17  };
18});
19</script>
20<polymer-element name="tr-v-ui-array-of-numbers-span">
21  <template>
22  </template>
23  <script>
24  'use strict';
25
26  Polymer({
27    created: function() {
28      this.numbers_ = undefined;
29      this.summaryMode_ = tr.v.ui.ArrayOfNumbersSummaryModes.AVERAGE_MODE;
30    },
31
32    get summaryMode() {
33      return this.summaryMode_;
34    },
35
36    set summaryMode(summaryMode) {
37      this.summaryMode_ = summaryMode;
38      this.updateContents_();
39    },
40
41    get numbers() {
42      return this.numbers_;
43    },
44
45    set numbers(numbers) {
46      if (numbers === undefined) {
47        this.numbers_ = undefined;
48        this.updateContents_();
49        return;
50      }
51      if (!(numbers instanceof Array))
52        throw new Error('Must provide an array');
53      this.numbers_ = numbers;
54      this.updateContents_();
55    },
56
57    updateContents_: function() {
58      if (this.numbers_ === undefined) {
59        this.shadowRoot.textContent = '-';
60        return;
61      }
62
63      var ArrayOfNumbersSummaryModes = tr.v.ui.ArrayOfNumbersSummaryModes;
64      var value;
65      if (this.summaryMode_ === ArrayOfNumbersSummaryModes.AVERAGE_MODE)
66        value = tr.b.Statistics.mean(this.numbers_);
67      else
68        value = tr.b.Statistics.sum(this.numbers_);
69
70      var valueRounded = Math.round(value * 1000.0) / 1000.0;
71      this.shadowRoot.textContent = valueRounded;
72    }
73  });
74  </script>
75</polymer-element>
76