I am new to d3.js and my JavaScript skills are at a basic level. Thank you for your assistance, it is greatly appreciated.
Total Clicks per Campaign
I have a CSV file with columns: "Campaign" and "Clicked". The "Clicked" column contains values: Clicked / No Click. I want to show the total number of clicks per campaign, excluding "No Click". I created a function to count the clicks and store the count in each data member. I used d.count to set the y domain. The y-axis and x-axis display correctly, but the values in the bars do not appear. I suspect that these two lines of code are incorrect:
.attr("y", f
unction(d) { return y(d.key); })
.attr("height"
, function(d) { return height - y(d.key); });
The console shows this error:
d3.v4.min
.js:2 Error: attribute height: Expected length, "NaN". d3.v4.min.j
s:2 Error: attribute height: Expected length, "NaN"
Since there are two values for the "Clicked" column: "Clicked" and "No Click", should I exclude "No Click" from my d3.nest() function?
How can I display the values of "Clicked" per Campaign? What am I overlooking?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>barChart</title>
</head>
<style> /* set the CSS */
.bar { fill: steelblue; }
</style>
<body>
<!-- load the d3.js library -->
<script src="d3.v4.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<!--<script src="d3.min.js"></script>-->
<script>
// set the dimensions and margins of the graph
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
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
d3.csv("EmailMarketingCampaign_Data.csv")
.row(function(d){ return {Campaign: (d.Campaign), Clicked: (d.Clicked)}; })
.get(function(error,data){
console.log(data[0]);
// format all data from strings
data.forEach(function(d) {
d.data = +d.data;
});
// Array [ Object, Object ] Key: Clicked, Key: No Clicked
var nested_data = d3.nest()
.key(function(d) { return d.Clicked; })
.rollup(function(values) {
return values.length;
})
.entries(data);
console.log(nested_data);
// count all clicked to set range for y axis
var countObj = {};
// count Clicked
data.forEach(function(d) {
var Clicked = d.Clicked;
if(countObj[Clicked] === undefined) {
countObj[Clicked] = 0;
} else {
countObj[Clicked] = countObj[Clicked] + 1;
}
});
// now store the count in each data member
data.forEach(function(d) {
var Clicked = d.Clicked;
d.count = countObj[Clicked];
});
console.log(countObj);
// Scale the range of the data in the domains
x.domain(data.map(function(d) { return d.Campaign; }));
y.domain([0, d3.max(data, function(d) { return d.count; })]);
// append the rectangles for the bar chart
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.Campaign); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d.key); })
.attr("height", function(d) { return height - y(d.key); });
// add the x Axis
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
svg.append("g")
.call(d3.axisLeft(y));
});
</script>
</body>
</html>