I am trying to understand how to run some code. I would like to have the circles plot with my data from the JSON file. I am not too sure how to go about this. I think I am messing up with not connecting the files or maybe my JSON file isn't set up correctly....my end goal is to make a connected scatterplot so, for now, I am just focusing on the circles.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<svg id="s" xmlns="http://www.w3.org/2000/svg" />
<script type="text/javascript">
function makeSVG(tag, attrs) {
var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var k in attrs)
el.setAttribute(k, attrs[k]);
return el;
}
var data;
$.getJSON("https://raw.githubusercontent.com/Nataliemcg18/Data/master/temp.json", function(data) {
draw(data);
});
var circle = makeSVG('circle', {
cx: 100,
cy: 50,
r: 40,
stroke: 'black',
'stroke-width': 2,
fill: 'red'
});
var data
document.getElementById('s').appendChild(circle);
</script>
<svg width="1000" height="1000">
<line x1="200" y1="100" x2="200" y2="600" height="1000" width="1000" style="stroke:black;stroke-width:2" />
<line x1="200" y1="600" x2="900" y2="600" height="1000" width="1000" style="stroke:black;stroke-width:2" /> <!-- bottom line -->
[
{"Month": 0, "Temperature": 32},
{"Month": 1, "Temperature": 43},
{"Month": 2, "Temperature": 52},
{"Month": 3, "Temperature": 60},
{"Month": 4, "Temperature": 70},
{"Month": 5, "Temperature": 80},
{"Month": 6, "Temperature": 90},
{"Month": 7, "Temperature": 80},
{"Month": 8, "Temperature": 65},
{"Month": 9, "Temperature": 45},
{"Month": 10, "Temperature": 33},
{"Month": 11, "Temperature": 24}
]
What about something like this ? 2 values - zero and max added just for fun... You can still read and pass data from original jQuery request.
// 1 (-2) new functions
function draw(data) {
var placeHolder = document.getElementById('s');
const diameter = 25, height = placeHolder.previousElementSibling.previousElementSibling.y2.baseVal.value - placeHolder.previousElementSibling.previousElementSibling.y1.baseVal.value;
setMax(data, height - 2 * diameter); // set last temperature value as chart max
for(var month in data) {
var pair = data[month];
var circle = makeSVG('circle', {
cx: pair.Month * diameter * 2 + 200 + diameter,
cy: 600 - diameter - pair.Temperature,
r: diameter,
stroke: 'black',
'stroke-width': 2,
fill: 'red'
});
placeHolder.appendChild(circle);
}
}
function setMax(data, value) {
data[data.length-1].Temperature = value;
}
// Your untouched logic
$.getJSON("https://raw.githubusercontent.com/Nataliemcg18/Data/master/temp.json", function(data) {
draw(data);
});
function makeSVG(tag, attrs) {
var el = document.createElementNS('http://www.w3.org/2000/svg', tag);
for (var k in attrs)
el.setAttribute(k, attrs[k]);
return el;
}
var circle = makeSVG('circle', {
cx: 100,
cy: 50,
r: 40,
stroke: 'black',
'stroke-width': 2,
fill: 'red'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<svg width="1000" height="1000">
<line x1="200" y1="100" x2="200" y2="600" height="1000" width="1000" style="stroke:black;stroke-width:2" />
<line x1="200" y1="600" x2="900" y2="600" height="1000" width="1000" style="stroke:black;stroke-width:2" />
<svg id="s" xmlns="http://www.w3.org/2000/svg" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With