Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="https://polygit.org/components/">
  6. <script src="webcomponentsjs/webcomponents-lite.min.js"></script>
  7. <link href="polymer/polymer.html" rel="import">
  8. <link href="google-chart/google-chart.html" rel="import"> </head>
  9.  
  10. <body>
  11. <dom-module id="x-element">
  12. <template>
  13. <style>
  14. google-chart {
  15. width: 100%;
  16. }
  17. </style>
  18. <br><br><br><br>
  19. <button on-tap="_show">Show Values</button>
  20. <button on-tap="clearAll">Clear All</button>
  21. <button on-tap="selectAll">Select All</button>
  22. <div>[[selectedString]]</div>
  23. <google-chart id="geochart"
  24. type="geo"
  25. options="[[options]]"
  26. data="[[data]]"
  27. on-google-chart-select="_onGoogleChartSelect">
  28. </google-chart>
  29. </template>
  30. <script>
  31. (function() {
  32. // Monkey patch for google-chart
  33. var gcp = Object.getPrototypeOf(document.createElement('google-chart'));
  34. gcp.drawChart = function() {
  35. if (this._canDraw) {
  36. if (!this.options) {
  37. this.options = {};
  38. }
  39. if (!this._chartObject) {
  40. var chartClass = this._chartTypes[this.type];
  41. if (chartClass) {
  42. this._chartObject = new chartClass(this.$.chartdiv);
  43. google.visualization.events.addOneTimeListener(this._chartObject,
  44. 'ready', function() {
  45. this.fire('google-chart-render');
  46. }.bind(this));
  47. google.visualization.events.addListener(this._chartObject,
  48. 'select', function() {
  49. this.selection = this._chartObject.getSelection();
  50. this.fire('google-chart-select', { selection: this.selection });
  51. }.bind(this));
  52. if (this._chartObject.setSelection){
  53. this._chartObject.setSelection(this.selection);
  54. }
  55. }
  56. }
  57. if (this._chartObject) {
  58. this._chartObject.draw(this._dataTable, this.options);
  59. } else {
  60. this.$.chartdiv.innerHTML = 'Undefined chart type';
  61. }
  62. }
  63. };
  64. Polymer({
  65. is: 'x-element',
  66. /** /
  67. * Fired when user selects chart item.
  68. *
  69. * @event us-map-select
  70. * @param {object} detail Alpabetized array of selected state names.
  71. /**/
  72. properties: {
  73. items: {
  74. type: Array,
  75. value: function() {
  76. return [ 'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming', ].sort();
  77. },
  78. },
  79. color: {
  80. type: String,
  81. value: 'blue',
  82. },
  83. options: {
  84. type: Object,
  85. computed: '_computeOptions(color)',
  86. },
  87. selected: {
  88. type: Array,
  89. value: function() {
  90. return [];
  91. }
  92. },
  93. data: {
  94. type: Array,
  95. computed: '_computeData(items, selected.length)'
  96. },
  97. selectedString: {
  98. type: String,
  99. computed: '_computeSelectedString(selected.length)',
  100. },
  101. },
  102. _computeOptions: function() {
  103. return {
  104. region: 'US',
  105. displayMode: 'regions',
  106. resolution: 'provinces',
  107. legend: 'none',
  108. defaultColor: 'white',
  109. colorAxis: {
  110. colors: ['#E0E0E0', this.color],
  111. minValue: 0,
  112. maxValue: 1,
  113. }
  114. }
  115. },
  116. // On select event, compute 'selected'
  117. _onGoogleChartSelect: function(e) {
  118. var string = e.path[0].textContent.split('Select')[0].trim(), // e.g. 'Ohio'
  119. selected = this.selected, // Array of selected items
  120. index = selected.indexOf(string);
  121. // If 'string' is not in 'selected' array, add it; else delete it
  122. if (index === -1) {
  123. this.push('selected', string);
  124. } else {
  125. this.splice('selected', index, 1);
  126. }
  127. },
  128. _computeSelectedString: function(selectedInfo) {
  129. return this.selected.sort().join(', ');
  130. },
  131. // After 'items' populates or 'selected' changes, compute 'data'
  132. _computeData: function(items, selectedInfo) {
  133. var data = [],
  134. selected = this.selected,
  135. i = items.length;
  136. while (i--) {
  137. data.unshift([items[i], selected.indexOf(items[i]) > -1 ? 1 : 0]);
  138. }
  139. data.unshift(['State', 'Select']);
  140. return data;
  141. },
  142. clearAll: function() {
  143. this.set('selected', []);
  144. },
  145. selectAll: function() {
  146. this.set('selected', this.items);
  147. },
  148. _show: function() {
  149. //console.log('items', this.items);
  150. console.log('selected', this.selected);
  151. //console.log('data', this.data);
  152. },
  153. });
  154. })();
  155. </script>
  156. </dom-module>
  157. <x-element color="red" selected='["Colorado", "South Dakota"]'></x-element>
  158. </body>
  159.  
  160. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement