2016-12-21 2 views
0

Я пытаюсь нарисовать линейный график, но столкнулся с этой ошибкой. Я уже разобраны мои данные, но я не знаю, почему я получаю эту ошибку на «вар Valueline»:D3 Line Chart JSON разбор

пожалуйста, помогите

Ошибка: Проблема разбора d = "MNaN, 679.1772151898734LNaN, 693.132911392405LNaN, 437.2784810126583LNaN, 655.9177215189874 LNaN, 618.7025316455697LNaN, 427.9746835443038LNaN, 423.32278481012656LNaN, 674.5253164556962LNaN, 581.4873417721519LNaN, 521.0126582278481LNaN, 148.86075949367086LNaN, 637.3101265822785LNaN

JSON данных выглядит следующим образом:

All=[{date: "01/09/2016", close: "12"}, {date: "01/11/2016", close: "9"},..] 

var margin = {top: 30, right: 20, bottom: 35, left: 50}, 
width = 1100 - margin.left - margin.right, 
height = 800 - margin.top - margin.bottom; 

var parseDate = d3.time.format("%m-%d-%Y").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 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 + ")"); 

// function for the x grid lines 
function make_x_axis() { 
return d3.svg.axis() 
    .scale(x) 
    .orient("bottom") 
    .ticks(5) 
} 

// function for the y grid lines 
function make_y_axis() { 
return d3.svg.axis() 
    .scale(y) 
    .orient("left") 
    .ticks(5) 
} 

// Get the data 
d3.json("http://xxx.xxx.xxx/trafficGraph.php", function(error, data) { 
// var data= data.All; 
console.log(data.All); 
data.All.forEach(function(d) { 
    d.date = parseDate(d.date); 
    d.close = +d.close; 
}); 

// Scale the range of the data 
x.domain(d3.extent(data.All, function(d) { return d.date; })); 
y.domain([0, d3.max(data.All, function(d) { return d.close; })]); 

// Add the filled area 
svg.append("path") 
    .datum(data.All) 
    .attr("class", "area") 
    .attr("d", area); 

// Draw the x Grid lines 
svg.append("g") 
    .attr("class", "grid") 
    .attr("transform", "translate(0," + height + ")") 
    .call(make_x_axis() 
     .tickSize(-height, 0, 0) 
     .tickFormat("") 
    ) 


    var area = d3.svg.area() 
.x(function(d) { return x(d.date); }) 
.y0(height) 
.y1(function(d) { return y(d.close); }); 


// Draw the y Grid lines 
svg.append("g")    
    .attr("class", "grid") 
    .call(make_y_axis() 
     .tickSize(-width, 0, 0) 
     .tickFormat("") 
    ) 

var valueline = d3.svg.line() 
.x(function(d) { return x(d.date); }) 
.y(function(d) { return y(d.close); }); 


// Add the valueline path. 
svg.append("path") 
    .attr("d", valueline(data.All)); 




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

// Add the Y Axis 
svg.append("g") 
    .attr("class", "y axis") 
    .call(yAxis); 

// Add the text label for the X axis 
svg.append("text") 
    .attr("transform", 
      "translate(" + (width/2) + " ," + 
         (height+margin.bottom) + ")") 
    .style("text-anchor", "middle") 
    .text("Date"); 

// Add the white background to the y axis label for legibility 
svg.append("text") 
    .attr("transform", "rotate(-90)") 
    .attr("y", 6) 
    .attr("x", margin.top - (height/2)) 
    .attr("dy", ".71em") 
    .style("text-anchor", "end") 
    .attr("class", "shadow") 
    .text("Price ($)"); 

// Add the text label for the Y axis 
svg.append("text") 
    .attr("transform", "rotate(-90)") 
    .attr("y", 6) 
    .attr("x", margin.top - (height/2)) 
    .attr("dy", ".71em") 
    .style("text-anchor", "end") 
    .text("Price ($)"); 

// Add the title 
svg.append("text") 
    .attr("x", (width/2))  
    .attr("y", 0 - (margin.top/2)) 
    .attr("text-anchor", "middle") 
    .style("font-size", "16px") 
    .style("text-decoration", "underline") 
    .text("Price vs Date Graph"); 

}); 

</script> 

ответ

2

Вашего parseDate находится в неправильном формате, а это означает, что идет нуль после разбора (и линия функция возвращает пренебрежимо малую). Должно быть:

var parseDate = d3.time.format("%m/%d/%Y").parse; 
+0

Большое спасибо! @Отметка. Знаете ли вы, как я могу анимировать график во время его запуска? – ros

+2

@ros, это довольно широкий вопрос для комментария (и он довольно нахмурился здесь, чтобы «зацепить» вопросы). Я предлагаю вам закрыть этот вопрос и открыть новый с ** спецификой ** того, как вы хотите оживить. – Mark

+0

Хорошо .. Спасибо @Mark! – ros

Смежные вопросы