Reputation: 507
I have taken the example from here and I am modifying it slightly so that the graph is drawn when the page loads and the line is drawn when a user selects a file from a jsTree. That is all working fine and the data is plotted correctly. But, the tick marks remain 0 to 1 on the Y axis and 6 PM to .001 on the x axis. I can't figure out how to update them based on the new data. I am just learning js and d3 so I'm sure the knowledge gap is the issue here. But, after a couple hours of being unable to resolve the issue, it was time to ask.
<script>
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
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");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg;
window.onload = function(){
svg = d3.select("#MainDiv").append("svg")
.attr("id","myGraph")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Price ($)");
}
function updateGraph(filename){
d3.xhr("http://127.0.0.1:8080/Data/TreeData/getGraphData?filename="+filename, function(error,data){
var data = d3.tsv.parse(data.responseText);
var parseDate = d3.time.format("%d-%b-%y").parse;
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
svg.selectAll("y axis")
.call(yAxis)
svg.selectAll("x axis")
.call(xAxis);
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.close; }));
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
}
</script>
Upvotes: 0
Views: 1459
Reputation: 109232
The selector for the axes elements is wrong. If you want to select by class, prefix the name of the class with a dot:
svg.selectAll(".y.axis")
.call(yAxis)
svg.selectAll(".x.axis")
.call(xAxis);
Upvotes: 1