<html>
<head>
<title>JS Charts</title>
</head>
<script type="text/javascript">
var Apple = [];
var Samsung = [];
var Nokia = [];
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'https://api.myjson.com/bins/1igag', true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
}
xobj.send(null);
}
loadJSON(function(response) {
var response;
var field=JSON.parse(response);
for (var i = 0; i < field.length; i++) {
Apple.push(field[i].xxx);
Samsung.push((field[i].xxx)+10);
Nokia.push((field[i].xxx)-30);
}
sections = 12;
Val_max = 130;
Val_min = -40;
var stepSize = 10;
var columnSize = 50;
var rowSize = 50;
var margin = 10;
var xAxis = [" ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
context.fillStyle = "#0099ff"
context.font = "20 pt Verdana"
yScale = (canvas.height - columnSize - margin) / (Val_max - Val_min);
xScale = (canvas.width - rowSize) / sections;
context.strokeStyle="#009933"; // color of grid lines
context.beginPath();
// print Parameters on X axis, and grid lines on the graph
for (i=1;i<=sections;i++) {
var x = i * xScale;
context.fillText(xAxis[i], x,columnSize - margin);
context.moveTo(x, columnSize);
context.lineTo(x, canvas.height - margin);
}
// print row header and draw horizontal grid lines
var count = 0;
for (scale=Val_max;scale>=Val_min;scale = scale - stepSize) {
var y = columnSize + (yScale * count * stepSize);
context.fillText(scale, margin,y + margin);
context.moveTo(rowSize,y)
context.lineTo(canvas.width,y)
count++;
}
context.stroke();
context.translate(rowSize,canvas.height + Val_min * yScale);
context.scale(1,-1 * yScale);
// Color of each dataplot items
context.strokeStyle="#FF0066";
plotData(Apple);
context.strokeStyle="#9933FF";
plotData(Samsung);
context.strokeStyle="#000";
plotData(Nokia);
function plotData(dataSet) {
context.beginPath();
context.moveTo(0, dataSet[0]);
for (i=1;i<sections;i++) {
context.lineTo(i * xScale, dataSet[i]);
}
context.stroke();
}
});
</script>
<body>
<div align="center">
<h2>Monthly Profits of Companies(in million $)</h2>
<canvas id="canvas" height="400" width="650">
</canvas>
<br>
<!--Legends for Dataplot -->
<span style="color:#FF0066"> Apple </span>
<span style="color:#9933FF"> Samsung</span>
<span style="color:#000"> Nokia </span>
</div>
</body>
</html>
Hi. I need to plot a linear graph. Here I have taken the sample. Now I need to take the data for both x and y from the json file. How I can do that. I am not allowed to use any library or API only HTML,CSS and Javascript are allowed.Can anyone please tell me. My json data looks like
[{"aa":{
"total_visits":"925",
"2017-07-29":{
"visits":38,
"leads":0
},
"total_leads":13
},
"bb":{
"total_visits":"144",
"2017-07-29":{
"visits":1,
"leads":0
},
"total_leads":1
},
"cc":{
"last_recorded":"2017-07-29",
"total_visits":"1386",
"2017-07-29":{
"visits":41,
"leads":0
},
"total_leads":12
},
"dd":{
"total_visits":"2364",
"2017-07-29":{
"visits":55,
"leads":2.1
},
"total_leads":59
},
"ee":{
"2017-07-29":{
"visits":44,
"leads":0
},
"total_leads":37
},
"ff":{
"total_leads":2
},
"gg":{
"total_leads":1
},
"hh":{
"total_visits":"115",
"2017-07-29":{
"visits":2,
"leads":0
},
"package_id":"2",
"total_leads":3
},
"ii":{
"total_visits":"2213",
"2017-07-29":{
"visits":94,
"leads":0
},
"total_leads":87
}
}]
I need to take the total_visits(or)visits(x-axis) and total_leads(or)leads(y-axis) and plot the graph. Thanks in advance.