D3js奇怪的绘画焦点+通过刷牙的背景

时间:2014-04-08 14:08:27

标签: javascript d3.js

我已经通过刷涂图实现了绘图焦点+上下文以显示卡方结果,但是d3中的结果绘制很奇怪(请在下午7点查看图像)。

在图像下方,我还包括绘制图表的代码。

ChiSquare Image

var chiSquare = function(config,data,d3){
var margin = {top: 10, right: 10, bottom: 100, left: 40},
    margin2 = {top: 430, right: 10, bottom: 20, left: 40},
    width = 1127 - 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([height, 0]),
    y2 = d3.scale.linear().range([height2, 0]);

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.timestamp_unix); })
    .y0(height)
    .y1(function(d) { return y(d.chiSquare); });

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

var svg = d3.select(config.selector).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("class", "focus")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

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

  x.domain(d3.extent(data,(function(d) {  return d.timestamp_unix; })));
  y.domain([0, d3.max(data, (function(d) {  return d.chiSquare; }))]);

  x2.domain(x.domain());
  y2.domain(y.domain());

  focus.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area);

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

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

  context.append("path")
      .datum(data)
      .attr("class", "area")
      .attr("d", area2);

  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() {
  x.domain(brush.empty() ? x2.domain() : brush.extent());
  focus.select(".area").attr("d", area);
  focus.select(".x.axis").call(xAxis);
}

function type(d) {
  d.timestamp_unix = parseDate(d.timestamp_unix);
  d.chiSquare = +d.chiSquare;
  return d;
}

//end class
}

我无法在代码中看到任何错误,所以我认为问题来自D3js,但我不知道在哪里看,有什么想法吗?

0 个答案:

没有答案