Currently, I am utilizing data from a JSON object to draw lines on a canvas:
var data = { values:[
{ X: "04-28", Xt: "7:45 pm", Ys: 116, Yd: 74, Yp: 0},
{ X: "04-29", Xt: "2:00 am", Ys: 112, Yd: 73, Yp: 0},
//
The lines are drawn using jQuery with the following code:
c.lineWidth = 4;
c.strokeStyle = '#f00';
c.beginPath();
c.moveTo(getXPixel(0), getYPixel(data.values[0].Ys));
for(var i = 1; i < data.values.length; i ++) {
c.lineTo(getXPixel(i), getYPixel(data.values[i].Ys));
}
c.stroke();
In order to draw multiple lines (including 'Yd', 'Yp', and potentially more lines in the future), I intend to create a function that can be called like this:
drawLine(4, '#f00', 'Ys');
I attempted to create the function as follows:
function drawLine(width, style, d) {
c.lineWidth = width;
c.strokeStyle = style;
c.beginPath();
c.moveTo(getXPixel(0), getYPixel(data.values[0].d));
for(var i = 1; i < data.values.length; i ++) {
c.lineTo(getXPixel(i), getYPixel(data.values[i].d));
}
c.stroke();
}
Unfortunately, this approach did not yield the desired results. While there were no errors, no lines were drawn. However, when I hardcoded 'Ys' into the function, it worked fine. How can I successfully pass this snippet of data as an argument?