charts.ko.js 12 KB

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