charts.ko.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. $(element).css("marginLeft", "auto");
  21. $(element).css("marginRight", "auto");
  22. if (typeof _options.maxWidth != "undefined"){
  23. var _max = _options.maxWidth*1;
  24. $(element).width(Math.min($(element).parent().width(), _max));
  25. }
  26. nv.addGraph(function () {
  27. var _chart = nv.models.growingPieChart()
  28. .x(function (d) {
  29. return d.label
  30. })
  31. .y(function (d) {
  32. return d.value
  33. })
  34. .height($(element).width())
  35. .showLabels(true).showLegend(false);
  36. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  37. _d3.datum(_data)
  38. .transition().duration(150)
  39. .each("end", _options.onComplete)
  40. .call(_chart);
  41. $.each(_options.fqs(), function(cnt, item){
  42. if (item.field() == _options.field()){
  43. _chart.selectSlices(item.filter());
  44. }
  45. });
  46. nv.utils.windowResize(_chart.update);
  47. $(element).height($(element).width());
  48. $(element).parents(".card-widget").on("resize", function(){
  49. if (typeof _options.maxWidth != "undefined"){
  50. var _max = _options.maxWidth*1;
  51. $(element).width(Math.min($(element).parent().width(), _max));
  52. }
  53. $(element).height($(element).width());
  54. _chart.update();
  55. });
  56. return _chart;
  57. }, function () {
  58. d3.selectAll(".nv-slice").on('click',
  59. function (d, i) {
  60. _options.onClick(d);
  61. });
  62. });
  63. }
  64. };
  65. ko.bindingHandlers.barChart = {
  66. init: function (element, valueAccessor) {
  67. barChart(element, valueAccessor(), false);
  68. }
  69. };
  70. ko.bindingHandlers.timelineChart = {
  71. init: function (element, valueAccessor) {
  72. barChart(element, valueAccessor(), true);
  73. }
  74. };
  75. ko.bindingHandlers.lineChart = {
  76. init: function (element, valueAccessor) {
  77. lineChart(element, valueAccessor());
  78. }
  79. };
  80. ko.bindingHandlers.mapChart = {
  81. render: function (element, valueAccessor) {
  82. var _options = valueAccessor();
  83. $(element).empty();
  84. $(element).css("position", "relative");
  85. $(element).css("marginLeft", "auto");
  86. $(element).css("marginRight", "auto");
  87. if (typeof _options.maxWidth != "undefined"){
  88. var _max = _options.maxWidth*1;
  89. $(element).width(Math.min($(element).parent().width(), _max));
  90. }
  91. $(element).height($(element).width() / 2.23);
  92. var _scope = typeof _options.data.scope != "undefined" ? String(_options.data.scope) : "world";
  93. var _data = _options.transformer(_options.data);
  94. var _maxWeight = 0;
  95. $(_data).each(function(cnt, item) {
  96. if (item.value > _maxWeight) _maxWeight = item.value;
  97. });
  98. var _chunk = _maxWeight / _data.length;
  99. var _mapdata = {};
  100. var _maphovers = {};
  101. var _fills = {};
  102. var _noncountries = [];
  103. if (_options.isScale) {
  104. _fills["defaultFill"] = HueColors.WHITE;
  105. var _colors = HueColors.scale(HueColors.LIGHT_BLUE, HueColors.DARK_BLUE, _data.length);
  106. $(_colors).each(function(cnt, item) {
  107. _fills["fill_" + cnt] = item;
  108. });
  109. $(_data).each(function(cnt, item) {
  110. var _place = item.label.toUpperCase();
  111. if (_place != null){
  112. _mapdata[_place] = {
  113. fillKey: "fill_" + (Math.floor(item.value / _chunk) - 1),
  114. id: _place,
  115. cat: item.obj.cat,
  116. value: item.obj.value,
  117. selected: item.obj.selected
  118. };
  119. _maphovers[_place] = item.value;
  120. }
  121. else {
  122. _noncountries.push(item);
  123. }
  124. });
  125. }
  126. else {
  127. _fills["defaultFill"] = HueColors.LIGHT_BLUE;
  128. _fills["selected"] = HueColors.DARK_BLUE;
  129. $(_data).each(function(cnt, item) {
  130. var _place = item.label.toUpperCase();
  131. if (_place != null){
  132. _mapdata[_place] = {
  133. fillKey: "selected",
  134. id: _place,
  135. cat: item.obj.cat,
  136. value: item.obj.value,
  137. selected: item.obj.selected
  138. };
  139. _maphovers[_place] = item.value;
  140. }
  141. else {
  142. _noncountries.push(item);
  143. }
  144. });
  145. }
  146. var _map = null;
  147. function createDatamap(element, options, fills, mapData, nonCountries, mapHovers) {
  148. _map = new Datamap({
  149. element: element,
  150. fills: fills,
  151. scope: _scope,
  152. data: mapData,
  153. onClick: function(data) {
  154. if (typeof options.onClick != "undefined") {
  155. options.onClick(data);
  156. }
  157. },
  158. done: function(datamap) {
  159. var _bubbles = [];
  160. if (options.enableGeocoding) {
  161. $(nonCountries).each(function(cnt, item){
  162. HueGeo.getCityCoordinates(item.label, function(lat, lng){
  163. _bubbles.push({
  164. fillKey: "selected",
  165. label: item.label,
  166. value: item.value,
  167. radius: 4,
  168. latitude: lat,
  169. longitude: lng
  170. });
  171. _map.bubbles(_bubbles, {
  172. popupTemplate: function(geo, data) {
  173. return '<div class="hoverinfo" style="text-align: center"><strong>' + data.label + '</strong><br/>' + item.value + '</div>'
  174. }
  175. });
  176. });
  177. });
  178. }
  179. },
  180. geographyConfig: {
  181. hideAntarctica: true,
  182. borderWidth: 1,
  183. borderColor: HueColors.DARK_BLUE,
  184. highlightOnHover: true,
  185. highlightFillColor: HueColors.DARK_BLUE,
  186. highlightBorderColor: HueColors.BLUE,
  187. selectedFillColor: HueColors.DARKER_BLUE,
  188. selectedBorderColor: HueColors.DARKER_BLUE,
  189. popupTemplate: function(geography, data) {
  190. var _hover = '';
  191. if (data != null) {
  192. _hover = '<br/>' + mapHovers[data.id];
  193. }
  194. return '<div class="hoverinfo" style="text-align: center"><strong>' + geography.properties.name + '</strong>' + _hover + '</div>'
  195. }
  196. }
  197. });
  198. options.onComplete();
  199. }
  200. createDatamap(element, _options, _fills, _mapdata, _noncountries, _maphovers)
  201. $(element).parents(".card-widget").one("resize", function(){
  202. ko.bindingHandlers.mapChart.render(element, valueAccessor);
  203. });
  204. },
  205. init: function (element, valueAccessor) {
  206. ko.bindingHandlers.mapChart.render(element, valueAccessor)
  207. },
  208. update: function (element, valueAccessor) {
  209. ko.bindingHandlers.mapChart.render(element, valueAccessor);
  210. }
  211. };
  212. function lineChart(element, options) {
  213. var _datum = options.transformer(options.datum);
  214. $(element).height(300);
  215. nv.addGraph(function () {
  216. var _chart = nv.models.lineWithBrushChart();
  217. if (_datum.length > 0 && _datum[0].values.length > 10){
  218. _chart.enableSelection();
  219. }
  220. _chart.onSelectRange(options.onSelectRange);
  221. _chart.xAxis.showMaxMin(false);
  222. _chart.yAxis
  223. .tickFormat(d3.format(',0f'));
  224. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  225. _d3.datum(_datum)
  226. .transition().duration(150)
  227. .each("end", options.onComplete)
  228. .call(_chart);
  229. nv.utils.windowResize(_chart.update);
  230. return _chart;
  231. }, function () {
  232. d3.selectAll(".nv-bar").on('click',
  233. function (d, i) {
  234. options.onClick(d);
  235. });
  236. });
  237. }
  238. function barChart(element, options, isTimeline) {
  239. var _datum = options.transformer(options.datum);
  240. $(element).height(300);
  241. nv.addGraph(function () {
  242. var _chart;
  243. var insertLinebreaks = function (d) {
  244. var _el = d3.select(this);
  245. var _mom = moment(d);
  246. if (_mom != null) {
  247. var _words = _mom.format("hh:mm:ss YYYY-MM-DD").split(" ");
  248. _el.text('');
  249. for (var i = 0; i < _words.length; i++) {
  250. var tspan = _el.append("tspan").text(_words[i]);
  251. if (i > 0) {
  252. tspan.attr("x", 0).attr("dy", '15');
  253. }
  254. }
  255. }
  256. };
  257. if (isTimeline) {
  258. _chart = nv.models.multiBarWithBrushChart();
  259. if (_datum.length > 0 && _datum[0].values.length > 10){
  260. _chart.enableSelection();
  261. }
  262. _chart.onSelectRange(options.onSelectRange);
  263. _chart.xAxis.tickFormat(d3.time.format("%Y-%m-%d %H:%M:%S"));
  264. _chart.multibar.hideable(true);
  265. _chart.multibar.stacked(typeof options.stacked != "undefined" ? options.stacked : false);
  266. _chart.onStateChange(options.onStateChange);
  267. _chart.onChartUpdate(function(){
  268. _d3.selectAll("g.nv-x.nv-axis g text").each(insertLinebreaks);
  269. });
  270. }
  271. else {
  272. var _isDiscrete = false;
  273. for (var j=0;j<_datum.length;j++){
  274. for (var i=0;i<_datum[j].values.length;i++){
  275. if (isNaN(_datum[j].values[i].x * 1)){
  276. _isDiscrete = true;
  277. break;
  278. }
  279. }
  280. }
  281. if (_isDiscrete){
  282. _chart = nv.models.growingDiscreteBarChart()
  283. .x(function(d) { return d.x })
  284. .y(function(d) { return d.y })
  285. .staggerLabels(true);
  286. }
  287. else {
  288. _chart = nv.models.multiBarWithBrushChart();
  289. if (_datum.length > 0 && _datum[0].values.length > 10){
  290. _chart.enableSelection();
  291. }
  292. _chart.xAxis.showMaxMin(false).tickFormat(d3.format(',0f'));
  293. _chart.multibar.hideable(true);
  294. _chart.multibar.stacked(typeof options.stacked != "undefined" ? options.stacked : false);
  295. _chart.onStateChange(options.onStateChange);
  296. _chart.onSelectRange(options.onSelectRange);
  297. }
  298. }
  299. _chart.transitionDuration(0);
  300. _chart.yAxis
  301. .tickFormat(d3.format(',0f'));
  302. var _d3 = ($(element).find('svg').length > 0) ? d3.select($(element).find('svg')[0]) : d3.select($(element)[0]).append('svg');
  303. _d3.datum(_datum)
  304. .transition().duration(150)
  305. .each("end", function(){
  306. options.onComplete();
  307. if (isTimeline) {
  308. _d3.selectAll("g.nv-x.nv-axis g text").each(insertLinebreaks);
  309. }
  310. }).call(_chart);
  311. nv.utils.windowResize(_chart.update);
  312. return _chart;
  313. }, function () {
  314. d3.selectAll(".nv-bar").on("click",
  315. function (d, i) {
  316. options.onClick(d);
  317. });
  318. });
  319. }