[英]D3 - No graph appearing
I have a variable called final csv which looks like and is stored in a CSV format. 我有一个名为final csv的变量,它看起来像并以CSV格式存储。 The lines are separated by a \\n. 行之间用\\ n分隔。 I'm using the statement D3.csv.parse. 我正在使用语句D3.csv.parse。 Can anyone tell me why nothing appears on the page at all? 谁能告诉我为什么页面上什么都没有显示? :- :-
String, Float
value, 1
value2, 2
function generateGraph(finalcsv)
{
var finaldata = finalcsv;
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatPercent = d3.format(".0");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1, 1);
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")
.tickFormat(formatPercent);
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 + ")");
console.log(finaldata);
d3.csv.parse(finaldata, function(error, data) {
data.forEach(function(d) {
d.Float = +d.Float;
});
x.domain(data.map(function(d) { return d.String; }));
y.domain([0, d3.max(data, function(d) { return d.Float; })]);
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("Integer");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.String); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.Float); })
.attr("height", function(d) { return height - y(d.Float); });
});
}
I quickly built an example which seems to work (or at least it shows something): http://jsfiddle.net/frXRW/2/ 我迅速建立了一个似乎有效的示例(或至少显示了一些内容): http : //jsfiddle.net/frXRW/2/
The first difference with the code you provided is that I didn't use the d3.csv.parse()
function , I don't know if your problem comes from there but I suggest you take a look at the documentation if your problem comes from there: https://github.com/mbostock/d3/wiki/CSV 与您提供的代码的第一个区别是,我没有使用d3.csv.parse()
函数,我不知道您的问题是否来自那里,但是如果您的问题来自我,我建议您看一下文档从那里: https : //github.com/mbostock/d3/wiki/CSV
The second thing I changed, and that I suspect is the source of your problem, is that you forgot to put the #body
identifier between ""
, so it is considered as an object. 我更改的第二件事是怀疑您是问题的根源,因为您忘记将#body
标识符放在""
之间,因此将其视为对象。 Thus I just changed the following line: 因此,我只是更改了以下行:
var svg = d3.select(#body).append("svg")
to 至
var svg = d3.select("#body").append("svg")
If this was your problem, then I would recommend you to use the chrome javascript console as it gave the following error pointing exactly to the line above: 如果这是您的问题,那么我建议您使用chrome javascript控制台,因为它给出了以下错误,它们完全指向上面的行:
Uncaught SyntaxError: Unexpected identifier 未捕获的SyntaxError:意外的标识符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.