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?

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

does not work I do not have I idea how to format it correctly :/ I tried many options :/
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);
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.

3 Comments

does not work I do not have I idea how to format it correctly :/
@LukášVonJančík Yes, i mistake, need adding staples: var obj = eval('({' + suradnice + '})'); and all working: my.jetscreenshot.com/6795/20130127-ueey-21kb
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 :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.