charts.ko.js 12 KB

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