Source: drawSBCAxis.js

  1. // source: http://jsfiddle.net/2khbceut/1/
  2. // datajb = data from 1 club from 1 year in JSON format
  3. // max_axis_value = the x axis maximum value
  4. // pos = ranking of the club
  5. /**
  6. * Renders the stacked bar chart axis
  7. * @param datajb data object to render containing the data of one club of one year
  8. * @param max_axis_value max absolute value for the stacked bar chart
  9. * @param drawid id of the element in which to draw the stacked bar chart
  10. */
  11. function buildgraphicAxis(datajb, max_axis_value, drawid) {
  12. var svg = d3.select(drawid),
  13. margin = {top: 0, right: 0, bottom: 0, left: 0},
  14. width = svg.attr("width") - margin.left - margin.right,
  15. height = svg.attr("height") - margin.top - margin.bottom;
  16. var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  17. var i = 0;
  18. //var y = d3.scale.ordinal().rangeRoundBands([0, height], .3);
  19. //var y = d3.scaleBand().rangeRound([0, height]).padding(0.3);
  20. // var y = d3.scaleOrdinal().range([0, height], .3);
  21. var x = d3.scaleLinear().range([0, width]);
  22. var color = d3.scaleOrdinal()
  23. .range([colors.kl, colors.ll, colors.ls, colors.sl, colors.ss, colors.ss, colors.sl, colors.ls, colors.ll, colors.kl]);
  24. // documentation axis: https://github.com/d3/d3-axis
  25. // https://stackoverflow.com/questions/13669239/remove-end-ticks-from-d3-js-axis#28176254
  26. var xAxis = d3.axisTop().scale(x).ticks(5).tickSizeOuter(0);
  27. console.log(xAxis);
  28. // var yAxis = d3.axisLeft().scale(y);
  29. //console.log(drawid);
  30. /*
  31. var svg = d3.select(drawid).append("svg")
  32. .attr("width", width + margin.left + margin.right)
  33. .attr("height", height + margin.top + margin.bottom)
  34. .attr("id", "d3-plot")
  35. .append("g")
  36. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  37. */
  38. color.domain(["ENKL", "ENLL", "ENLS", "ENSL", "ENSS", "ESSS", "ESSL", "ESLS", "ESLL", "ESKL"]);
  39. var datajbEin = [datajb];
  40. /*
  41. console.log("StartdatajbEin");
  42. console.log(datajbEin);
  43. console.log("EnddatajbEin");
  44. */
  45. // TODO schleife wird immer nur einmal ausgefuehrt
  46. datajbEin.forEach(function (d) {
  47. if (drawid.includes("_2012_10")) {
  48. //max_axis_value = 71;
  49. //console.log(drawid + " max: " + max_axis_value);
  50. var essum = parseInt(d["ESSS"]) + parseInt(d["ESLS"]) + parseInt(d["ESSL"]) + parseInt(d["ESLL"]) + parseInt(d["ESKL"]);
  51. var ensum = parseInt(d["ENSS"]) + parseInt(d["ENLS"]) + parseInt(d["ENSL"]) + parseInt(d["ENLL"]) + parseInt(d["ENKL"]);
  52. //console.log("ESSS " + d["ESSS"] + " ESLS " + d["ESLS"] + " ESSL " + d["ESSL"] + " ESLL " + d["ESLL"] + " ESKL " + d["ESKL"] + " Summe " + essum + " ES " + d["ES"]);
  53. //console.log("ENSS " + d["ENSS"] + " ENLS " + d["ENLS"] + " ENSL " + d["ENSL"] + " ENLL " + d["ENLL"] + " ESKL " + d["ESKL"] + " Summe " + ensum + " EN " + d["EN"]);
  54. }
  55. //console.log("Frankreich");
  56. //console.log(d);
  57. // calc percentages
  58. var len = d["ES"] + d["EN"];
  59. d["ENSS"] = +d["ENSS"];
  60. d["ENSL"] = +d["ENSL"];
  61. d["ENLS"] = +d["ENLS"];
  62. d["ENLL"] = +d["ENLL"];
  63. d["ENKL"] = +d["ENKL"];
  64. d["ESLL"] = +d["ESLL"];
  65. d["ESLS"] = +d["ESLS"];
  66. d["ESSL"] = +d["ESSL"];
  67. d["ESSS"] = +d["ESSS"];
  68. d["ESKL"] = +d["ESKL"];
  69. // TODO edit here
  70. var x0 = -1 * d["EN"];
  71. var idx = 0;
  72. d.boxes = color.domain().map(function (name) {
  73. return {name: name, x0: x0, x1: x0 += +d[name]};
  74. });
  75. });
  76. var min_val = d3.min(datajbEin, function (d) {
  77. return (-1) * max_axis_value;
  78. });
  79. var max_val = d3.max(datajbEin, function (d) {
  80. return max_axis_value;
  81. });
  82. x.domain([min_val, max_val]);
  83. /* y.domain(datajbEin.map(function (d) {
  84. return d.name;
  85. }));*/
  86. x.domain([min_val, max_val]);
  87. svg.append("g")
  88. .attr("class", "x axis")
  89. .attr("transform", "translate(0,20)")
  90. .attr("width", 5)
  91. .call(xAxis);
  92. /*svg.append("g")
  93. .attr("class", "y axis")
  94. .call(yAxis); // */
  95. var vakken = svg.selectAll(".name")
  96. .data(datajbEin)
  97. .enter().append("g")
  98. .attr("class", "bar")
  99. /*
  100. .attr("transform", function (d) {
  101. return "translate(0," + " " + ")";
  102. })//*/;
  103. //console.log(vakken);
  104. var bars = vakken.selectAll("rect")
  105. .data(function (d) {
  106. return d.boxes;
  107. })
  108. .enter().append("g").attr("class", "subbar"); // */
  109. /*
  110. bars.append("rect")
  111. .attr("height", 0) // y.rangeBand()/2
  112. .attr("stroke-width", function (d) {
  113. return 0;
  114. })
  115. .attr("x", function (d) {
  116. if (isNaN(d.x0)) {
  117. console.log("x" + i++);
  118. console.log(d);
  119. }
  120. return x(d.x0);
  121. })
  122. .attr("width", function (d) {
  123. if (isNaN(x(d.x0)) || isNaN(x(d.x1))) {
  124. console.log("width" + i++);
  125. console.log(d);
  126. }
  127. return x(d.x1) - x(d.x0);
  128. })
  129. .style("fill", function (d) {
  130. return color(d.name);
  131. }); // */
  132. //console.log(bars);
  133. // Beschriftung // remove Numbers here
  134. bars.append("text")
  135. .attr("x", function (d) {
  136. return x(d.x0);
  137. })
  138. .attr("y", height / 2) // rangeBand()/2
  139. .attr("dy", "0.5em")
  140. .attr("dx", "0.5em")
  141. .style("font", "10px sans-serif")
  142. .style("text-anchor", "begin")
  143. .text(function (d) {
  144. return d.n !== 0 && (d.x1 - d.x0) > 3 ? d.n : ""
  145. });
  146. // this is not nice, we should calculate the bounding box and use that
  147. var movesize = width / 2 - width / 2;//startp.node().getBBox().width/2;
  148. d3.selectAll(".legendbox").attr("transform", "translate(" + movesize + ",0)");
  149. }