The attached code should produce a few coloured circles, and indeed it does when using a hard-coded dataset of [1,4,2,3,5].
However, I don't understand why the code below isn't working using my JSON data array.
The console in the browser is showing the JSON query is successfully running and pulling the data into the page - just nothing seems to happen with it.
The JSON query is as follows:
[{"Id":1,"UserID":"JNP","HCRef":"ProjSetup","AnswerString":"","Answerlist":1},
{"Id":2,"UserID":"JNP","HCRef":"ProjSetup","AnswerString":null,"Answerlist":2},
{"Id":3,"UserID":"JNP","HCRef":"ProjSetup","AnswerString":null,"Answerlist":5},
{"Id":4,"UserID":"JNP","HCRef":"ProjSetup","AnswerString":null,"Answerlist":6},
{"Id":5,"UserID":"JNP","HCRef":"ProjSetup","AnswerString":null,"Answerlist":3},
{"Id":6,"UserID":"JNP","HCRef":null,"AnswerString":null,"Answerlist":4}]
What I want to happen, is use the Answerlist variable to be returned in the returncolour section of the code and drive the colour of the circles. The data is otherwise only used to trigger a circle being drawn at all (using the 'i' parameter).
As I say, the code works fine doing this with a hardcoded dataset.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<style type="text/css">
</style>
</head>
<body>
<script type="text/javascript">
var dataset = d3.json('/api/answers/', function (d) {
console.log(d);
});
var w = 1000;
var h = 1000;
// var dataset = [1,4,2,3,5];
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var circles = svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle");
circles.attr("cx", function (d, i)
{
var Xaxis;
if (i === 0) { Xaxis = "500"; }
else if (i === 1) { Xaxis = "400"; }
else if (i === 2) { Xaxis = "420"; }
else if (i === 3) { Xaxis = "452.5"; }
else if (i === 4) { Xaxis = "485"; }
else if (i === 5) { Xaxis = "515"; }
else if (i === 6) { Xaxis = "547.5"; }
else if (i === 7) { Xaxis = "580"; }
else if (i === 8) { Xaxis = "600"; }
else if (i === 9) { Xaxis = "600"; }
else if (i === 10) { Xaxis = "650"; }
else if (i === 11) { Xaxis = "700"; }
else if (i === 12) { Xaxis = "750"; }
else if (i === 13) { Xaxis = "750"; }
else if (i === 14) { Xaxis = "750"; }
else if (i === 15) { Xaxis = "750"; }
else if (i === 16) { Xaxis = "750"; }
return Xaxis;
})
circles.attr("cy", function (d, i) {
var Yaxis;
if (i === 0) { Yaxis = "500"; }
else if (i === 1) { Yaxis = "500"; }
else if (i === 2) { Yaxis = "535"; }
else if (i === 3) { Yaxis = "560"; }
else if (i === 4) { Yaxis = "585"; }
else if (i === 5) { Yaxis = "585"; }
else if (i === 6) { Yaxis = "560"; }
else if (i === 7) { Yaxis = "535"; }
else if (i === 8) { Yaxis = "500"; }
else if (i === 9) { Yaxis = "600"; }
else if (i === 10) { Yaxis = "550"; }
else if (i === 11) { Yaxis = "500"; }
else if (i === 12) { Yaxis = "450"; }
else if (i === 13) { Yaxis = "600"; }
else if (i === 14) { Yaxis = "550"; }
else if (i === 15) { Yaxis = "500"; }
else if (i === 16) { Yaxis = "450"; }
return Yaxis;
})
.attr("r", function (d, i) {
var size;
if (i === 0) { size = "30"; }
else if (i > 0) { size = "20"; }
return size;
})
.attr("fill", function (d) {
return d.Answerlist;
var returnColor;
if (d.Answerlist === 1) { returnColor = "green"; }
else if (d.Answerlist === 2) { returnColor = "lightgreen"; }
else if (d.Answerlist === 3) { returnColor = "gold"; }
else if (d.Answerlist === 4) { returnColor = "darkorange"; }
else if (d.Answerlist === 5) { returnColor = "red"; }
else if (d.Answerlist === 6) { returnColor = "lightgrey"; }
return returnColor;
});
</script>
</body>
</html>