我如何使用d3.layout.使用D3.js与此代码堆栈绘制折线图



我想画一个面积图。我无法访问数据,我不知道为什么。在firefox控制台中,"数据未定义"。这是我的代码和数据。此数据为JSON格式,位于服务器端:

  {"Id":466,"Name":"korea",                                                                                                                                                                                                                                                                                                  
      "Occurrences":[
            {"OccurrenceDate":"/Date(1398207600000+0100)/","OccurrenceFrequency":27},         
            {"OccurrenceDate":"/Date(1398726000000+0100)/","OccurrenceFrequency":1}, 
            {"OccurrenceDate":"/Date(1398898800000+0100)/","OccurrenceFrequency":4},
            {"OccurrenceDate":"/Date(1399071600000+0100)/","OccurrenceFrequency":303}
   ]
     } 

这是我的代码

  <script src="http://d3js.org/d3.v3.min.js"></script>
  <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
  <script>
 var margin = {top: 80, right: 80, bottom: 80, left: 80},
 width = 960 - margin.left - margin.right,
 height = 500 - margin.top - margin.bottom;
 var parseDate1 = d3.time.format.iso.parse;

// Scales and axes. Note the inverted domain for the y-scale: bigger is up!
var x = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]),
xAxis = d3.svg.axis().scale(x).tickSize(-height).tickSubdivide(true),
yAxis = d3.svg.axis().scale(y).ticks(4).orient("right");
// An area generator, for the light fill.
var area = d3.svg.area()
.interpolate("monotone")
.x(function(d) { return x(d.OccurrenceDate); })
.y0(height)
.y1(function(d) { return y(d.OccurrenceFrequency); });
// A line generator, for the dark stroke.
var line = d3.svg.line()
.interpolate("monotone")
.x(function(d) { return x(d.OccurrenceDate); })
.y(function(d) { return y(d.OccurrenceFrequency); });
 var svg = d3.select("body").append("svg")
  .attr("width", width + margin.left + margin.right)
  .attr("height", height + margin.top + margin.bottom)
.append("g")
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var strURI = "http://tr-l6/STWebService/Service.svc/session/Fetchnodehistorybyname?     
strnodename=korea";
    // use jquery to get the json dataset because I cannot get d3.json to work    with Firefox/Chrome (but it is OK with IE)
    // this is probably a jsonp / cross domain issue that requires further tweaking in the WCF web,config file
    // d3.json(strURI,function(error, graph) {
    $.ajax({
            type: "GET",
            url: strURI,
            dataType: 'jsonp',
            success: function (graph) {
    x
data.forEach(function(d) {
 // Convert unix date format to regular format
var dc 
 //dc = (d.date).substring(1, 26);
dc = d.OccurrenceDate.substring(6, 16)
console.log(dc);    
dc = new Date(dc*1000)
console.log(dc);    

   d.OccurrenceDate= parseDate1(dc);   
   d.OccurrenceFrequency = +d.OccurrenceFrequency;
return d
  });
   x.domain(d3.extent(data, function(d) { return d.OccurrenceDate; }));
   y.domain([0, d3.max(data, function(d) { return d.OccurrenceFrequency; })]);
  svg.append("path")
    .datum(data)
    .attr("class", "area")
    .attr("d", area);
  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")
   }});  

我不知道这是否是错误的原因,但是您错过了末尾的</script>脚本

最新更新