我正在使用Polymer来渲染一些d3图表。最初渲染Polymer时,我只绘制一个带有轴而没有数据的图形,因为一旦API调用成功,数据就会出现。但是,当我开始在svg中选择'rect'元素时,调用$fd = fopen('php://memory','rw');
fwrite($fd, 'hey there'); //This would be your fputcsv stuff
fseek($fd, 0); //Reset the file pointer
$content = fread($fd, 4096); //This will read 4096 bytes maximum
echo $content;
就失败了。这是我的代码:
data()
当父组件传递一大块数据时, dataChanged: function() {
var data = this.data;
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = this.width - margin.left - margin.right,
height = this.height - margin.top - margin.bottom;
// format the data
data.forEach(function(d) {
d.date = d3.isoParse(d.date);
});
// set the ranges
var x = d3.scaleTime()
.domain(d3.extent(data, function(d) { return d.date; }))
.rangeRound([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
var svg = d3.select(this.$.chart).transition();
var histogram = d3.histogram()
.value(function(d) { return d.date; })
.domain(x.domain())
.thresholds(x.ticks(d3.timeMonth));
var bins = histogram(data);
y.domain([0, d3.max(bins, function(d) { return d.length; })]);
svg.selectAll("rect")
.data(bins)
.enter().append("rect")
.attr("class", "bar")
.attr("x", 1)
.attr("transform", function(d) {
return "translate(" + x(d.x0) + "," + y(d.length) + ")";
})
.attr("width", function(d) { return x(d.x1) - x(d.x0) -1 ; })
.attr("height", function(d) { return height - y(d.length); });
svg.select(".xAxis")
.duration(300)
.call(d3.axisBottom(x));
svg.select(".yAxis")
.duration(300)
.call(d3.axisLeft(y));
},
ready: function() {
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = this.width - margin.left - margin.right,
height = this.height - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleTime()
.domain([new Date(2010, 6, 3), new Date(2012, 0, 1)])
.rangeRound([0, width]);
var y = d3.scaleLinear()
.range([height, 0]);
// Add the SVG to my 'chart' div.
var svg = d3.select(this.$.chart).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 + ")");
// Add the X Axis
svg.append("g")
.attr("class","xAxis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add the Y Axis
svg.append("g")
.attr("class","yAxis")
.call(d3.axisLeft(y));
}
在渲染时被调用ready()
。一切正常,直到dataChanged()
与svg.selectAll("rect").data(bins)
崩溃时才会被调用。 Uncaught TypeError: svg.selectAll(...).data is not a function
中包含正确的数据,因此不是空的。我知道没有bins
元素可供选择,但在example I followed here之前没有rect
元素,无论如何它们都被这个调用追加,所以我很困惑为什么这不起作用。
答案 0 :(得分:1)
这条线的目的是什么:
var svg = d3.select(this.$.chart).transition();
这会使svg
成为转换,您的代码暗示它应该是选择。所以,只需删除.transition
:
var svg = d3.select(this.$.chart);
...
svg.selectAll("rect")
.data(bins)
...