BarChart.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: SHARE
  5. *
  6. * Copyright IBM Corp. 2015, 2016
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. */
  10. define([
  11. "q",
  12. 'jquery',
  13. 'rave2',
  14. 'bi/sharecommon/utils/translator',
  15. 'bi/schedule/utils/charts/ChartControl'
  16. ],
  17. function(Q, $, rave, t, ChartControl) {
  18. 'use strict';
  19. var BarChart = ChartControl.extend({
  20. init: function(options) {
  21. this.textOffset = 15;
  22. this.duration = 500;
  23. this.bars = null;
  24. // This must be called last as it calls the _draw function below
  25. BarChart.inherited('init', this, arguments);
  26. },
  27. _draw: function(data) {
  28. var chartDesc = t.translate("subscribe_graph_description", {enabled: data[0].value, disabled: data[1].value});
  29. this.chart.attr("aria-label", chartDesc);
  30. this.chart.attr("role", "presentation");
  31. var maxWidth = Math.max(15, rave.max(data, function(row){ return row.value;}) +2);
  32. var x = rave.scale.linear()
  33. .domain([0, maxWidth])
  34. .range([0, this.width]);
  35. var y = rave.scale.ordinal()
  36. .domain([
  37. t.translate("subscribe_enabled_axis_title"),
  38. t.translate("subscribe_disabled_axis_title"), ""
  39. ])
  40. .rangePoints([0, this.height]);
  41. var xAxis = rave.svg.axis().scale(x).orient('bottom');
  42. var yAxis = rave.svg.axis().scale(y).tickSize(0).orient('left');
  43. this.bar = this.chart.selectAll("g")
  44. .data(data).enter().append("g")
  45. .attr("class", function(row) { return 'bar_'+row.name; })
  46. .style("fill", function(row) {
  47. return this._colours(row.name)
  48. }.bind(this))
  49. .attr("transform", function(d, i) {
  50. return "translate(" + this.margin.left + ", " + (i * this.barHeight + this.margin.top) + ")";
  51. }.bind(this));
  52. var rs = this.bar.append("rect")
  53. .attr("width", function(row) { return x(row.value); })
  54. .attr("height", this.barHeight - this.barSeparation)
  55. .on("mouseover", function() {
  56. var rect = rave.select(this);
  57. var colour = rect.style("fill") || "#ddd";
  58. var rgb = rave.rgb(colour).darker();
  59. rect.style("fill", rgb.toString());
  60. })
  61. .on("mouseout", function() {
  62. rave.select(this).style("fill", null);
  63. });
  64. this.bar.append("text")
  65. .attr("x", this.textOffset)
  66. .attr("y", this.barHeight / 2 - 3)
  67. .attr("dy", ".35em")
  68. .attr("class", "innerbar")
  69. .text(function(row) { return row.value; })
  70. .attr("x", function(row) { return x(row.value) + this.textOffset; }.bind(this));
  71. // append x axes
  72. this.chart.append("g")
  73. .attr("class", "x axis")
  74. .attr("transform", "translate(" + this.margin.left + "," + (this.height + this.barSeparation + 5) + ")")
  75. .call(xAxis);
  76. // Add the y-axis.
  77. this.chart.append("g")
  78. .attr("class", "y axis")
  79. .attr("transform", "translate(" + this.margin.left + "," + (this.margin.top - 5) + ")")
  80. .call(yAxis)
  81. .selectAll("text")
  82. .attr("y", this.barHeight / 2 -3)
  83. .attr("x", -10);
  84. }
  85. });
  86. return BarChart;
  87. });