javascript D3.js 滚动条形图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23591914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:10:45  来源:igfitidea点击:

D3.js scrolling bar chart

javascriptjqueryd3.js

提问by Sergey Scopin

I have a simple bar chart:

我有一个简单的条形图:

var margin = {top: 20, right: 20, bottom: 30, left: 50};
var xLPU=d3.scale.ordinal();
var yLPU=d3.scale.linear();
var xLPUAxis = d3.svg.axis()
    .scale(xLPU)
    .orient("bottom");

var yLPUAxis = d3.svg.axis()
    .scale(yLPU)
    .orient("left")
    .ticks(10, "чел.");
var LPUdivision=d3.select("#LPUgraph").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 + ")");
d3.json("LPUdivision.json",function(data){
    xLPU.domain(data.map(function(d){return d.lpu;}))
    .rangeBands([0, width]);
    yLPU.domain([0,d3.max(data, function(d) { return d.amount; })])
    .range([height,0]);
    LPUdivision.append("g")
    .attr("class","x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xLPUAxis);
    LPUdivision.append("g")
      .attr("class", "y axis")
      .call(yLPUAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", ".71em")
      .style("text-anchor", "end")
      .text("численность состава");
    LPUdivision.selectAll(".bar")
    .data(data)
    .enter()
    .append("rect")
    .attr("class","bar")
    .attr("x",function(d){return xLPU(d.lpu)})
    .attr("width", xLPU.rangeBand()-5)
    .attr("y", function(d) { return yLPU(d.amount); })
    .attr("height", function(d) { return height - yLPU(d.amount); })
    .attr("fill","steelblue");

});

LPUdivision.json

LPUdivision.json

[
{"lpu":"lpu1","amount":"20"},
{"lpu":"lpu2","amount":"40"},
{"lpu":"lpu3","amount":"60"},
{"lpu":"lpu4","amount":"10"},
{"lpu":"lpu5","amount":"80"},
{"lpu":"lpu6","amount":"30"},
{"lpu":"lpu7","amount":"20"},
{"lpu":"lpu8","amount":"40"},
{"lpu":"lpu9","amount":"60"},
{"lpu":"lpu10","amount":"10"},
{"lpu":"lpu11","amount":"80"},
{"lpu":"lpu12","amount":"30"},
{"lpu":"lpu13","amount":"20"},
{"lpu":"lpu14","amount":"40"},
{"lpu":"lpu15","amount":"60"},
{"lpu":"lpu16","amount":"10"},
{"lpu":"lpu17","amount":"80"},
{"lpu":"lpu18","amount":"30"}
]

Here is output graphic: Lpu graphAs you can see it's messy a lot. I need all the lpu labels to display. So I suppose that there should be some scroll bar, but I didn't find good example with scroll bar on graphic. Any advises? Here is fiddle

这是输出图形: Lpu 图正如您所看到的,它非常混乱。我需要显示所有 lpu 标签。所以我想应该有一些滚动条,但我没有找到图形滚动条的好例子。任何建议?这是小提琴

回答by Dave

Try as below

尝试如下

<div class="outer">
    <div class="inner"> 
      <div id="LPUgraph"> </div>
    </div>
</div>

<style>
.outer {
    width: 700px;
    height: 600px;
    overflow: auto;
}
.inner {
    width: 800px;
    height: 600px;
}
#LPUgraph{
    display: block;
    width: 100%;
    height: 100%;
}
</style>

jsfiddle sample

jsfiddle 示例