charts.ko.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. ko.bindingHandlers.pieChart = {
  17. init: function (element, valueAccessor) {
  18. var _options = valueAccessor();
  19. var _data = _options.transformer(_options.data);
  20. nv.addGraph(function () {
  21. var _chart = nv.models.growingPieChart()
  22. .x(function (d) {
  23. return d.label
  24. })
  25. .y(function (d) {
  26. return d.value
  27. })
  28. .height($(element).width())
  29. .showLabels(true).showLegend(false);
  30. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  31. _d3.datum(_data)
  32. .transition().duration(350)
  33. .each("end", _options.onComplete)
  34. .call(_chart);
  35. $.each(_options.fqs(), function(cnt, item){
  36. if (item.field() == _options.field()){
  37. _chart.selectSlices(item.filter());
  38. }
  39. });
  40. nv.utils.windowResize(_chart.update);
  41. $(element).height($(element).width());
  42. return _chart;
  43. }, function () {
  44. d3.selectAll(".nv-slice").on('click',
  45. function (d, i) {
  46. _options.onClick(d);
  47. });
  48. });
  49. },
  50. update: function (element, valueAccessor) {
  51. var value = valueAccessor();
  52. // do something with the updated value
  53. }
  54. };
  55. ko.bindingHandlers.barChart = {
  56. init: function (element, valueAccessor) {
  57. barChart(element, valueAccessor(), false);
  58. },
  59. update: function (element, valueAccessor) {
  60. var value = valueAccessor();
  61. // do something with the updated value
  62. }
  63. };
  64. ko.bindingHandlers.timelineChart = {
  65. init: function (element, valueAccessor) {
  66. barChart(element, valueAccessor(), true);
  67. },
  68. update: function (element, valueAccessor) {
  69. var value = valueAccessor();
  70. // do something with the updated value
  71. }
  72. };
  73. ko.bindingHandlers.lineChart = {
  74. init: function (element, valueAccessor) {
  75. lineChart(element, valueAccessor());
  76. },
  77. update: function (element, valueAccessor) {
  78. var value = valueAccessor();
  79. // do something with the updated value
  80. }
  81. };
  82. ko.bindingHandlers.mapChart = {
  83. init: function (element, valueAccessor) {
  84. $(element).height($(element).width()/2.5);
  85. $(element).css("position", "relative");
  86. var _options = valueAccessor();
  87. var basic = new Datamap({
  88. element: element
  89. });
  90. _options.onComplete();
  91. },
  92. update: function (element, valueAccessor) {
  93. var value = valueAccessor();
  94. // do something with the updated value
  95. }
  96. };
  97. function lineChart(element, options) {
  98. var _datum = options.transformer(options.datum);
  99. $(element).height(300);
  100. nv.addGraph(function () {
  101. var _chart = nv.models.lineWithBrushChart();
  102. _chart.enableSelection();
  103. _chart.onSelectRange(options.onSelectRange);
  104. _chart.xAxis
  105. .showMaxMin(true)
  106. .tickFormat(d3.format(',0f'));
  107. _chart.margin({bottom: 100})
  108. .transitionDuration(300);
  109. _chart.yAxis
  110. .tickFormat(d3.format(',0f'));
  111. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  112. _d3.datum(_datum)
  113. .transition().duration(350)
  114. .each("end", options.onComplete)
  115. .call(_chart);
  116. nv.utils.windowResize(_chart.update);
  117. return _chart;
  118. }, function () {
  119. d3.selectAll(".nv-bar").on('click',
  120. function (d, i) {
  121. options.onClick(d);
  122. });
  123. });
  124. }
  125. function barChart(element, options, isTimeline) {
  126. var _datum = options.transformer(options.datum);
  127. $(element).height(300);
  128. nv.addGraph(function () {
  129. var _chart;
  130. if (isTimeline) {
  131. _chart = nv.models.multiBarWithBrushChart();
  132. _chart.enableSelection();
  133. _chart.onSelectRange(options.onSelectRange);
  134. _chart.xAxis
  135. .showMaxMin(true)
  136. .tickFormat(d3.time.format("%Y-%m-%d %H:%M:%S"));
  137. _chart.multibar.hideable(true);
  138. }
  139. else {
  140. var _isDiscrete = false;
  141. for (var j=0;j<_datum.length;j++){
  142. for (var i=0;i<_datum[j].values.length;i++){
  143. if (isNaN(_datum[j].values[i].x * 1)){
  144. _isDiscrete = true;
  145. break;
  146. }
  147. }
  148. }
  149. if (_isDiscrete){
  150. _chart = nv.models.growingDiscreteBarChart()
  151. .x(function(d) { return d.x })
  152. .y(function(d) { return d.y })
  153. .staggerLabels(true);
  154. }
  155. else {
  156. _chart = nv.models.growingMultiBarChart();
  157. _chart.xAxis
  158. .showMaxMin(true)
  159. .tickFormat(d3.format(',0f'));
  160. _chart.multibar.hideable(true);
  161. }
  162. }
  163. _chart.margin({bottom: 100})
  164. .transitionDuration(300);
  165. _chart.yAxis
  166. .tickFormat(d3.format(',0f'));
  167. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  168. _d3.datum(_datum)
  169. .transition().duration(350)
  170. .each("end", options.onComplete)
  171. .call(_chart);
  172. nv.utils.windowResize(_chart.update);
  173. return _chart;
  174. }, function () {
  175. d3.selectAll(".nv-bar").on('click',
  176. function (d, i) {
  177. options.onClick(d);
  178. });
  179. });
  180. }