charts.ko.js 12 KB

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