Reputation: 13
I am writing a program that will eventually generate a graph based on user input. so I've created a proof of concept that should click a button and make a chart.js scatter graph my issue is that the data is not included for some reason not sure why.
<html>
<head>
<title>Test1</title>
<script src="Chart.min.js"></script>
</head>
<body>
<button onmousedown="addData()"></button>
<script>
function addData(){
ABC=[[1,2,1],[2,2,2],[3,3,3]];
chart(ABC);
}
function chart(array)
{
for (counter = 0; counter < array.length; counter++)
{
var entery = {x: array[counter][0], y: array[counter][3]};
storage.push(entery);
}
document.write('<canvas id="lineChart" height="200" width="450"></canvas>');
const chart=document.getElementById("lineChart");
console.log(chart)
var myChart = new Chart(chart, {
type: 'scatter',
data: {
datasets: [{
"fill":false,
pointRadius: 10,
label: 'Scatter Dataset',
data: storage
}]
},
options: {
scales: {
xAxes: [{
type: 'linear',
}]
}
}
});
}
</script>
</body>
</html>
I cannot work out why the data is not included any help would be appreciated
Upvotes: 1
Views: 863
Reputation: 2085
You have some issues with setting up your code:
storage
arrayconsole.log(chart)
throws an error, you have to comment that line out or remove itvar entery = {x: array[counter][0], y: array[counter][3]};
should be changed to var entery = {x: array[counter][0], y: array[counter][2]};
So, your code should look like this:
<html>
<head>
<title>Test1</title>
<script src="Chart.min.js"></script>
</head>
<body>
<button onclick="addData();">Test</button>
<script type="text/javascript">
function addData(){
ABC=[[1,2,1],[2,2,2],[3,3,3]];
chart(ABC);
}
function chart(array)
{
var storage = [];
for (counter = 0; counter < array.length; counter++)
{
var entery = {x: array[counter][0], y: array[counter][2]};
storage.push(entery);
}
document.write('<canvas id="lineChart" height="200" width="450"></canvas>');
const chart = document.getElementById("lineChart");
//console.log(chart)
var myChart = new Chart(chart, {
type: 'scatter',
data: {
datasets: [{
"fill":false,
pointRadius: 10,
label: 'Scatter Dataset',
data: storage
}]
},
options: {
scales: {
xAxes: [{
type: 'linear',
}]
}
}
});
}
</script>
</body>
</html>
Also here you can find a working copy of the chart.
Upvotes: 1