我正在处理php驱动页面中的一些D3示例。我找到了一个很好的例子,我想使用带有内联标签的折线图(完整代码在这里:https://bl.ocks.org/mbostock/4b66c0d9be9a0d56484e),但我无法弄清楚如何从tsv导入转换代码直接从数据库提供的数组。
我显然是直接提供这样的数据:
<body style="padding-top: 0px; padding-left: 10px; padding-right: 10px; margin: 0px; overflow-y: scroll; overflow-x: hidden; background-color: white;">
<table style="width: 100px; height: 100px;">
<tr style="width: 100px; height: 100px;">
<td style="vertical-align:bottom; text-align:center; border-style: solid; width: 45%; height: 100px;">
just some text.
</td>
<td style="vertical-align:bottom; text-align:center; border-style: solid; width: 10%; height: 100px;">
some more text, just a little bit more than the other cell. I'm looking for some overflow to this thing.
</td>
<td style="vertical-align:bottom; text-align:center; border-style: solid; width: 45%; height: 100px;">
just some text.
</tr>
</table>
</body>
然后我试图替换这个处理导入和自动排序的代码块。
var data = [
{date:2009, Apples:130, Bananas:40},
{date:2010, Apples:137, Bananas:58},
{date:2011, Apples:166, Bananas:97},
{date:2012, Apples:154, Bananas:117},
{date:2013, Apples:179, Bananas:98},
{date:2014, Apples:187, Bananas:120},
{date:2015, Apples:189, Bananas:84}
]
但我认为我在复制创建该系列的部分时遇到了问题。
我尝试了几种变体:
d3.requestTsv("data.tsv", function(d) {
d.date = parseTime(d.date);
for (var k in d) if (k !== "date") d[k] = +d[k];
return d;
}, function(error, data) {
if (error) throw error;
var series = data.columns.slice(1).map(function(key) {
return data.map(function(d) {
return {
key: key,
date: d.date,
value: d[key]
};
});
});
后面跟一个函数来设置代码末尾的数据类型:
var series = data.map(function(key) {
return data.map(function(d) {
return {
key: key,
date: d.date,
value: d[key]
};
});
});
但似乎没有任何效果。我确定我有一些简单的遗漏,但是我应该通过直接在数组而不是tsv导入中直接提供数据来改变使用代码?
答案 0 :(得分:0)
您的数据数组是正确的(关于Bostock代码中的TSV)。
但是,您有两个问题:
d3.tsv
函数创建一个名为columns
的数组属性。由于您放弃了d3.tsv
并使用变量来存储数据,因此您必须自己创建该数组:
data.columns = ["date", "Apples", "Bananas"]
d3.tsv
接受行功能。同样,由于您使用变量来存储数据,因此您必须使用forEach
来执行Bostock代码中的行函数:
data.forEach(d=>{
d.date = parseTime(d.date);
for (var k in d) if (k !== "date") d[k] = +d[k];
});
以下是使用变量存储数据的更新代码:https://bl.ocks.org/anonymous/749f2c5bc6a42d68bca3ec579646ff1d
这里是Stack片段中的相同代码:
<style>
text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke-width: 1.5px;
}
.label {
text-anchor: middle;
}
.label rect {
fill: white;
}
.label-key {
font-weight: bold;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v4.0.0-alpha.9.min.js"></script>
<script>
var parseTime = d3.timeParse("%Y");
var svg = d3.select("svg");
var margin = {top: 30, right: 50, bottom: 30, left: 30},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
labelPadding = 3;
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var data = [
{date:2009, Apples:130, Bananas:40},
{date:2010, Apples:137, Bananas:58},
{date:2011, Apples:166, Bananas:97},
{date:2012, Apples:154, Bananas:117},
{date:2013, Apples:179, Bananas:98},
{date:2014, Apples:187, Bananas:120},
{date:2015, Apples:189, Bananas:84}
];
data.columns = ["date", "Apples", "Bananas"]
data.forEach(d=>{
d.date = parseTime(d.date);
for (var k in d) if (k !== "date") d[k] = +d[k];
});
var series = data.columns.slice(1).map(function(key) {
return data.map(function(d) {
return {
key: key,
date: d.date,
value: d[key]
};
});
});
var x = d3.scaleTime()
.domain([data[0].date, data[data.length - 1].date])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, d3.max(series, function(s) { return d3.max(s, function(d) { return d.value; }); })])
.range([height, 0]);
var z = d3.scaleCategory10();
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
var serie = g.selectAll(".serie")
.data(series)
.enter().append("g")
.attr("class", "serie");
serie.append("path")
.attr("class", "line")
.style("stroke", function(d) { return z(d[0].key); })
.attr("d", d3.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.value); }));
var label = serie.selectAll(".label")
.data(function(d) { return d; })
.enter().append("g")
.attr("class", "label")
.attr("transform", function(d, i) { return "translate(" + x(d.date) + "," + y(d.value) + ")"; });
label.append("text")
.attr("dy", ".35em")
.text(function(d) { return d.value; })
.filter(function(d, i) { return i === data.length - 1; })
.append("tspan")
.attr("class", "label-key")
.text(function(d) { return " " + d.key; });
label.append("rect", "text")
.datum(function() { return this.nextSibling.getBBox(); })
.attr("x", function(d) { return d.x - labelPadding; })
.attr("y", function(d) { return d.y - labelPadding; })
.attr("width", function(d) { return d.width + 2 * labelPadding; })
.attr("height", function(d) { return d.height + 2 * labelPadding; });
</script>
&#13;