charts.ko.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.pieChart()
  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. nv.utils.windowResize(_chart.update);
  36. $(element).height($(element).width());
  37. return _chart;
  38. }, function () {
  39. d3.selectAll(".nv-slice").on('click',
  40. function (d, i) {
  41. _options.onClick(d);
  42. });
  43. });
  44. },
  45. update: function (element, valueAccessor) {
  46. var value = valueAccessor();
  47. // do something with the updated value
  48. }
  49. };
  50. ko.bindingHandlers.barChart = {
  51. init: function (element, valueAccessor) {
  52. barChart(element, valueAccessor(), false);
  53. },
  54. update: function (element, valueAccessor) {
  55. var value = valueAccessor();
  56. // do something with the updated value
  57. }
  58. };
  59. ko.bindingHandlers.timelineChart = {
  60. init: function (element, valueAccessor) {
  61. barChart(element, valueAccessor(), true);
  62. },
  63. update: function (element, valueAccessor) {
  64. var value = valueAccessor();
  65. // do something with the updated value
  66. }
  67. };
  68. function barChart(element, options, isTimeline) {
  69. var _data = options.transformer(options.data);
  70. $(element).height(300);
  71. nv.addGraph(function () {
  72. var _chart;
  73. if (isTimeline) {
  74. _chart = nv.models.multiBarWithFocusChart();
  75. _chart.enableSelection();
  76. _chart.onSelectRange(function(from, to){
  77. console.log("SELECT RANGE!")
  78. console.log(from);
  79. console.log(to);
  80. });
  81. }
  82. else {
  83. _chart = nv.models.multiBarChart();
  84. }
  85. _chart.margin({bottom: 100})
  86. .transitionDuration(300);
  87. _chart.multibar
  88. .hideable(true);
  89. if (isTimeline) {
  90. _chart.xAxis
  91. .showMaxMin(true)
  92. .tickFormat(d3.time.format("%Y-%m-%d %H:%M:%S"));
  93. }
  94. else {
  95. _chart.xAxis
  96. .showMaxMin(true)
  97. .tickFormat(d3.format(',0f'));
  98. }
  99. _chart.yAxis
  100. .tickFormat(d3.format(',0f'));
  101. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  102. _d3.datum([
  103. {
  104. key: options.label,
  105. values: _data
  106. }
  107. ])
  108. .transition().duration(350)
  109. .each("end", options.onComplete)
  110. .call(_chart);
  111. nv.utils.windowResize(_chart.update);
  112. return _chart;
  113. }, function () {
  114. d3.selectAll(".nv-bar").on('click',
  115. function (d, i) {
  116. options.onClick(d);
  117. });
  118. });
  119. }