使现有垂直条形图水平

时间:2018-05-21 10:30:02

标签: d3.js

我有一个现有的条形图,工作正常。

但我想通过翻转轴将其设为水平。

完整代码:

$(document).ready(function() {
  render_chart();
});

function render_chart() {
  var stack = d3.stack;
  var dataset = {
    "categories": ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
    "series": ["Group1", "Group2", "Group3"],
    "colors": ["#3498db", "#e74c3c", "#2ecc71"],
    "layers": [
      [{
          "y": 1,
          "y0": 20,
          "month": "Monday"
        },
        {
          "y": 2,
          "y0": 18,
          "month": "Tuesday"
        },
        {
          "y": 5,
          "y0": 18,
          "month": "Wednesday"
        },
        {
          "y": 10,
          "y0": 20,
          "month": "Thursday"
        },
        {
          "y": 14,
          "y0": 23,
          "month": "Friday"
        },
        {
          "y": 18,
          "y0": 24,
          "month": "Saturday"
        },
        {
          "y": 20,
          "y0": 24,
          "month": "Sunday"
        }
      ],
      [{
          "y": 12,
          "y0": 24,
          "month": "Monday"
        },
        {
          "y": 14,
          "y0": 24,
          "month": "Tuesday"
        },
        {
          "y": 13,
          "y0": 23,
          "month": "Wednesday"
        },
        {
          "y": 16,
          "y0": 21,
          "month": "Thursday"
        },
        {
          "y": 18,
          "y0": 23,
          "month": "Friday"
        },
        {
          "y": 19,
          "y0": 22,
          "month": "Saturday"
        },
        {
          "y": 20,
          "y0": 22,
          "month": "Sunday"
        }
      ],
      [{
          "y": 8,
          "y0": 24,
          "month": "Monday"
        },
        {
          "y": 14,
          "y0": 21,
          "month": "Tuesday"
        },
        {
          "y": 12,
          "y0": 19,
          "month": "Wednesday"
        },
        {
          "y": 15,
          "y0": 23,
          "month": "Thursday"
        },
        {
          "y": 18,
          "y0": 21,
          "month": "Friday"
        },
        {
          "y": 16,
          "y0": 23,
          "month": "Saturday"
        },
        {
          "y": 17,
          "y0": 24,
          "month": "Sunday"
        }
      ]
    ]
  };

  n = dataset["series"].length;
  m = dataset["layers"].length;

  yGroupMax = d3.max(dataset["layers"], function(layer) {
    return d3.max(layer, function(d) {
      return d.y0;
    });
  });
  yGroupMin = d3.min(dataset["layers"], function(layer) {
    return d3.min(layer, function(d) {
      return d.y;
    });
  });

  var margin = {
      top: 50,
      right: 50,
      bottom: 50,
      left: 100
    },
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

  var xScale = d3.scaleBand()
    .domain(dataset["categories"])
    .rangeRound([0, width])
    .padding(.08);

  var yScale = d3.scaleLinear()
    .domain([yGroupMin, yGroupMax])
    .range([height, 0]);

  var xAxis = d3.axisBottom(xScale)
    .tickSize(7)
    .tickPadding(6);

  var yAxis = d3.axisLeft(yScale).ticks(24);

  var svg = d3.select("#groupchart").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 + ")");

  var layer = svg.selectAll(".layer")
    .data(dataset["layers"])
    .enter().append("g")
    .attr("class", "layer");

  var rect = layer.selectAll("rect")
    .data(function(d, i) {
      d.map(function(b) {
        b.colorIndex = i;
        return b;
      });
      return d;
    })
    .enter()
    .append("rect")
    .transition()
    .duration(500)
    .delay(function(d, i) {
      return i * 10;
    })
    .attr("x", function(d, i, j) {
      var k = Array.prototype.indexOf.call(j[i].parentNode.parentNode.childNodes, j[i].parentNode);
      return xScale(d.month) + xScale.bandwidth() / n * k;
    })
    .attr("width", xScale.bandwidth() / n)
    .transition()
    .attr("y", function(d) {
      return yScale(d.y0);
    })
    .attr("height", function(d) {
      return height - yScale(d.y0 - d.y)
    })
    .attr("class", "bar")
    .style("fill", function(d) {
      return dataset["colors"][d.colorIndex];
    });

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

  svg.select("g")
    .attr("class", "y axis")
    .call(yAxis);

  svg.append("text")
    .attr("x", width / 3)
    .attr("y", 0)
    .attr("dx", ".71em")
    .attr("dy", "-.71em")
    .text("Grouped Bar Chart Test");

  var legend = svg.append("g")
    .attr("class", "legend")

  legend.selectAll('text')
    .data(dataset["colors"])
    .enter()
    .append("rect")
    .attr("x", function(d, i) {
      return (i * 120) + (width / 3);
    })
    .attr("y", (width - margin.right) / 1.6)
    .attr("width", 10)
    .attr("height", 10)
    .style("fill", function(d) {
      return d;
    })

  legend.selectAll('text')
    .data(dataset["series"])
    .enter()
    .append("text")
    .attr("x", function(d, i) {
      return (i * 120) + (width / 3) + 12;
    })
    .attr("y", ((width - margin.right) / 1.6) + 9)
    .text(function(d) {
      return d;
    });

  var tooltip = d3.select("body")
    .append('div')
    .attr('class', 'tooltip');

  tooltip.append('div')
    .attr('class', 'month');
  tooltip.append('div')
    .attr('class', 'tempRange');

  svg.selectAll("rect")
    .on('mouseover', function(d) {
      if (!d.month) return null;

      tooltip.select('.month').html("<b>" + d.month + "</b>");
      tooltip.select('.tempRange').html(d.y + ":00 Hours to " + d.y0 + ":00 Hours");

      tooltip.style('display', 'block');
      tooltip.style('opacity', 2);

    })
    .on('mousemove', function(d) {

      if (!d.month) return null;

      tooltip.style('top', (d3.event.layerY + 10) + 'px')
        .style('left', (d3.event.layerX - 25) + 'px');
    })
    .on('mouseout', function() {
      tooltip.style('display', 'none');
      tooltip.style('opacity', 0);
    });

}
    .axis path,
    .axis line {
      fill: none;
      font: 10px sans-serif;
      stroke: #000;
      shape-rendering: crispEdges;
    }

    .legend {
        padding: 5px;
        font-size: 15px;
        font-family: 'Roboto', sans-serif;
        background: yellow;
        box-shadow: 2px 2px 1px #888;
    }

    .tooltip {
        background: #eee;
        box-shadow: 0 0 5px #999999;
        color: #333;
        font-size: 12px;
        left: 130px;
        padding: 10px;
        position: absolute;
        text-align: center;
        top: 95px;
        z-index: 10;
        display: block;
        opacity: 0;
    }
<!DOCTYPE html>
<html>
	<head>
    <meta charset="UTF-8">
		<title>Grouped Bar Graph</title>

    <script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
	</head>
	<body>
    <div id="groupchart" class="chart"></div>
	</body>
</html>

我试图这样做,但我每次都把它搞砸了。当我试图通过改变下面刚刚反转x和y的部分来翻转轴时,它没有正确显示:

var xAxis = d3.axisBottom(xScale)
    .tickSize(7)
    .tickPadding(6);

 var yAxis = d3.axisLeft(yScale).ticks(24);

我无法弄清楚这一点。

为什么

因此,当它们处于水平状态时,我可以在每组条形图中放入更多的东西,因为它们会很长并且内部空间更大。

0 个答案:

没有答案