简体   繁体   English

d3轴刻度不更新

[英]d3 Axis Ticks Not Updating

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. 我从这里以示例为例,我对其进行了一些修改,以便在页面加载时绘制图形,而当用户从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. 但是,刻度线在Y轴上保持0到1,在x轴上保持6 PM到.001。 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. 我只是在学习js和d3,所以我确定知识差距是这里的问题。 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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM