我一直在经历这个例子并成为d3的菜鸟,并且看过并尝试了一百万个例子,我可以说我完全糊涂了。我有3个不同的对象和数组嵌套json。在我的情况下,我只想使用“购买”节点,但我不知道如何只获得它。这是我正在使用的json。
{
"spend":
[
{ "letter": "A", "frequency": ".12167" },
{ "letter": "B", "frequency": ".03467" },
{ "letter": "C", "frequency": ".09167" },
{ "letter": "D", "frequency": ".03267" },
{ "letter": "E", "frequency": ".09467" },
{ "letter": "F", "frequency": ".13167" },
{ "letter": "G", "frequency": ".08134" },
{ "letter": "H", "frequency": ".12167" },
{ "letter": "I", "frequency": ".03467" },
{ "letter": "J", "frequency": ".09167" },
{ "letter": "K", "frequency": ".03267" },
{ "letter": "L", "frequency": ".09467" },
{ "letter": "M", "frequency": ".13167" },
{ "letter": "N", "frequency": ".08134" },
{ "letter": "O", "frequency": ".03467" },
{ "letter": "P", "frequency": ".09167" },
{ "letter": "Q", "frequency": ".03267" },
{ "letter": "R", "frequency": ".09467" },
{ "letter": "S", "frequency": ".13167" },
{ "letter": "T", "frequency": ".08134" },
{ "letter": "U", "frequency": ".03467" },
{ "letter": "V", "frequency": ".09167" },
{ "letter": "W", "frequency": ".03267" },
{ "letter": "X", "frequency": ".09467" },
{ "letter": "Y", "frequency": ".13167" },
{ "letter": "Z", "frequency": ".08134" }
],
"purchase":
[
{ "letter": "A", "frequency": ".12345" },
{ "letter": "B", "frequency": ".03467" },
{ "letter": "C", "frequency": ".09167" },
{ "letter": "D", "frequency": ".03267" },
{ "letter": "E", "frequency": ".09467" },
{ "letter": "F", "frequency": ".13167" },
{ "letter": "G", "frequency": ".08134" },
{ "letter": "H", "frequency": ".12167" },
{ "letter": "I", "frequency": ".03467" },
{ "letter": "J", "frequency": ".09167" },
{ "letter": "K", "frequency": ".03267" },
{ "letter": "L", "frequency": ".09467" },
{ "letter": "M", "frequency": ".13167" },
{ "letter": "N", "frequency": ".08134" },
{ "letter": "O", "frequency": ".03467" },
{ "letter": "P", "frequency": ".09167" },
{ "letter": "Q", "frequency": ".03267" },
{ "letter": "R", "frequency": ".09467" },
{ "letter": "S", "frequency": ".13167" },
{ "letter": "T", "frequency": ".08134" },
{ "letter": "U", "frequency": ".03467" },
{ "letter": "V", "frequency": ".09167" },
{ "letter": "W", "frequency": ".03267" },
{ "letter": "X", "frequency": ".09467" },
{ "letter": "Y", "frequency": ".13167" },
{ "letter": "Z", "frequency": ".08134" }
],
"visits":
[
{ "letter": "A", "frequency": ".12167" },
{ "letter": "B", "frequency": ".03467" },
{ "letter": "C", "frequency": ".09167" },
{ "letter": "D", "frequency": ".03267" },
{ "letter": "E", "frequency": ".09467" },
{ "letter": "F", "frequency": ".13167" },
{ "letter": "G", "frequency": ".08134" },
{ "letter": "H", "frequency": ".12167" },
{ "letter": "I", "frequency": ".03467" },
{ "letter": "J", "frequency": ".09167" },
{ "letter": "K", "frequency": ".03267" },
{ "letter": "L", "frequency": ".09467" },
{ "letter": "M", "frequency": ".13167" },
{ "letter": "N", "frequency": ".08134" },
{ "letter": "O", "frequency": ".03467" },
{ "letter": "P", "frequency": ".09167" },
{ "letter": "Q", "frequency": ".03267" },
{ "letter": "R", "frequency": ".09467" },
{ "letter": "S", "frequency": ".13167" },
{ "letter": "T", "frequency": ".08134" },
{ "letter": "U", "frequency": ".03467" },
{ "letter": "V", "frequency": ".09167" },
{ "letter": "W", "frequency": ".03267" },
{ "letter": "X", "frequency": ".09467" },
{ "letter": "Y", "frequency": ".13167" },
{ "letter": "Z", "frequency": ".08134" }
]
}
我使用的条形图的d3 / javascript是
function BarChartJson(thewidth, theheight, theJsonFile , displayElement)
{
var margin = {top: 40, right: 20, bottom: 30, left: 40};
var width = thewidth - margin.left - margin.right;
var height = theheight - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .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 tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
})
var svg = d3.select(displayElement).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 + ")");
svg.call(tip);
d3.json(theJsonFile, function(error, data) {
x.domain(data.map(function(d) {
return d.letter;
}));
y.domain([0, d3.max(data, function(d) {
return d.frequency;
})]);
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("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
});
}
调用就像这样的BarChartJson(300,200,“data1.json”,“#book13list”);
当我仅将文件设为“购买”节点时,它会显示示例条形图。我尝试了各种不同的东西,现在我不确定最好的方式或者是否有可能。
感谢您的帮助