I have a set of X an Y coordinates being returned from a javascript function in JSON format. Here's the format:
[{"x":1,"y":0},
{"x":3.6385872025818373,"y":1.6200033965859757},{"x":0.9503872615007972,"y":1.0555119865570302},{"x":0.4389065636069681,"y":1.3508155051713286},{"x":-0.4163295056303866,"y":3.961110649613818},{"x":-0.4999999999999998,"y":0.8660254037844387},{"x":-3.2222576969514476,"y":2.3411072530278374},{"x":-1.3892938251077656,"y":0.29530351861429965},{"x":3.6385872025818435,"y":-1.6200033965859781},
{"x":1,"y":-2.4492935982947064e-16}]
If I use JSON.parse the string then converts to:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Here's my events:
I'm trying to then either convert these to an array so I can draw those points on a Drawing Canvas, OR write Javascript to loop through the data and set up the drawing points/lines. I'm unable to get C3 to read either JSON or parsed JSON into an array using Load From JSON String.
Nor can I get javascript to take control of the canvas. Here's the javascript I use to draw the points, it doesn't even do the alert:
function drawPoints(jsonString) {
let points = JSON.parse(jsonString);
const canvas = runtime.objects.MyCanvas.getFirstInstance();
canvas.ctx.beginPath();
for (let i = 0; i < points.length; i++) {
if (i === 0) {
canvas.ctx.moveTo(points[i].x, points[i].y);
} else {
canvas.ctx.lineTo(points[i].x, points[i].y);
}
}
alert("Done");
canvas.ctx.closePath();
canvas.ctx.stroke();
}
I'd prefer to be able to just turn the X,Y coordinates in my json string into an array. I'm also not trying to save these points to a json to then reload through AJAX, as I already have the data stored in a string.