OLD | NEW |
1 <!-- | 1 <!-- |
2 The common.js file must be included before this file. | 2 The common.js file must be included before this file. |
3 | 3 |
4 This in an HTML Import-able file that contains the definition | 4 This in an HTML Import-able file that contains the definition |
5 of the following elements: | 5 of the following elements: |
6 | 6 |
7 <bar-chart-sk> | 7 <bar-chart-sk> |
8 | 8 |
9 To use this file import it: | 9 To use this file import it: |
10 | 10 |
11 <link href="/res/imp/bar-chart-sk.html" rel="import" /> | 11 <link href="/res/imp/bar-chart-sk.html" rel="import" /> |
12 | 12 |
13 Usage: | 13 Usage: |
14 | 14 |
15 <bar-chart-sk heading="My Chart"></bar-chart-sk> | 15 <bar-chart-sk heading="My Chart"></bar-chart-sk> |
16 <script> | 16 <script> |
17 myChart.draw(columns, data); | 17 myChart.draw(columns, data); |
18 </script> | 18 </script> |
19 | 19 |
20 Properties: | 20 Properties: |
21 heading: string; title of the chart. | 21 heading: string; title of the chart. |
22 colors: array of strings; colors to use in the chart. | 22 colors: array of strings; colors to use in the chart. |
| 23 columns: array of arrays indicating the types and names of the columns, eg: |
| 24 [["string", "seriesName"], ["number", "dataPoint"]]. |
| 25 data: array of arrays containing data for each column, eg: |
| 26 [["series1", 42], ["series2", 7]]. |
23 | 27 |
24 Methods: | 28 Methods: |
25 draw: render the chart using the given columns and data. The 'columns' | 29 draw: render the chart. |
26 parameter is an array of arrays indicating the types and names of the | |
27 columns, eg: [["string", "seriesName"], ["number", "dataPoint"]]. | |
28 The 'data' parameter is an array of arrays containing data for each | |
29 column, eg: [["series1", 42], ["series2", 7]]. | |
30 --> | 30 --> |
31 <polymer-element name="bar-chart-sk"> | 31 <polymer-element name="bar-chart-sk"> |
32 <template> | 32 <template> |
33 <style> | 33 <style> |
34 .chart { | 34 .chart { |
35 border: 1px solid #eeeeee; | 35 border: 1px solid #eeeeee; |
36 margin: 5px; | 36 margin: 5px; |
37 padding: 10px; | 37 padding: 10px; |
| 38 font-size: 12px; |
| 39 } |
| 40 h2 { |
| 41 font-size: 16px; |
38 } | 42 } |
39 .collapseHeader { | 43 .collapseHeader { |
40 cursor: pointer; | 44 cursor: pointer; |
41 } | 45 } |
42 .collapseBody { | 46 .collapseBody { |
43 padding: 10px; | 47 padding: 10px; |
44 } | 48 } |
45 </style> | 49 </style> |
46 <div class="chart"> | 50 <div class="chart" id="host"> |
47 <div class="collapseHeader" on-click="{{toggle}}"> | 51 <div class="collapseHeader" on-click="{{toggle}}" horizontal layout center
> |
48 {{heading}} | 52 <h2 flex>{{heading}}</h2> |
| 53 <core-icon icon="{{icon}}"></core-icon> |
49 </div> | 54 </div> |
50 <core-collapse id="collapse"> | 55 <core-collapse id="collapse" on-core-collapse-open="{{collapseOpen}}"> |
51 <div class="collapseBody" id="chartcontainer"></div> | 56 <div class="collapseBody" id="chartcontainer"></div> |
52 </core-collapse> | 57 </core-collapse> |
53 </div> | 58 </div> |
54 | 59 |
55 </template> | 60 </template> |
56 <script> | 61 <script> |
| 62 (function() { |
| 63 var chartsReady = false; |
| 64 var ChartsReady = new Promise(function(resolve, reject) { |
| 65 if (chartsReady) { |
| 66 resolve(); |
| 67 } else { |
| 68 google.setOnLoadCallback(function() { |
| 69 chartsReady = true; |
| 70 resolve(); |
| 71 }); |
| 72 } |
| 73 }); |
| 74 |
57 Polymer({ | 75 Polymer({ |
58 publish: { | 76 publish: { |
59 heading: { | 77 heading: { |
60 value: null, | 78 value: null, |
61 reflect: true, | 79 reflect: true, |
62 }, | 80 }, |
63 colors: { | 81 colors: { |
64 value: [], | 82 value: [], |
65 reflect: false, | 83 reflect: false, |
66 }, | 84 }, |
| 85 columns: { |
| 86 value: null, |
| 87 reflect: false, |
| 88 }, |
| 89 data: { |
| 90 value: null, |
| 91 reflect: false, |
| 92 }, |
| 93 }, |
| 94 |
| 95 created: function() { |
| 96 this.icon = "arrow-drop-down"; |
| 97 }, |
| 98 |
| 99 ready: function() { |
| 100 var that = this; |
| 101 window.addEventListener("resize", function() { |
| 102 that.draw(); |
| 103 }); |
67 }, | 104 }, |
68 | 105 |
69 toggle: function() { | 106 toggle: function() { |
70 this.$.collapse.toggle(); | 107 this.$.collapse.toggle(); |
71 }, | 108 }, |
72 | 109 |
73 draw: function(columns, data) { | 110 colorsChanged: function() { |
74 var table = new google.visualization.DataTable(); | 111 this.draw(); |
75 for (var i = 0; i < columns.length; i++) { | 112 }, |
76 table.addColumn(columns[i][0], columns[i][1]); | 113 |
| 114 columnsChanged: function() { |
| 115 this.draw(); |
| 116 }, |
| 117 |
| 118 dataChanged: function() { |
| 119 this.draw(); |
| 120 }, |
| 121 |
| 122 collapseOpen: function() { |
| 123 if (this.$.collapse.opened) { |
| 124 this.icon = "arrow-drop-up"; |
| 125 } else { |
| 126 this.icon = "arrow-drop-down"; |
77 } | 127 } |
78 table.addRows(data); | 128 }, |
79 | 129 |
80 var headerHeight = 0; | 130 draw: function() { |
81 var chartWidth = 400; | 131 if (!this.columns || !this.data) { |
82 var chartHeight = 15 * data.length; | 132 return; |
83 var labelWidth = 410; | |
84 var options = { | |
85 "chartArea": { | |
86 "top": headerHeight, | |
87 "left": labelWidth, | |
88 "width": chartWidth, | |
89 "height": chartHeight, | |
90 }, | |
91 "legend": { "position": "none" }, | |
92 "width": labelWidth + chartWidth, | |
93 "height": headerHeight + chartHeight, | |
94 "fontSize": 12, | |
95 "titleTextStyle": { "fontSize": 18 }, | |
96 }; | |
97 if (this.colors) { | |
98 options["colors"] = this.colors; | |
99 } | 133 } |
| 134 var that = this; |
| 135 ChartsReady.then(function() { |
| 136 var table = new google.visualization.DataTable(); |
| 137 for (var i = 0; i < that.columns.length; i++) { |
| 138 table.addColumn(that.columns[i][0], that.columns[i][1]); |
| 139 } |
| 140 table.addRows(that.data); |
100 | 141 |
101 var chart = new google.visualization.BarChart(this.$.chartcontainer); | 142 var totalWidth = that.$.host.offsetWidth; |
102 chart.draw(table, options); | 143 var headerHeight = 0; |
| 144 var labelWidth = 410; |
| 145 var chartWidth = totalWidth - labelWidth; |
| 146 var chartHeight = 15 * that.data.length; |
| 147 var options = { |
| 148 "chartArea": { |
| 149 "top": headerHeight, |
| 150 "left": labelWidth, |
| 151 "width": chartWidth, |
| 152 "height": chartHeight, |
| 153 }, |
| 154 "legend": { "position": "none" }, |
| 155 "width": totalWidth, |
| 156 "height": headerHeight + chartHeight, |
| 157 "fontSize": 12, |
| 158 "titleTextStyle": { "fontSize": 18 }, |
| 159 }; |
| 160 if (that.colors) { |
| 161 options["colors"] = that.colors; |
| 162 } |
| 163 |
| 164 var chart = new google.visualization.BarChart(that.$.chartcontainer); |
| 165 chart.draw(table, options); |
| 166 }); |
103 }, | 167 }, |
104 }); | 168 }); |
| 169 })(); |
105 </script> | 170 </script> |
106 </polymer-element> | 171 </polymer-element> |
OLD | NEW |