0

I have tried to work on my D3 scatter plot. And it seems like my data isn't showing on the graph itself. Any reasons why? And also.. i have set my x and y domain to certain limit. But when i zoom in, out.. it will still go beyond the limit.

Here is my codes...

var margin = { top: 20, right: 20,bottom: 20, left: 45 }, 
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;

var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%S").parse;  

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


var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .ticks(5);

var yAxis = d3.svg.axis()
    .scale(y)
    .orient("left")
    .ticks(5);

var zoom = d3.behavior.zoom()
    .x(x)
    .on("zoom", zoomed);

var svg = d3.select('.graph')
    .append("svg")
    .attr('width', width + margin.left + margin.right)
    .attr('height', height + margin.top + margin.bottom)
    .call(zoom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var clip = svg.append("svg:clipPath")
    .attr("id", "clip")
    .append("svg:rect")
    .attr("x", 0)
    .attr("y", 0)
    .attr("width", width)
    .attr("height", height);

var chartBody = svg.append("g")
    .attr("clip-path", "url(#clip)");


var make_x_axis = function () {
    return d3.svg.axis()
        .scale(x)
        .orient("bottom")
        .ticks(5);
};

var make_y_axis = function () {
    return d3.svg.axis()
        .scale(y)
        .orient("left")
        .ticks(5);
};


function zoomed() {
    console.log(d3.event.translate);
    console.log(d3.event.scale);
    svg.select(".x.axis").call(xAxis);
    svg.select(".y.axis").call(yAxis);
    svg.select(".x.grid")
        .call(make_x_axis()
        .tickSize(-height, 0, 0)
        .tickFormat(""));
    svg.select(".y.grid")
        .call(make_y_axis()
        .tickSize(-width, 0, 0)
        .tickFormat(""));
    svg.select(".dot")
        .attr("class", "circle")
        .attr("r", 5)
        .attr("cx", function(d) { return x(d.ArtDateTime);})
        .attr("cy", function(d) { return y(d.Ranking * 3); })
}

d3.csv("FinalCSVFile.csv", function(error, data) {
    data.forEach(function(d) {
        d.ArtDateTime = parseDate(d.ArtDateTime);
        d.Ranking = +d.Ranking;
    });
x.domain(d3.extent(data, function(d) { return d.ArtDateTime; }));
y.domain([0, 5]);


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



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

svg.append("g")
    .attr("class", "x grid")
    .attr("transform", "translate(0," + height + ")")
    .call(make_x_axis()
    .tickSize(-height, 0, 0)
    .tickFormat(""));

svg.append("g")
    .attr("class", "y grid")
    .call(make_y_axis()
    .tickSize(-width, 0, 0)
    .tickFormat(""));


chartBody.append("svg:path")
    .selectAll(".dot")
    .data(data)
    .enter().append("circle")
    .attr("class", "dot")
    .attr("r", 5)
    .attr("cx", function(d) { return x(d.ArtDateTime);})
    .attr("cy", function(d) { return y(d.Ranking); });
});

Enlighten me please? NEED HELP AND ADVICE! I AM very new to D3.js

  • You can't append `circle` to `path` elements. You should be able to see the circles if you remove the `.append("svg:path")`. – Lars Kotthoff Jul 25 '14 at 14:37
  • @LarsKotthoff I tried that already. My circle does show out. But ALL my circles will directly move to the Y axis bar on the left when i tried to zoom in/out. – user3766044 Jul 25 '14 at 14:53
  • Have you seen [this question](http://stackoverflow.com/questions/15069959/d3-js-scatter-plot-zoom-drag-boundaries-zoom-buttons-reset-zoom-calculate-m)? – Lars Kotthoff Jul 25 '14 at 14:59
  • @LarsKotthoff Yes, i have looked at that web page before. As i am still new to D3.js, i don't quite understand how the codes work... – user3766044 Jul 25 '14 at 15:06

0 Answers0