如何在分组条形图的每个条上旋转文本?

时间:2016-03-24 20:53:32

标签: d3.js

我有一个分组的条形图,我正在旋转x轴文本。但是x轴文本未在条形下正确对齐。如何使x轴文本与特定条形对齐?

我认为x轴文本旋转变换属性出了问题。

以下是代码:

html

 <!DOCTYPE html>
 <script src="http://d3js.org/d3.v3.min.js"></script>
 <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js">
 </script>
 <body>
   <div id="chartdiv" style="height: 500px; width: 600px;">                                </div>
 </body>

这里是javascript

var data = [
  {
    "PAG": "FACT",
    "Projects": "3",
    "High Level Design": "1",
    "Business Requirements": "1",
    "Change Impact Document": "2",
    "MAC Reviews": "3"
  }
  ,
  {
    "PAG": "Advisory Platforms & Solutions",
    "Projects": "1",                                                              "MAC Reviews": "1"
  },
  {
    "PAG": "Capital Markets",
    "Projects": "2"                                                                                                                                          },
    {
      "PAG": "Field Management Home Office",
      "Projects": "3"

    },
    {
      "PAG": "Institutional Wealth Services",
      "Projects": "1",
      "Business Requirements": "1",
      "Change Impact Document": "1"

    },
    {
      "PAG": "Traditional Invest Products",
      "Projects": "2"
    },
    {
      "PAG": "WM Operations",
      "Projects": "2",
      "High Level Design": "2",
      "Low Level Design": "1"
    }
  ];
  function keysLen(obj) {
    count = 0;
    index = 0;
    obj.map(function (d, i) {
      if (d3.keys(d).length > count) {
        count = d3.keys(d).length;
        index = i
      }
    })
    return obj[index];
  }
  var margin = { top: 20, right: 20, bottom: 30, left: 40 },
  width = parseInt(d3.select("#chartdiv").style("width")) - margin.left - margin.right,
  height = parseInt(d3.select("#chartdiv").style("height")) - margin.top - margin.bottom,
  padding = 100;

  //var border = 1; var bordercolor = 'black';

  var x0 = d3.scale.ordinal().domain(data.map(function (d) { return d.PAG; }))
  .rangeRoundBands([0, width - padding], .1);

  //var x0 = d3.scale.ordinal()
  //    .rangeRoundBands([0, width], .1);

  var x1 = d3.scale.ordinal();

  var y = d3.scale.linear().range([height, 0]);

  var ageNames = d3.keys(keysLen(data)).filter(function (d) {
    return d != "PAG"
  });

  var p = d3.scale.category20();
  var r = p.range(); // ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
  var color = d3.scale.ordinal().range(r);

  var xAxis = d3.svg.axis()
  .scale(x0)
  .tickSize(0)
  //.tickPadding(8)
  .orient("bottom");


  var yAxis = d3.svg.axis()
  .scale(y)
  .orient("left")
  .tickFormat(d3.format(".0d"));

  //add tooltip------------------------------------------------
  var tooltip = d3.select("#chartdiv")
  .append("div")
  .attr("class", "d3-tip")
  .style("position", "absolute")
  .style("opacity", 0);


  var svg = d3.select("#chartdiv").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
  .append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  data.forEach(function (d) {
    d.ages = ageNames.map(function (name) {
      return { name: name, value: +d[name] };
    });
  });
  x0.domain(data.map(function (d) { return d.PAG; }));
  //x0.domain(data.map(function (d) { return d.PAG; })).rangeRoundBands([0, width - padding], .1);
  x1.domain(ageNames).rangeRoundBands([0, x0.rangeBand()]);
  y.domain([0, d3.max(data, function (d) { return d3.max(d.ages, function (d) { return d.value; }); })]).nice().range([height - padding, 0]);

  var tip = d3.tip()
  .attr('class', 'd3-tip')
  .offset([-10, 0])
  .html(data, function (d) {
    return "<strong>Total Count:</strong> <span style='color:#1F497D'>" + d.ages.value + "</span>";
  })

  //svg.call(tip);


  var gXAxis = svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + (height - padding) + ")")
  .call(xAxis);


  gXAxis.selectAll("text")
  .style("text-anchor", "end")
  .attr("dx", "-.8em")
  .attr("dy", ".15em")
  .attr("transform", "rotate(-30)");

  // xAxis label
  svg.append("text")
  .attr("transform", "translate(" + (width / 2) + " ," + (height + margin.bottom - 5) + ")")
  .style("text-anchor", "middle")
  .attr("class", "subtitle")
  .text("Process Area Group");

  svg.append("g")
  .attr("class", "y axis")
  .call(yAxis)
  .append("text")
  .attr("transform", "rotate(-90)")
  .attr("x", -(height - padding) / 2)
  .attr("y", 0 - margin.left)
  //.attr("y", -margin.bottom)
  .attr("dy", "1em")
  .style("text-anchor", "middle")
  .text("Artifacts")
  .attr("class", "subtitle");

  var state = svg.selectAll(".PAG")
  .data(data)
  .enter().append("g")
  .attr("class", "PAG")
  .attr("transform", function (d) { return "translate(" + x0(d.PAG) + ",0)"; });

  state.selectAll("rect")
  .data(function (d) { return d.ages; })
  .enter().append("rect")
  .attr("width", x1.rangeBand())
  .attr("x", function (d) { return x1(d.name); })
  .attr("y", function (d) { return y(d.value); })
  .attr("height", function (d) { return height - padding - y(d.value); })
  .style("fill", function (d) { return color(d.name); })
  .on("mouseover", function (d) {
    var pos = d3.mouse(this);
    tooltip
    .transition()
    .duration(500)
    .style("opacity", 1);
    tooltip.html('<strong>' + d.name + ':' + '</strong>' + '<span style=color:#1F497D>' + d.value + '</span>')
    .style("left", d3.event.x + "px")
    .style("top", d3.event.y + "px")
    //.text(d.name + ":" + d.value);

  })
  .on("mouseout", function () {
    tooltip.style("opacity", 0);
  });

  //Adding Legend for the Grouped Bar Chart

  var legend = svg.selectAll(".legend")
  .data(ageNames.slice().reverse())
  .enter().append("g")
  .attr("class", "legend")
  .attr("transform", function (d, i) { return "translate(0," + i * 20 + ")"; });

  legend.append("rect")
  .attr("x", width - 18)
  .attr("width", 15)
  .attr("height", 15)
  .style("fill", color);

  legend.append("text")
  .attr("x", width - 24)
  .attr("y", 9)
  .attr("dy", ".35em")
  .style("text-anchor", "end")
  .text(function (d) { return d; });

  svg.append("text")
  .text("Project Artifacts By PAG")
  .attr("x", width / 2)
  .attr("y", 0 - (margin.top / 2))
  .attr("text-anchor", "middle")
  .style("text-decoration", "underline")
  .attr("class", "title");

请为http://jsfiddle.net/qAHC2/1246/

找到下面提到的工作jsfiddle

0 个答案:

没有答案