我在一个SVG上绘制了两个图形(Stacked Bar and Line)。两者都正确追加。我需要在同一个svg上的两个图表的图例。传说即将出现堆积条形图而非线图。代码如下:
var marginStackedLine = {top: 20, right: 20, bottom: 30, left: 35},
width = 690 - marginStackedLine.left - marginStackedLine.right,
height = 230 - marginStackedLine.top - marginStackedLine.bottom;
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .4);
var y = d3.scale.linear()
.rangeRound([height, 0]);
var color = d3.scale.ordinal()
.range(["#58585b","#00adef"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var svgStackedLine = d3.select("#callVolume").append("svg")
.attr("width", width + marginStackedLine.left + marginStackedLine.right)
.attr("height", height + marginStackedLine.top + marginStackedLine.bottom)
.append("g")
.attr("transform", "translate(" + marginStackedLine.left + "," + marginStackedLine.top + ")");
var x1 = d3.time.scale()
.range([0, width]);
var y1 = d3.scale.linear()
.range([height, 0]);
var xAxis1 = d3.svg.axis()
.scale(x1)
.orient("bottom");
var yAxis1 = d3.svg.axis()
.scale(y1)
.orient("right")
.tickFormat(d3.format("5"));
var line = d3.svg.line()
.x(function(d) { return x1(d.Days); })
.y(function(d) { return y1(d.cost); });
//Code For Stacked Bar Graph
d3.json("JSON/StackedLineBarData.json", function(error, data) {
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "key"; }));
data.forEach(function(d) {
var y0 = 0;
d.ages = color.domain().map(function(name) { return {name: name, y0: y0, y1: y0 += +d[name]}; });
d.total = d.ages[d.ages.length - 1].y1;
});
data.sort(function(a, b) { return b.total - a.total; });
x.domain(data.map(function(d) { return d.key; }));
y.domain([0, d3.max(data, function(d) { return d.total; })]);
svgStackedLine.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svgStackedLine.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)");
var state = svgStackedLine.selectAll(".state")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.key) + ",0)"; });
state.selectAll("rect")
.data(function(d) { return d.ages; })
.enter().append("rect")
.attr("width",45)
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.style("fill", function(d) { return color(d.name); });
//Legend for Stacked Bar Graph
var legend = svgStackedLine.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(" + parseInt((i*100)-80) + ",-18)"; });
legend.append("rect")
.attr("x", width - 103)
.attr("y",0)
.attr("width", 6)
.attr("height", 6)
.style("fill", color);
legend.append("text")
.attr("x", width - 90)
.attr("y", 3)
.attr("dy", ".35em")
.style("font","0.75em HelveticaNeue")
.style("fill","#939597")
.text(function(d) { return d; });
});
//Code For Line graph On Stacked Bar graph
d3.json("JSON/StackedLineData.json", function(error, data) {
data.forEach(function(d) {
d.cost = +d.cost;
});
x1.domain(d3.extent(data, function(d) { return d.Days; }));
y1.domain(d3.extent(data, function(d) { return d.cost; }));
svgStackedLine.append("g")
.attr("class", "x axis");
svgStackedLine.append("g")
.attr("class", "y axis")
.attr("transform", "translate("+ width +",0)")
.call(yAxis1);
svgStackedLine.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
//Legend For Line Graph
var legend1 = svgStackedLine.selectAll('.legend1')
.attr("transform", "translate(" + (width - 30) + ",20)")
.data(status)
.enter()
.append('g')
.attr('class', 'legend1');
legend1.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
legend1.append('rect')
.attr('x', width - 30)
.attr('y', 0)
.attr('width', 10)
.attr('height', 10);
legend1.append('text')
.attr('x', width - 8)
.attr('y',0)
.text(function(d){ return d.cost; });
});
Json对象是:
StackedLineBarData.json
[ { "key": "MON",
"Call Conversion": 30,
"Call Abaoundont": 40},
{ "key": "TUE",
"Call Conversion": 30,
"Call Abaoundont": 30 },
]
StackedLineData.json
[
{"Days":"1","cost":"10"},
{"Days":"2","cost":"20"}
]
答案 0 :(得分:1)
在折线图的图例中,您正在调用.data(status)
,其中未定义status
。如果你在那里传递了正确的数据,它应该可以工作。