I need to draw a graph with three different lines. Line graph.
I tried to do this:
function draw() { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.lineWidth=10; ctx.strokeStyle="teal"; ctx.moveTo(10,CompanyA[0]); ctx.lineTo(110,CompanyA[1]); ctx.lineTo(210,CompanyA[2]); ctx.stroke(); ctx.strokeStyle="green"; ctx.moveTo(10,CompanyB[0]); ctx.lineTo(110,CompanyB[1]); ctx.lineTo(210,CompanyB[2]); ctx.stroke(); ctx.strokeStyle="yellow"; ctx.moveTo(10,CompanyC[0]); ctx.lineTo(110,CompanyC[1]); ctx.lineTo(210,CompanyC[2]); ctx.stroke(); }
But, apparently, the final touch draws for all lines. Thus, I get 3 yellow lines, instead of teak, green and yellow. I tried to create three different contexts (ctx1, ctx2 and ctx3), but for some reason they were all drawn using the call to "ctx3.stroke ()".
What would be the right way to do this?
source share