javascript <path> 属性的值无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26070783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid value for <path> attribute
提问by skaz
I see other posts with this problem but I just can't seem to solve it. I am new to d3 and am trying to load some data into a line graph, but i keep getting the error
我看到其他帖子有这个问题,但我似乎无法解决它。我是 d3 的新手,正在尝试将一些数据加载到折线图中,但我不断收到错误消息
Invalid value for <path> attribute d="MNaN,250LNaN,71.05263157894737LNaN,55.26315789473685
Invalid value for <path> attribute d="MNaN,250LNaN,71.05263157894737LNaN,55.26315789473685
I have setup everything like in an example (which worked), but this isn't working for some reason. I am getting data back from a webservice as a list of the following form: Date="1/1/2014" NumberOfActive=1 (Example). I have tried using the parse functions but they don't really work. Here is my code:
我已经像示例中一样设置了所有内容(有效),但是由于某种原因这不起作用。我正在从 Web 服务获取数据作为以下表单的列表:Date="1/1/2014" NumberOfActive=1(示例)。我曾尝试使用 parse 函数,但它们并没有真正起作用。这是我的代码:
var data;
var margin = { top: 20, right: 20, bottom: 30, left: 50 },
width = 1000 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
function parseDateForViewing(d) {
return d3.time.format('%b %Y')(d3.time.format('%m/%d/%Y').parse(d));
}
function parseDate(d) {
return d3.time.format('%c')(d3.time.format('%m/%d/%Y').parse(d));
}
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 line = d3.svg.line()
.x(function (d) { return x(d.Date); })
.y(function (d) { return y(d.NumberOfActive); });
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 + ")");
$.ajax({
type: "POST",
url: "Dashboard.aspx/GetActiveLoansData",
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (thedata) {
var d = thedata.d;
data = thedata.d;
var _len = d.length;
var post, i;
for (i = 0; i < data.length; i++) {
d = data[i];
var t = parseDate(d.Date);
var s = d.Date;
x.domain(d3.extent(data, function (d) { return d.Date; }));
y.domain(d3.extent(data, function (d) { return d.NumberOfActive; }));
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("Closed Loans");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
}
}
});
采纳答案by skaz
Before actually plotting the graph, I needed to convert the dates with the following line:
在实际绘制图形之前,我需要使用以下行转换日期:
for (i = 0; i < data.length; i++) {
data[i].Date = parseDate(data[i].Date);
}
This line can be seen in the following, final code:
可以在以下最终代码中看到这一行:
var data;
var margin = { top: 20, right: 60, bottom: 30, left: 50 },
width = 1000 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
function parseDateForViewing(d) {
return d3.time.format('%b %Y')(d3.time.format('%m/%d/%Y').parse(d));
}
function parseDate2(d) {
return d3.time.format('%c')(d3.time.format('%m/%d/%Y').parse(d));
}
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");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function (d) { return x(d.Date); })
.y(function (d) { return y(d.NumberOfActive); });
var svg = d3.select("#svgsection").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 d = thedata.d;
data = thedata.d;
var _len = d.length;
var post, i;
for (i = 0; i < data.length; i++) {
data[i].Date = parseDate(data[i].Date);
}
for (i = 0; i < data.length; i++) {
d = data[i];
x.domain(d3.extent(data, function (d) { return d.Date; }));
y.domain(d3.extent(data, function (d) { return d.NumberOfActive; }));
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("Closed Loans");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2) + 10)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("Closed Loans by Month");
回答by vijay gupta
I faced the same problem with my code.I found that it is because if there is no value then this error will come.We have to make a check for the value we are using like : if(!value){ value=correct_value; }
我的代码遇到了同样的问题。我发现这是因为如果没有值,则会出现此错误。我们必须检查我们正在使用的值,例如: if(!value){ value=correct_value; }