charts.ko.js 11 KB

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