charts.ko.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.23);
  85. $(element).css("position", "relative");
  86. var _options = valueAccessor();
  87. var _data = _options.transformer(_options.data);
  88. var _maxWeight = 0;
  89. $(_data).each(function(cnt, item){
  90. if (item.value > _maxWeight) _maxWeight = item.value;
  91. });
  92. var _chunk = _maxWeight / _data.length;
  93. var _mapdata = {};
  94. var _maphovers = {};
  95. var _fills = {};
  96. var _noncountries = [];
  97. if (_options.isScale){
  98. _fills["defaultFill"] = HueColors.LIGHT_BLUE;
  99. var _colors = HueColors.scale(HueColors.LIGHT_BLUE, HueColors.DARK_BLUE, _data.length);
  100. $(_colors).each(function(cnt, item){
  101. _fills["fill_" + cnt] = item;
  102. });
  103. $(_data).each(function(cnt, item){
  104. var _country = HueGeo.getCountryFromName(item.label);
  105. if (_country != null){
  106. _mapdata[_country.alpha3] = {
  107. fillKey: "fill_" + Math.floor(item.value/_chunk)
  108. };
  109. _maphovers[_country.name.split(",")[0].toLowerCase()] = item.value;
  110. }
  111. else {
  112. _noncountries.push(item);
  113. }
  114. });
  115. }
  116. else {
  117. _fills["defaultFill"] = HueColors.BLUE;
  118. _fills["selected"] = HueColors.DARK_BLUE;
  119. $(_data).each(function(cnt, item){
  120. var _country = HueGeo.getCountryFromName(item.label);
  121. if (_country != null){
  122. _mapdata[_country.alpha3] = {
  123. fillKey: "selected"
  124. };
  125. _maphovers[_country.name.split(",")[0].toLowerCase()] = item.value;
  126. }
  127. else {
  128. _noncountries.push(item);
  129. }
  130. });
  131. }
  132. var _map = new Datamap({
  133. element: element,
  134. fills: _fills,
  135. scope: 'world',
  136. data: _mapdata,
  137. done: function(datamap) {
  138. datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
  139. if (typeof _options.onClick != "undefined"){
  140. _options.onClick(geography);
  141. }
  142. });
  143. var _bubbles = [];
  144. if (_options.enableGeocoding){
  145. $(_noncountries).each(function(cnt, item){
  146. HueGeo.getCityCoordinates(item.label, function(lat, lng){
  147. _bubbles.push({
  148. fillKey: "selected",
  149. label: item.label,
  150. radius: 4,
  151. latitude: lat,
  152. longitude: lng
  153. });
  154. _map.bubbles(_bubbles, {
  155. popupTemplate: function(geo, data) {
  156. return '<div class="hoverinfo"><strong>' + data.label + '</strong></div>'
  157. }
  158. });
  159. });
  160. });
  161. }
  162. },
  163. geographyConfig: {
  164. hideAntarctica: true,
  165. borderWidth: 1,
  166. borderColor: HueColors.DARK_BLUE,
  167. highlightOnHover: true,
  168. highlightFillColor: HueColors.DARK_BLUE,
  169. highlightBorderColor: HueColors.DARK_BLUE,
  170. popupTemplate: function(geography, data) {
  171. var _hover = _maphovers[geography.properties.name.toLowerCase()];
  172. return '<div class="hoverinfo" style="text-align: center"><strong>' + geography.properties.name + '</strong>' + ((typeof _hover != "undefined")?'<br/>' + _hover : '') + '</div>'
  173. }
  174. }
  175. });
  176. _options.onComplete();
  177. nv.utils.windowResize(_map.update);
  178. $(element).parents(".card-widget").on("resize", function(){
  179. console.log("resize!");
  180. });
  181. },
  182. update: function (element, valueAccessor) {
  183. var value = valueAccessor();
  184. // do something with the updated value
  185. }
  186. };
  187. function lineChart(element, options) {
  188. var _datum = options.transformer(options.datum);
  189. $(element).height(300);
  190. nv.addGraph(function () {
  191. var _chart = nv.models.lineWithBrushChart();
  192. _chart.onSelectRange(options.onSelectRange);
  193. _chart.xAxis
  194. .showMaxMin(true)
  195. .tickFormat(d3.format(',0f'));
  196. _chart.margin({bottom: 100})
  197. .transitionDuration(300);
  198. _chart.yAxis
  199. .tickFormat(d3.format(',0f'));
  200. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  201. _d3.datum(_datum)
  202. .transition().duration(350)
  203. .each("end", options.onComplete)
  204. .call(_chart);
  205. nv.utils.windowResize(_chart.update);
  206. return _chart;
  207. }, function () {
  208. d3.selectAll(".nv-bar").on('click',
  209. function (d, i) {
  210. options.onClick(d);
  211. });
  212. });
  213. }
  214. function barChart(element, options, isTimeline) {
  215. var _datum = options.transformer(options.datum);
  216. $(element).height(300);
  217. nv.addGraph(function () {
  218. var _chart;
  219. if (isTimeline) {
  220. _chart = nv.models.multiBarWithBrushChart();
  221. if (_datum.length > 0 && _datum[0].values.length > 10){
  222. _chart.enableSelection();
  223. }
  224. _chart.onSelectRange(options.onSelectRange);
  225. _chart.xAxis.tickFormat(d3.time.format("%Y-%m-%d %H:%M:%S"));
  226. _chart.multibar.hideable(true);
  227. _chart.multibar.stacked(typeof options.stacked != "undefined" ? options.stacked : false);
  228. _chart.onStateChange(options.onStateChange);
  229. }
  230. else {
  231. var _isDiscrete = false;
  232. for (var j=0;j<_datum.length;j++){
  233. for (var i=0;i<_datum[j].values.length;i++){
  234. if (isNaN(_datum[j].values[i].x * 1)){
  235. _isDiscrete = true;
  236. break;
  237. }
  238. }
  239. }
  240. if (_isDiscrete){
  241. _chart = nv.models.growingDiscreteBarChart()
  242. .x(function(d) { return d.x })
  243. .y(function(d) { return d.y })
  244. .staggerLabels(true);
  245. }
  246. else {
  247. _chart = nv.models.growingMultiBarChart();
  248. _chart.xAxis
  249. .showMaxMin(true)
  250. .tickFormat(d3.format(',0f'));
  251. _chart.multibar.hideable(true);
  252. _chart.multibar.stacked(typeof options.stacked != "undefined" ? options.stacked : false);
  253. _chart.onStateChange(options.onStateChange);
  254. }
  255. }
  256. _chart.margin({bottom: 100})
  257. .transitionDuration(300);
  258. _chart.yAxis
  259. .tickFormat(d3.format(',0f'));
  260. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  261. _d3.datum(_datum)
  262. .transition().duration(350)
  263. .each("end", options.onComplete)
  264. .call(_chart);
  265. if (isTimeline) {
  266. var insertLinebreaks = function (d) {
  267. var _el = d3.select(this);
  268. var _mom = moment(d);
  269. if (_mom != null) {
  270. var _words = _mom.format("hh:mm:ss YYYY-MM-DD").split(" ");
  271. _el.text('');
  272. for (var i = 0; i < _words.length; i++) {
  273. var tspan = _el.append("tspan").text(_words[i]);
  274. if (i > 0) {
  275. tspan.attr("x", 0).attr("dy", '15');
  276. }
  277. }
  278. }
  279. };
  280. _d3.selectAll("g.nv-x.nv-axis g text").each(insertLinebreaks);
  281. }
  282. nv.utils.windowResize(_chart.update);
  283. return _chart;
  284. }, function () {
  285. d3.selectAll(".nv-bar").on('click',
  286. function (d, i) {
  287. options.onClick(d);
  288. });
  289. });
  290. }