D3.js焦点+上下文溢出 [英] D3.js Focus + Context Overflow

查看:705
本文介绍了D3.js焦点+上下文溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我要重新设置Focus +上下文图表的位置: Focus + Context Example a>使用条形图。我已经能够得到酒吧,但我有一些重叠的问题。当您在上下文区域中进行选择时,对焦区域有时会显示干扰标尺显示的条。这里是我的代码,有一些部分,我知道我需要清理,所以不要专注于那。有人可以在这里指向正确的方向吗?

Hello all I'm trying to repurpose the Focus+Context graph found here: Focus + Context Example to work with a bar graph instead. I've been able to get the bars in but I have some overlap problems. When you make a selection in the context area the focus area will sometimes display bars that interfere with the display of the scale. Here's my code, there are some parts I know I need to clean up so lets not focus on that. Can someone point me in the right direction here?

 <!DOCTYPE html>
    <meta charset="utf-8">
    <style>

    svg {
      font: 10px sans-serif;
    }

path {
  fill: steelblue;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.brush .extent {
  stroke: #fff;
  fill-opacity: .125;
  shape-rendering: crispEdges;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var focusGraph;
var dataTest = d3.csv("sp500.csv");

var margin = {top: 10, right: 10, bottom: 100, left: 40},
    margin2 = {top: 430, right: 10, bottom: 20, left: 40},
    width = 960 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom,
    height2 = 500 - margin2.top - margin2.bottom;

var parseDate = d3.time.format("%b %Y").parse;

var x = d3.time.scale().range([0, width]),
    x2 = d3.time.scale().range([0, width]),
    y = d3.scale.linear().range([0,height]),
    y2 = d3.scale.linear().range([0,height2]);

var xAxis = d3.svg.axis().scale(x).orient("bottom"),
    xAxis2 = d3.svg.axis().scale(x2).orient("bottom"),
    yAxis = d3.svg.axis().scale(y).orient("left");

var brush = d3.svg.brush()
    .x(x2)
    .on("brush", brushed);

var area = d3.svg.area()
    .interpolate("monotone")
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.price); });

/*var area2 = d3.svg.area()
    .interpolate("monotone")
    .x(function(d) { return x2(d.date); })
    .y0(height2)
    .y1(function(d) { return y2(d.price); });*/

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom);

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);

var focus = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
    .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");

d3.csv("sp500.csv", function(error, data) {

  data.forEach(function(d) {
    d.date = parseDate(d.date);
    d.price = +d.price;
  });

  x.domain(d3.extent(data.map(function(d) { return d.date; })));
  y.domain([0, d3.max(data.map(function(d) { return d.price; }))]);
  x2.domain(x.domain());
  y2.domain(y.domain());

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

  focus.append("g")
      .attr("class", "y axis")
      .call(yAxis);

  focusGraph = focus.selectAll("rect")
      .data(data)
    .enter().append("rect")
      .attr("x", function(d, i) { return x(d.date); })
      .attr("y", function(d) { return height - y(d.price); })
      .attr("width", 5)
      .attr("height", function(d) { return y(d.price); });

  context.selectAll("rect")
      .data(data)
    .enter().append("rect")
      .attr("x", function(d, i) { return x2(d.date); })
      .attr("y", function(d) { return height2 - y2(d.price); })
      .attr("width", 5)
      .attr("height", function(d) { return y2(d.price); });

  context.append("g")
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height2 + ")")
      .call(xAxis2);

  context.append("g")
      .attr("class", "x brush")
      .call(brush)
    .selectAll("rect")
      .attr("y", -6)
      .attr("height", height2 + 7);
});

function brushed() {

  var data = d3.csv("sp500.csv");

  x.domain(brush.empty() ? x2.domain() : brush.extent());
  focusGraph.attr("x", function(d, i) { return x(d.date); });
  focusGraph.attr("width", 20);

  focus.select(".x.axis").call(xAxis);

}

</script>


推荐答案

这是因为你只是忘记设置剪辑路径图表。因此,请考虑以下想法:

That's because you simply forgot to set clip-path to the chart. So consider the following idea:

var focus = svg.append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// here we insert the new group that will be the container for the bars
var barsGroup = focus.append("g")
    .attr('clip-path', 'url(#clip)');

然后在这个新组中定义实际图表:

And then you define the actual chart as within this new group:

focusGraph = barsGroup.selectAll("rect")
  .data(data)
.enter().append("rect")

查看演示: http://jsfiddle.net/JGytk/

这篇关于D3.js焦点+上下文溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆