我有以下d3折线图,虽然我知道有关如何使折线图响应的this教程,但我无法将其应用到现有图表中...
基本上,我希望图表能够将浏览器窗口的宽度和高度填充100%,并使用窗口调整大小。我确定我的图形代码本身很糟糕,所以任何关于清理它的指针也会非常感激。
<script type="text/javascript">
// set dimensions of the graph
var margin = { top: 30, right: 20, bottom: 30, left: 50 },
width = 1200 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// parse the date format
var parseDate = d3.time.format("%Y-%m-%d").parse;
//colors (this is new)
// Our color bands
var color = d3.scale.ordinal()
.range(["#308fef", "#5fa9f3", "#1176db"]);
// set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// define the axis
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// Adds the svg canvas
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 + ")");
// Get the data
var simple_pre_ingest_staging_data = <?php echo $simple_pre_ingest_staging_data; ?>;
var simple_backlog_data = <?php echo $simple_backlog_data; ?>;
var simple_ready_data = <?php echo $simple_ready_data; ?>;
//get data for pre-ingest
simple_pre_ingest_staging_data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
//get data for readyForIngest_data
simple_ready_data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
//get data for artworkBacklog_data
simple_backlog_data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// Scale the range of the data
x.domain(d3.extent(simple_pre_ingest_staging_data, function(d) { return d.date; }));
y.domain([0, 800]);
// draw pre-ingest
svg.append("path")
.attr("class", "pre_ingest")
.attr("d", valueline(simple_pre_ingest_staging_data))
.attr("data-legend",function(d) { return "Pre-ingest Staging"});
// draw simple_ready_data
svg.append("path")
.attr("class", "readyForIngest")
.attr("d", valueline(simple_ready_data))
.attr("data-legend",function(d) { return "Ready for ingest"});
// draw artworkBacklog_data
svg.append("path")
.attr("class", "artworkBacklog")
.attr("d", valueline(simple_backlog_data))
.attr("data-legend",function(d) { return "Artwork level backlog"});
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
legend = svg.append("g")
.attr("class","legend")
.attr("transform","translate(850,30)")
.style("font-size","12px")
.call(d3.legend)
setTimeout(function() {
legend
.style("font-size","20px")
.attr("data-style-padding",10)
.call(d3.legend)
},1000)
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width)
.attr("y", height - 6)
.text("date");
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 6)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("number of artworks");
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Number of Artworks in ingest queue");
</script>