我最近将d3.js代码从v3切换到了v4,但是在较新版本中,轴显示但是图形不是。 我的旧代码是
var margin = {top: 4, right: 0, bottom: 30, left: 40},
width = 960 - 12*margin.left - margin.right,
height = 500 - 45*margin.top - margin.bottom;
var x0 = d3.scale.ordinal()
.rangeRoundBands([0, width-40], 0.1);
var x1 = d3.scale.ordinal();
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x0)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var color_range = [$('#correct_h').css('background-color'),
$('#correct_m').css('background-color'),
$('#correct_l').css('background-color'),
$('#wrong_h').css('background-color'),
$('#wrong_m').css('background-color'),
$('#wrong_l').css('background-color'),
$('#na_h').css('background-color'),
$('#na_m').css('background-color'),
$('#na_l').css('background-color')];
var color = d3.scale.ordinal()
.range(color_range);
//d3.selectAll("svg > g > *").remove();
var svg = d3.select( d3.selectAll("#d3select")[0].pop() ).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 + ")");
var yBegin;
var innerColumns = {
"column1" : ["Correct H","Correct M","Correct L"],
"column2" : ["Wrong H","Wrong M","Wrong L"],
"column3" : ["NA H","NA M","NA L"]
}
var columnHeaders = d3.keys(data[0]).filter(function(key) { return key !== "Section"; });
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Section"; }));
data.forEach(function(d) {
var yColumn = new Array();
d.columnDetails = columnHeaders.map(function(name) {
for (ic in innerColumns) {
if($.inArray(name, innerColumns[ic]) >= 0){
if (!yColumn[ic]){
yColumn[ic] = 0;
}
yBegin = yColumn[ic];
yColumn[ic] += +d[name];
return {name: name, column: ic, yBegin: yBegin, yEnd: +d[name] + yBegin,};
}
}
});
d.total = d3.max(d.columnDetails, function(d) {
return d.yEnd;
});
});
x0.domain(data.map(function(d) { return d.Section; }));
x1.domain(d3.keys(innerColumns)).rangeRoundBands([0, x0.rangeBand()]);
y.domain([0, d3.max(data, function(d) {
return d.total;
})]);
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", 3)
.attr("dy", ".7em")
.style("text-anchor", "end")
.text("");
var project_stackedbar = svg.selectAll(".project_stackedbar")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.Section) + ",0)"; });
project_stackedbar.selectAll("rect")
.data(function(d) { return d.columnDetails; })
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function(d) {
return x1(d.column);
})
.attr("y", function(d) {
return y(d.yEnd);
})
.attr("height", function(d) {
return y(d.yBegin) - y(d.yEnd);
})
.style("fill", function(d) { return color(d.name); });
但在v4中,当我进行更改时,没有发生错误,但图表也没有显示。我正在使用的新代码是
var margin = {top: 4, right: 0, bottom: 30, left: 40},
width = 960 - 12*margin.left - margin.right,
height = 500 - 45*margin.top - margin.bottom;
var x0 = d3.scaleBand()
.rangeRound([0, width-40], 0.1);
var x1 = d3.scaleBand();
var y = d3.scaleLinear()
.range([height, 0]);
var xAxis = d3.axisBottom()
.scale(x0)
var yAxis = d3.axisLeft()
.scale(y)
.tickFormat(d3.format(".2s"));
var color_range = [$('#correct_h').css('background-color'),
$('#correct_m').css('background-color'),
$('#correct_l').css('background-color'),
$('#wrong_h').css('background-color'),
$('#wrong_m').css('background-color'),
$('#wrong_l').css('background-color'),
$('#na_h').css('background-color'),
$('#na_m').css('background-color'),
$('#na_l').css('background-color')];
var color = d3.scaleOrdinal()
.range(color_range);
//d3.selectAll("svg > g > *").remove();
var svg = d3.select("#d3select").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 + ")");
var yBegin;
var innerColumns = {
"column1" : ["Correct H","Correct M","Correct L"],
"column2" : ["Wrong H","Wrong M","Wrong L"],
"column3" : ["NA H","NA M","NA L"]
}
var columnHeaders = d3.keys(data[0]).filter(function(key) { return key !== "Section"; });
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "Section"; }));
data.forEach(function(d) {
var yColumn = new Array();
d.columnDetails = columnHeaders.map(function(name) {
for (ic in innerColumns) {
if($.inArray(name, innerColumns[ic]) >= 0){
if (!yColumn[ic]){
yColumn[ic] = 0;
}
yBegin = yColumn[ic];
yColumn[ic] += +d[name];
return {name: name, column: ic, yBegin: yBegin, yEnd: +d[name] + yBegin,};
}
}
});
d.total = d3.max(d.columnDetails, function(d) {
return d.yEnd;
});
});
x0.domain(data.map(function(d) { return d.Section; }));
x1.domain(d3.keys(innerColumns)).rangeRound([0, x0.range()]);
y.domain([0, d3.max(data, function(d) {
return d.total;
})]);
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", 3)
.attr("dy", ".7em")
.style("text-anchor", "end")
.text("");
var project_stackedbar = svg.selectAll(".project_stackedbar")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x0(d.Section) + ",0)"; });
project_stackedbar.selectAll("rect")
.data(function(d) { return d.columnDetails; })
.enter().append("rect")
.attr("width", x1.bandwidth())
.attr("x", function(d) {
return x1(d.column);
})
.attr("y", function(d) {
return y(d.yEnd);
})
.attr("height", function(d) {
return y(d.yBegin) - y(d.yEnd);
})
.style("fill", function(d) { return color(d.name); });
问题的数据是
var data = [
{
"Section": "Quant",
"Correct H": 1, "Correct M": 1, "Correct L": 3, "Wrong H": 2, "Wrong M": 3, "Wrong L": 5, "NA H": 2, "NA M": 4, "NA L": 3
},
{
"Section": "PS",
"Correct H": 1, "Correct M": 1, "Correct L": 3, "Wrong H": 2, "Wrong M": 3, "Wrong L": 5, "NA H": 2, "NA M": 4, "NA L": 3
},
{
"Section": "Verbal",
"Correct H": 1, "Correct M": 1, "Correct L": 3, "Wrong H": 2, "Wrong M": 3, "Wrong L": 5, "NA H": 2, "NA M": 4, "NA L": 3
}
]