I have a JSON object and I'm trying to plot the data on a line graph using Chart.Js. I'm able to do it succesfully with a single event, but I'm having trouble charting events that cover multiple geographic locations.
[{"DT":"2018-10-10","LOCATIONOOS":0,"GRP":"GEORGIA"},{"DT":"2018-10-10","LOCATIONOOS":6,"GRP":"ALABAMA/MISSISSIPPI/LOUISIANA"},{"DT":"2018-10-11","LOCATIONOOS":13,"GRP":"ALABAMA/MISSISSIPPI/LOUISIANA"},{"DT":"2018-10-11","LOCATIONOOS":195,"GRP":"GEORGIA"},{"DT":"2018-10-12","LOCATIONOOS":66,"GRP":"GEORGIA"},{"DT":"2018-10-12","LOCATIONOOS":3,"GRP":"ALABAMA/MISSISSIPPI/LOUISIANA"},{"DT":"2018-10-13","LOCATIONOOS":0,"GRP":"ALABAMA/MISSISSIPPI/LOUISIANA"},{"DT":"2018-10-13","LOCATIONOOS":20,"GRP":"GEORGIA"},{"DT":"2018-10-14","LOCATIONOOS":10,"GRP":"GEORGIA"},{"DT":"2018-10-14","LOCATIONOOS":0,"GRP":"ALABAMA/MISSISSIPPI/LOUISIANA"},{"DT":"2018-10-15","LOCATIONOOS":0,"GRP":"ALABAMA/MISSISSIPPI/LOUISIANA"},{"DT":"2018-10-15","LOCATIONOOS":5,"GRP":"GEORGIA"},{"DT":"2018-10-16","LOCATIONOOS":2,"GRP":"GEORGIA"},{"DT":"2018-10-17","LOCATIONOOS":2,"GRP":"GEORGIA"}]
First: I am getting the unique dates from the JSON object to use as my x-axis labels:
//get the unique dates from the json obj which will be used as labels on the chart
labels = [];
var labels = [];
for(i = 0; i< obj.length; i++){
if(labels.indexOf(obj[i].DT) === -1){
labels.push(obj[i].DT);
}
}
Next I am doing the same things for the GRP (locations). I believe I need this data because that will be the amount of lines I'd like to display on my graph:
locations = [];
for(i = 0; i< obj.length; i++){
if(locations.indexOf(obj[i].GRP) === -1){
locations.push(obj[i].GRP);
}
}
Finally - I somehow have to loop over the JSON obj and push data to (in this case) two different datasets to chart on the graph. This is the part I am fuzzy about... I believe I probably need a nested loop with the outer loop going over the JSON obj and the inner loop going over the locations array and pushing data into two different datasets object...
locationoos = [];
for(i = 0; i< obj.length; i++){
var data = {
labels: labels,
datasets: [{
label: locations[0],
borderColor: "red",
borderWidth: 2,
hoverBackgroundColor: "red",
hoverBorderColor: "red",
data: locationsoos,
fill: false
}]
}
}
Anyone have experience doing this?
Full function:
function initGraph() {
var labels = [];
var counts = [];
var grp = [];
$.ajax({
type: "GET",
url: "../test/cfc/test.cfc",
cache: false,
async: false,
data: { method: "getChartData",
Id: 29,
},
success: function(output) {
json = output;
obj = JSON.parse(json);
},
error: function(httpRequest, textStatus, errorThrown) {
alert("initGraph status=" + textStatus + ",error=" + errorThrown);
}
});
//get the unique dates from the json obj which will be used as labels on the chart
var labels = [];
for(i = 0; i< obj.length; i++){
if(labels.indexOf(obj[i].DT) === -1){
labels.push(obj[i].DT);
}
}
//select the unique market clusters
var locations = [];
for(i = 0; i< obj.length; i++){
if(locations.indexOf(obj[i].GRP) === -1){
locations.push(obj[i].GRP);
}
}
locationsoos=[]
//loop over obj.length
for(i = 0; i< obj.length; i++){
//loop over locations array
for(j = 0; j< locations.length; j++){
var data = {
labels: labels,
datasets: [{
label: locations[0],
//backgroundColor: "",
borderColor: "red",
borderWidth: 2,
hoverBackgroundColor: "red",
hoverBorderColor: "red",
data: locationsoos,
fill: false
}]
}
}
};
var options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
precision: 0
}
}]
},
title: {
display: true,
text: 'Locations OOS vs Time',
fontStyle: 'bold',
fontColor: 'blue',
position: 'top',
fontSize: 14
}
};
Chart.Line('mobGraph', {
data: data,
options: options
});
}