簡體   English   中英

D3分層條形圖-更改X和Y軸值

[英]D3 Hierarchical Bar Chart - Change X and Y axis values

當前,對於D3分層條形圖,每個藍色條形圖代表一個文件夾,該文件夾的長度編碼該文件夾中所有文件(以及所有子文件夾)的總大小。 我希望藍色條應該編碼總編號的長度 (而不是大小)。 里面的物品。 例如,在下面的示例中,如果我單擊“ vis”,則其中有7個項目。 因此,“ vis”欄的長度應為7。所有文件夾中的所有項目都類似。 這是鏈接-https://bl.ocks.org/mbostock/1283663

 <!DOCTYPE html>
<html>
<head>
<style>

text {
  font: 10px sans-serif;
}

rect.background {
  fill: white;
}

.axis {
  shape-rendering: crispEdges;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
}

</style>
<title>D3</title>
</head>
<body>
 <script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 30, right: 30, bottom: 40, left: 50},
    width =  window.innerWidth - margin.left - margin.right-100,
    height =  window.innerHeight - margin.top - margin.bottom-100;

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

var barHeight = 20;

var color = d3.scale.ordinal()
    .range(["steelblue", "#ccc"]);

var duration = 750,
    delay = 25;

var partition = d3.layout.partition()
    .value(function(d) { return d.size; });

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left");

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

svg.append("rect")
    .attr("class", "background")
    .attr("width", width)
    .attr("height", height)
    .on("click", up);

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0, " + height + ")")
    .append("line")
    .attr("x1", "100%");

svg.append("g")
    .attr("class", "y axis");

d3.json("https://jsonblob.com/api/f577d19c-0f2b-11e7-a0ba-09040711ce47", function(error, root) {
  if (error) throw error;

  partition.nodes(root);
  y.domain([0, root.value]).nice();
  down(root, 1000);
});

function down(d,i) {
  if (!d.children || this.__transition__) return;
  var end = duration + d.children.length * delay;

  // Mark any currently-displayed bars as exiting.
  var exit = svg.selectAll(".enter")
      .attr("class", "exit");

  // Entering nodes immediately obscure the clicked-on bar, so hide it.
  exit.selectAll("rect").filter(function(p) { return p === d; })
      .style("fill-opacity", 1e-6);

  // Enter the new bars for the clicked-on data.
  // Per above, entering bars are immediately visible.
  var enter = bar(d)
      .attr("transform", stack(i))
      .style("opacity", 1);

  // Have the text fade-in, even though the bars are visible.
  // Color the bars as parents; they will fade to children if appropriate.
  enter.select("text").style("fill-opacity", 1e-6);
  enter.select("rect").style("fill", color(true));

  // Update the x-scale domain.
  y.domain([0, d3.max(d.children, function(d) { return d.value; })]).nice();

  // Update the x-axis.
  svg.selectAll(".y.axis").transition()
      .duration(duration)
      .call(yAxis);

  // Transition entering bars to their new position.
  var enterTransition = enter.transition()
      .duration(duration)
      .delay(function(d, i) { return i * delay; })
      .attr("transform", function(d, i) { return "translate(" + barHeight * i * 2.5 + "," + 0 + ")"; });

  // Transition entering text.
  enterTransition.select("text")
      .style("fill-opacity", 1);

  // Transition entering rects to the new y-scale.
  enterTransition.select("rect")
      .attr("y", function(d) { return y(d.value); })
      .attr("height", function(d) { return height - y(d.value); })
      .style("fill", function(d) { return color(!!d.children); });

  // Transition exiting bars to fade out.
  var exitTransition = exit.transition()
      .duration(duration)
      .style("opacity", 1e-6)
      .remove();

  // Transition exiting bars to the new y-scale.
  exitTransition.selectAll("rect")
      .attr("height", function(d) { return height - y(d.value); });

  // Rebind the current node to the background.
  svg.select(".background")
      .datum(d)
    .transition()
      .duration(end);

  d.index = i;
}

function up(d) {
  if (!d.parent || this.__transition__) return;
  var end = duration + d.children.length * delay;

  // Mark any currently-displayed bars as exiting.
  var exit = svg.selectAll(".enter")
      .attr("class", "exit");

  // Enter the new bars for the clicked-on data's parent.
  var enter = bar(d.parent)
      .attr("transform", function(d, i) { return "translate(" + barHeight * i * 2.5 + "," + 0 + ")"; })
      .style("opacity", 1e-6);

  // Color the bars as appropriate.
  // Exiting nodes will obscure the parent bar, so hide it.
  enter.select("rect")
      .style("fill", function(d) { return color(!!d.children); })
    .filter(function(p) { return p === d; })
      .style("fill-opacity", 1e-6);

  // Update the y-scale domain.
  y.domain([0, d3.max(d.parent.children, function(d) { return d.value; })]).nice();

  // Update the y-axis.
  svg.selectAll(".y.axis").transition()
      .duration(duration)
      .call(yAxis);

  // Transition entering bars to fade in over the full duration.
  var enterTransition = enter.transition()
      .duration(end)
      .style("opacity", 1);

  // Transition entering rects to the new y-scale.
  // When the entering parent rect is done, make it visible!
  enterTransition.select("rect")
      .attr("height", function(d) { return height - y(d.value); })
      .attr("y", function(d) { return y(d.value); })
      .each("end", function(p) { if (p === d) d3.select(this).style("fill-opacity", null); });

  // Transition exiting bars to the parent's position.
  var exitTransition = exit.selectAll("g").transition()
      .duration(duration)
      .delay(function(d, i) { return i * delay; })
      .attr("transform", stack(d.index));

  // Transition exiting text to fade out.
  exitTransition.select("text")
      .style("fill-opacity", 1e-6);

  // Transition exiting rects to the new scale and fade to parent color.
  exitTransition.select("rect")
      .attr("height", function(d) { return height - y(d.value); })
      .attr("y", function(d) { return y(d.value); })
      .style("fill", color(true));

  // Remove exiting nodes when the last child has finished transitioning.
  exit.transition()
      .duration(end)
      .remove();

  // Rebind the current parent to the background.
  svg.select(".background")
      .datum(d.parent)
    .transition()
      .duration(end);
}

// Creates a set of bars for the given data node, at the specified index.
function bar(d) {
  var bar = svg.insert("g", ".x.axis")
      .attr("class", "enter")
          .attr("transform", "translate(15,0)")
       .selectAll("g")
          .data(d.children)
        .enter().append("g")
         .style("cursor", function(d) { return !d.children ? null : 
    "pointer"; })
          .on("click", down);

      bar.append("text")
          .attr("x", barHeight / 2)
          .attr("y", height + 10)
          .attr("dy", ".35em")
          .style("text-anchor", "start")
          .text(function(d) { return d.name; })
          .attr("transform", "rotate(45 " + (barHeight / 2) + " " + (height + 
    10) + ")")

      bar.append("rect")
          .attr("width", barHeight)
          .attr("height", function(d) { return height - y(d.value); })
          .attr("y", function(d) { return y(d.value); });

      return bar;
    }

    // A stateful closure for stacking bars horizontally.
    function stack(i) {
      var y0 = 0;
      return function(d) {
        var tx = "translate(" + barHeight * i * 1.5 + "," + y0 + ")";
        y0 += y(d.value);
        return tx;
      };
    }
bar.append("rect")
          .attr("width", barHeight)
          .attr("height", function(d) { return height - y(d.value); })

d.value定義用於顯示的數字。 如果需要其他東西,請選擇與您相關的對象(d)的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM