所以我创建了一个D3条形图,但我似乎无法弄清楚如何使条形图的值正确显示。它显示最大值为30的所有条形。
我已将错误指向此行:
d.amenities = amenityNames.map(function (name)
{
console.log( +d["sum"]);
return {name: name, value: +d["sum"]};
});
我似乎错误地指定了值,但我不确定如何纠正这个问题。
我该如何纠正?有一个相同的JSON。任何帮助都会很感激。
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
//todo generate color rang based on the amenities
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00", "#7b6844","#f45000","#f9fc30"]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
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 + ")");
d3.json("StudentInteractionApi", function (error, data) {
var amenityName = function () {
var bool = true;
var unique = [];
for (var i = 0; i < data.length - 1; i++) {
for (var x = 0; x < unique.length - 1; x++) {
if (unique[x] == data[i]['amenity']) {
bool = false;
break;
}
bool = true;
}
if (bool == true) {
unique.push(data[i]['amenity']);
}
}
return unique;
};
var amenityNames = amenityName(data);
console.log(amenityNames);
data.forEach(function (d) {
d.amenities = amenityNames.map(function (name)
{
console.log( +d["sum"]);
return {name: name, value: +d["sum"]};
});
});
x0.domain(data.map(function (d) {
return d.userid;
}));
x1.domain(amenityNames).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function (d) {
return d3.max(d.amenities,
function (d) {
return d.value;
});
})]);
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("Population");
var state = svg.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function (d) {
return "translate(" + x0(d.userid) + ",0)";
});
state.selectAll("rect")
.data(function (d) {
console.log(d.amenities);
return d.amenities;
})
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function (d) {
return x1(d.name);
})
.attr("y", function (d) {
return y(d.value);
})
.attr("height", function (d) {
return height - y(d.value);
})
.style("fill", function (d) {
return color(d.name);
});
});
json sample
[{"userid":1,"amenity":"time","sum":28},
{"userid":1,"amenity":"kiii","sum":33},
{"userid":1,"amenity":"food","sum":5},
{"userid":1,"amenity":"taste","sum":32},
{"userid":1,"amenity":"hmmmm","sum":10},
{"userid":1,"amenity":"max","sum":19.5},
{"userid":1,"amenity":"youtube","sum":10},
{"userid":1,"amenity":"f","sum":2.5},
{"userid":1,"amenity":"smith","sum":2.5},]