Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new with d3.js. I want to create a timeline with brushing composed by two axes; one for focus and one for context. The problem is that when I add simple element as rectangle o circle on the context axis, I can't see this element in the focus axis during the brushing. Where I wrong?

JavaScript
var elem;
var margin = {top: 10, right: 10, bottom: 100, left: 40},
    margin2 = {top: 10, right: 10, bottom: 20, left: 40},
    width = 960 - margin.left - margin.right,
    height = 150 - margin.top - margin.bottom,
    height2 = 150 - 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]);


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


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

var area = d3.svg.area()

    .x(function(d) { return x(d.date); })


var area2 = d3.svg.area()

    .x(function(d) { return x2(d.date); })


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

d3.csv("Timeline-portlet/sp500.csv", type, function(error, data) {
  x.domain(d3.extent(data.map(function(d) { return d.date; })));

  x2.domain(x.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);


  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) ;

  var a=new Date('2009-03-10');
  var b=new Date('2009-05-10');

  var dateTIM=[a,b];



  elem= svg.append("g").attr("transform", "translate(0," + 140 + ")").selectAll(".objTim").data(dateTIM).enter().append("rect").attr("class","objTim")
  .attr("d",elem ).attr("x", function(d){return x2(d)-5}).attr("width", 10)
  .attr("y", -25).attr("height",30).attr("fill","#00C")


});

function brushed() {
  x.domain(brush.empty() ? x2.domain() : brush.extent());
  focus.select(".objTim").attr("d",elem);
  focus.select(".area").attr("d", area);
  focus.select(".x.axis").call(xAxis);
}

function type(d) {
  d.date = parseDate(d.date);
  d.price = +d.price;
  return d;
} 



Regards

What I have tried:

I want to create a timeline with brushing composed by two axes; one for focus and one for context.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900