Javascript D3层次条形图-更改X轴和Y轴值

Javascript D3层次条形图-更改X轴和Y轴值,javascript,python,d3.js,bar-chart,data-visualization,Javascript,Python,D3.js,Bar Chart,Data Visualization,目前,对于D3层次条形图,每个蓝色条表示一个文件夹,其长度编码该文件夹(以及所有子文件夹)中所有文件的总大小。我希望蓝色条应该编码其中项目总数的长度(而不是大小)。例如,在下面的示例中,如果我单击“vis”,它有7个项目。因此,“vis”条的长度应为7。对于所有文件夹中的所有项目也是如此。这是链接- 正文{ 字体:10px无衬线; } 矩形背景{ 填充物:白色; } .安讯士{ 形状渲染:边缘清晰; } .轴线路径, .轴线{ 填充:无; 行程:#000; } D3 var margin={顶

目前,对于D3层次条形图,每个蓝色条表示一个文件夹,其长度编码该文件夹(以及所有子文件夹)中所有文件的总大小。我希望蓝色条应该编码其中项目总数的长度(而不是大小)。例如,在下面的示例中,如果我单击“vis”,它有7个项目。因此,“vis”条的长度应为7。对于所有文件夹中的所有项目也是如此。这是链接-


正文{
字体:10px无衬线;
}
矩形背景{
填充物:白色;
}
.安讯士{
形状渲染:边缘清晰;
}
.轴线路径,
.轴线{
填充:无;
行程:#000;
}
D3
var margin={顶部:30,右侧:30,底部:40,左侧:50},
宽度=window.innerWidth-margin.left-margin.right-100,
高度=window.innerHeight-margin.top-margin.bottom-100;
变量y=d3.scale.linear()
.范围([高度,0]);
var-barHeight=20;
var color=d3.scale.ordinal()
.范围([“钢蓝”、“#ccc”]);
var持续时间=750,
延迟=25;
var partition=d3.layout.partition()
.value(函数(d){返回d.size;});
var yAxis=d3.svg.axis()
.比例(y)
.东方(“左”);
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
svg.append(“rect”)
.attr(“类别”、“背景”)
.attr(“宽度”,宽度)
.attr(“高度”,高度)
。打开(“单击”,向上);
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.附加(“行”)
.attr(“x1”,“100%”);
svg.append(“g”)
.attr(“类”、“y轴”);
d3.json(“https://jsonblob.com/api/f577d19c-0f2b-11e7-a0ba-09040711ce47,函数(错误,根){
如果(错误)抛出错误;
分区.节点(根);
y、 域([0,root.value]).nice();
向下(根,1000);
});
功能下降(d,i){
如果(!d.children | | | | this.uuu transition_uuu)返回;
var end=持续时间+d.children.length*延迟;
//将当前显示的任何条标记为退出。
var exit=svg。选择全部(“.enter”)
.attr(“类”、“出口”);
//输入节点会立即隐藏单击的工具栏,因此请将其隐藏。
exit.selectAll(“rect”).filter(函数(p){return p==d;})
.样式(“填充不透明度”,1e-6);
//为单击的数据输入新栏。
//根据上面的说明,可以立即看到输入条。
var enter=bar(d)
.attr(“转换”,堆栈(i))
.样式(“不透明”,1);
//使文本淡入,即使条形图可见。
//以家长的身份给条形图上色;如果合适,它们将淡出给孩子们。
输入。选择(“文本”)。样式(“填充不透明度”,1e-6);
输入。选择(“矩形”)。样式(“填充”,颜色(真));
//更新x尺度域。
y、 域([0,d3.max(d.children,函数(d){返回d.value;})]).nice();
//更新x轴。
svg.selectAll(“.y.axis”).transition()
.持续时间(持续时间)
.呼叫(yAxis);
//将输入杆转换到其新位置。
var entertransformation=enter.transition()
.持续时间(持续时间)
.delay(函数(d,i){返回i*delay;})
.attr(“transform”,函数(d,i){return”translate(“+barHeight*i*2.5+”,“+0+”));});
//转换输入文本。
输入转换。选择(“文本”)
.样式(“填充不透明度”,1);
//将输入矩形转换为新的y比例。
entertransformation.select(“rect”)
.attr(“y”,函数(d){返回y(d.value);})
.attr(“高度”,函数(d){返回高度-y(d.value);})
.style(“填充”,函数(d){返回颜色(!!d.children);});
//将退出条转换为淡出。
var exitTransition=exit.transition()
.持续时间(持续时间)
.样式(“不透明度”,1e-6)
.remove();
//将现有条形图过渡到新的y形刻度。
exitTransition。选择All(“rect”)
.attr(“高度”,函数(d){返回高度-y(d.value);});
//将当前节点重新绑定到背景。
svg.select(“.background”)
.基准(d)
.transition()
.持续时间(结束);
d、 指数=i;
}
功能启动(d){
如果(!d.parent | | this.uu transition_uu)返回;
var end=持续时间+d.children.length*延迟;
//将当前显示的任何条标记为退出。
var exit=svg。选择全部(“.enter”)
.attr(“类”、“出口”);
//为单击的数据的父级输入新栏。
var enter=bar(d.parent)
.attr(“transform”,函数(d,i){return”translate(“+barHeight*i*2.5+”,“+0+”);)
.样式(“不透明度”,1e-6);
//根据需要为条形图上色。
//退出节点将隐藏父栏,因此将其隐藏。
输入。选择(“rect”)
.style(“填充”,函数(d){返回颜色(!!d.children);})
.filter(函数(p){返回p==d;})
.样式(“填充不透明度”,1e-6);
//更新y比例域。
y、 域([0,d3.max(d.parent.children,函数(d){返回d.value;})]).nice();
//更新y轴。
svg.selectAll(“.y.axis”).transition()
.持续时间(持续时间)
.呼叫(yAxis);
//过渡输入条在整个持续时间内淡入。
var entertransformation=enter.transition()
.期限(完)
.样式(“不透明”,1);
//将输入矩形转换为新的y比例。
//完成输入父矩形后,使其可见!
entertransformation.select(“rect”)
.attr(“高度”,函数(d){返回高度-y(d.value);})
.attr(“y”,函数(d){返回y(d.value);})
.each(“end”,function(p){if(p==d)d3.select(this).style(“fill opacity”,null);});
//将退出栏转换到父级位置。
var exitTransition=exit.selectAll(“g”).transition()
.持续时间(持续时间)
.delay(函数(d,i){返回i*delay;})
.attr(“转换”,堆栈(d.index));
//将退出文本转换为淡出。
exitTransition.选择(“文本”)
.样式(“填充不透明度”,1e-6);
//从退出矩形到t的转换
 <!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); })