Having done some hunting I thought I was getting close to a solution a couple of times by alas still no luck!
Fully aware that this post was created yesterday: React Chartjs, how to handle a dynamic number of datasets and is a very similar question but sadly no answers.
I am using Chart.JS version 2 and have a script which processes an AJAX request to load the variable number of datasets and their corresponding data.
The dataset label and values are actually displayed and accessed by the script in text inputs within a form.
Accessing everything for the most part is fine but creating a loop to create the datasets is where I keep failing.
I was hoping that this could just be a simple loop but I cannot get it to work and having found a couple of other solutions but having attempted to combine their solutions with my script still no luck.
Script is below. The "GraphLabelString" input is being access no problem as are the other form elements, although the code to get the dataset label and values is currently commented out as I'll cross that bridge once I get the loop to work.
Any help greatly apprieciated.
function LoadProjectActivityGraph(ProjectRef, StartDate, EndDate)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("ProjectActivityGraph").innerHTML = xmlhttp.responseText;
//GET THE VALUE OF THE GRAPH LABELS FORM INPUT
var GraphLabelString = document.forms.GraphData.GraphLabels.value;
//TURN THE VALUE OF THE ABOVE INTO AN ARRAY
var GraphLabelArray = GraphLabelString.split(",");
//GET THE VALUE OF THE TOTAL NUMBER OF DATASETS INPUTS
var GraphTotalDatasets = parseInt(document.forms.GraphData.TotalDatasets.value);
//SET THE GRAPH CONFIGURATION VALUES
var LineChartConfig = {
type: 'line',
data: {
labels: GraphLabelArray,
datasets:
[
for (i=0; i < GraphTotalDatasets; i++)
{
{
label: 'apples',
//label: document.forms.GraphData['GraphDatasetLabel'+i].value,
data: [12, 19, 3, 17, 6, 3, 7],
//data: [document.forms.GraphData['GraphDatasetValues'+i].value],
fill: false,
borderColor: "red",
backgroundColor: "red",
pointBackgroundColor: "#ffffff",
tension: 0,
},
}
]
}
};
var ctx = document.getElementById("ProjectActivityGraphCanvas").getContext("2d");
window.myLine = new Chart(ctx, LineChartConfig);
}
}
xmlhttp.open("GET", "ProjectManagement/HoursManagement_AJAX/Response_ProjectActivityGraph.asp?ProjectRef="+ProjectRef+"&StartDate="+StartDate+"&EndDate="+EndDate, true);
xmlhttp.send();
}
LineChartConfigand then just dodatasets: array?