In: Computer Science
My code is supposed to display a stacked bar chart of my average week. Can you help me locate my errors and fix them? I'm just so confused
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset="UTF-8" />
<title>Assignment 3: My Weekly Schedule</title>
<script src="https://d3js.org/d3.v3.min.js"
charset="utf-8"></script>
<style type="text/css">
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
.axis path,
.axis line{
fill: none;
stroke: #000;
}
path.domain {
stroke: none;
}
.y .tick line{
stroke: #ddd;
}
</style>
</head>
<body>
<script type="text/javascript">
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 960 - margin.left - margin.right,
height = 500 + margin.top + margin.bottom;
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 +
")");
var data = [
{weekday: "Monday", eat: "2", sleep: "6", work: "8", study: "4",
relax: "1", dance: "3"},
{weekday: "Tuesday", eat: "2", sleep: "6", work: "8", study: "4",
relax: "1", dance: "3"},
{weekday: "Wednesday", eat: "2", sleep: "6", work: "8", study: "4",
relax: "1", dance: "3"},
{weekday: "Thursday", eat: "2", sleep: "6", work: "8", study: "4",
relax: "1", dance: "3"},
{weekday: "Friday", eat: "2", sleep: "6", work: "8", study: "4",
relax: "1", dance: "3"},
{weekday: "Saturday", eat: "3", sleep: "5", work: "0", study: "2",
relax: "9", dance: "5"},
{weekday: "Sunday", eat: "3", sleep: "15", work: "0", study: "0",
relax: "4", dance: "2"},
];
var parse = d3.time.format("%A").parse;
//shifts data to layers
var dataset = d3.layout.stack()(["eat", "sleep", "work", "study",
"relax", "dance"].map(function(activities){
return data.map(function(d){
return {x: parse(d.weekday), y: +d[activities]};
});
activitie
//set the x and y colours
var x = d3.scale.ordinal()
.domain(dataset[0].map(function(d) { return d.x;}))
.rangeRoundBands([10, width-20] 0.02);
var y = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) {return d3.max(d,
function(d) {return d.y0 + d.y; }); })])
.range([height, 0]);
var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574", "#5fca27", "#0076b2", "#224060"];
// Define and draw axes
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width, 0, 0)
.tickFormat( function(d) { return d } );
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(d3.time.format("%A"));
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//create groups for each series and rectangles for each segment
var groups = svg.selectAll("g.cost")
.data(dataset)
.enter().append("g")
.attr("class", "cost")
.style("fill", function(d, i) { return colors[i];});
var rect = groups.selectAll("rect")
.data(function(d) {return d; })
.enter()
.append("rect")
.attr("x", function(d) {return x(d.x); })
.attr("y", function(d) { return y(d.y0 + d.y); })
.attr("height", function(d) {return y(d.y0) - y(d.y0 + d.y);
})
.attr("width", x.rangeBand())
// Draw legend
var legend = svg.selectAll(".legend")
.data(colors)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19
+ ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) {return
colors.slice().reverse()[i];});
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d, i) {
switch (i) {
case 0: return "Eat";
case 1: return "Sleep";
case 2: return "Work";
case 3: return "Study";
case 4: return "Relax";
case 5: return "Dance";
}
});
</script>
</body>
</html>
Updated code:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset="UTF-8" />
<title>Assignment 3: My Weekly Schedule</title>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<style type="text/css">
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
.axis path,
.axis line{
fill: none;
stroke: #000;
}
path.domain {
stroke: none;
}
.y .tick line{
stroke: #ddd;
}
</style>
</head>
<body>
<script type="text/javascript">
var margin = {top: 20, right: 160, bottom: 35, left: 30};
var width = 960 - margin.left - margin.right,
height = 500 + margin.top + margin.bottom;
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 + ")");
var data = [
{weekday: "Monday", eat: "2", sleep: "6", work: "8", study: "4", relax: "1", dance: "3"},
{weekday: "Tuesday", eat: "2", sleep: "6", work: "8", study: "4", relax: "1", dance: "3"},
{weekday: "Wednesday", eat: "2", sleep: "6", work: "8", study: "4", relax: "1", dance: "3"},
{weekday: "Thursday", eat: "2", sleep: "6", work: "8", study: "4", relax: "1", dance: "3"},
{weekday: "Friday", eat: "2", sleep: "6", work: "8", study: "4", relax: "1", dance: "3"},
{weekday: "Saturday", eat: "3", sleep: "5", work: "0", study: "2", relax: "9", dance: "5"},
{weekday: "Sunday", eat: "3", sleep: "15", work: "0", study: "0", relax: "4", dance: "2"},
];
//var parse = d3.time.format("%A").parse;
//shifts data to layers
var dataset = d3.layout.stack()(["eat", "sleep", "work", "study", "relax", "dance"].map(function(activities){
return data.map(function(d){
return {x: d.weekday, y: +d[activities]};
})}));
//set the x and y colours
var x = d3.scale.ordinal()
.domain(dataset[0].map(function(d) { return d.x;}))
.rangeRoundBands([10, width-10],0.07);
var y = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) {return d3.max(d, function(d) {return d.y0 + d.y; }); })])
.range([height, 0]);
var colors = ["b33040", "#d25c4d", "#f2b447", "#d9d574", "#5fca27", "#0076b2", "#224060"];
// Define and draw axes
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10)
.tickSize(-width, 0, 0)
.tickFormat( function(d) { return d } );
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//create groups for each series and rectangles for each segment
var groups = svg.selectAll("g.cost")
.data(dataset)
.enter().append("g")
.attr("class", "cost")
.style("fill", function(d, i) { return colors[i];});
var rect = groups.selectAll("rect")
.data(function(d) {return d; })
.enter()
.append("rect")
.attr("x", function(d) {return x(d.x); })
.attr("y", function(d) { return y(d.y0 + d.y); })
.attr("height", function(d) {return y(d.y0) - y(d.y0 + d.y); })
.attr("width", x.rangeBand())
// Draw legend
var legend = svg.selectAll(".legend")
.data(colors)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(30," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", function(d, i) {return colors.slice().reverse()[i];});
legend.append("text")
.attr("x", width + 5)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "start")
.text(function(d, i) {
switch (i) {
case 0: return "Eat";
case 1: return "Sleep";
case 2: return "Work";
case 3: return "Study";
case 4: return "Relax";
case 5: return "Dance";
}
});
</script>
</body>
</html>
Output: