0

Hi I am using this jQuery plugin jCanvas for draving lines.

It work perfect

    $("canvas").drawLine({   
    strokeStyle: "#d96363",   
    strokeWidth: 5,  
    x1: 68, y1: 318,   
    x2: 176, y2: 158,   
    x3: 566, y3: 138,   
    x4: 559, y4: 199,   
    x5: 68, y5: 318 
});

But in my javascript source is generating string suradnice which looks (for example with same values) :

x1: 68, y1: 318, x2: 176, y2: 158, x3: 566, y3: 138, x4: 599, y4: 199, x5: 68, y5: 318

and I need to draw lines with values in the string suradnice

function draw_graph(suradnice){

  $("canvas").drawLine({
  strokeStyle: "#d96363",
  strokeWidth: 5,

  suradnice //Here 

}));
} 

How can I fix it?

Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62

2 Answers2

1

What you're doing looks like you're trying to use a C-style macro to insert your values. Unfortunately there is no such feature in JavaScript.

However, if your data were formatted like this:

var suradnice = '{"x1":68,"y1":318.....}';

Then you would be able to do this:

var tmp = JSON.parse(suradnice);
tmp.strokeStye = "#d96363";
tmp.strokeWidth = 5;
$("canvas").drawLine(tmp);
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • does not work I do not have I idea how to format it correctly :/ I tried many options :/ – Lukáš Von Jančík Jan 27 '13 at 00:14
  • This is way how I make parts of string in function: `code suradnice[p-1]='"x' +p+ '":'68 , '"y' +p+ '":' 318; return suradnice[p-1];` and this is how I call function and get string together: `code min_suradnice[0]= kresli(garage,1)+ ", " +kresli(z_miest[0],2)+ ", " +kresli(z_miest[1],3)+ ", " +kresli(garage,4); ` – Lukáš Von Jančík Jan 27 '13 at 00:38
0

You can use eval, as variant:

obj = eval('({' + suradnice + '})');
obj.strokeStyle = '#d96363';
obj.strokeWidth = 5;
$("canvas").drawLine(obj);

More variants you can find in this question answers.

Community
  • 1
  • 1
Vlad
  • 3,626
  • 16
  • 14
  • does not work I do not have I idea how to format it correctly :/ – Lukáš Von Jančík Jan 27 '13 at 00:14
  • @LukášVonJančík Yes, i mistake, need adding staples: var obj = eval('({' + suradnice + '})'); and all working: http://my.jetscreenshot.com/6795/20130127-ueey-21kb – Vlad Jan 27 '13 at 01:46
  • it works if I manualy set string but I need to draw generated string. I have got switch in kresli() where goes name of city and counter "p" (xp - x1 or x2 ..) `code case "Bratislava": suradnice[p-1]="x" +p+ ":68 ,y" +p+ ":318"; break; return suradnice[p-1];` and then I call `code min_suradnice[0]=kresli(garage,1)+ ", " +kresli(z_miest[0],2)+ ", " +kresli(z_miest[1],3)+ ", " +kresli(garage,4);` I dont have idea how to fic it correctly :/ – Lukáš Von Jančík Jan 27 '13 at 10:02