How to draw a plot in Javascript

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

How to draw a plot in Javascript

lechristophe
Hi (again)

Could someone tell me please why this Javascript code :

Xcoor = [1, 2, 3, 4];
Ycoor = [10, 20, 30, 40];
Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
plot.show();

giving me an error saying :

org.mozilla.javascript.EvaluatorException: Le choix de la méthode Java
ij.gui.Plot.ij.gui.Plot correspondant aux types d'argument JavaScript
(string,string,string,object,object) est ambigu. Les méthodes proposées
sont les suivantes�:
    Plot(java.lang.String,java.lang.String,java.lang.String,float[],float[])

Plot(java.lang.String,java.lang.String,java.lang.String,double[],double[])
(New_.js#3)

which tells me (in french) that the types of the arguments is "ambiguous".
But Javascript doesn't allow me to specify what types are my array numbers ?


Christophe
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

dscho
Hi Christophe,

On Mon, 31 Oct 2011, Christophe Leterrier wrote:

> Could someone tell me please why this Javascript code :
>
> Xcoor = [1, 2, 3, 4];
> Ycoor = [10, 20, 30, 40];
> Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
> plot.show();
>
> giving me an error saying :
>
> org.mozilla.javascript.EvaluatorException: Le choix de la méthode Java
> ij.gui.Plot.ij.gui.Plot correspondant aux types d'argument JavaScript
> (string,string,string,object,object) est ambigu. Les méthodes proposées
> sont les suivantes�:
>     Plot(java.lang.String,java.lang.String,java.lang.String,float[],float[])
>
> Plot(java.lang.String,java.lang.String,java.lang.String,double[],double[])
> (New_.js#3)
Cast to either float[] or double[] and you're set (Javascript arrays are
neither, but I'd expect they are cast'able).

Hth,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

Albert Cardona-2
2011/10/31 Johannes Schindelin <[hidden email]>:

> Hi Christophe,
>
> On Mon, 31 Oct 2011, Christophe Leterrier wrote:
>
>> Could someone tell me please why this Javascript code :
>>
>> Xcoor = [1, 2, 3, 4];
>> Ycoor = [10, 20, 30, 40];
>> Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
>> plot.show();
>>
>> giving me an error saying :
>>
>> org.mozilla.javascript.EvaluatorException: Le choix de la méthode Java
>> ij.gui.Plot.ij.gui.Plot correspondant aux types d'argument JavaScript
>> (string,string,string,object,object) est ambigu. Les méthodes proposées
>> sont les suivantes�:
>>     Plot(java.lang.String,java.lang.String,java.lang.String,float[],float[])
>>
>> Plot(java.lang.String,java.lang.String,java.lang.String,double[],double[])
>> (New_.js#3)
>
> Cast to either float[] or double[] and you're set (Javascript arrays are
> neither, but I'd expect they are cast'able).


Johannes, can one cast in javascript? I don't think so.
What Christopher can do is use native arrays directly, instead of
javascript arrays. This is necessary in this case given the
unfortunate ambiguity in the constructor signature.

http://fiji.sc/wiki/index.php/Javascript_Scripting#Native_Java_arrays

Albert
--
http://albert.rierol.net
http://www.ini.uzh.ch/~acardona/
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

Stephan Saalfeld
In reply to this post by lechristophe
Hi Christophe,

the problem is that Javascript arrays are not Java arrays (which are
special citizens in the Java universe).  To my knowledge, there is no
handy way to create them other than going the painful path through
reflections.   There is an example at the Fiji Wiki:

http://fiji.sc/wiki/index.php/Javascript_Scripting#Native_Java_arrays

Your code would explode into

Xcoor = new java.lang.reflect.Array.newInstance(java.lang.Double.TYPE,
4);
Ycoor = new java.lang.reflect.Array.newInstance(java.lang.Double.TYPE,
4);
Xcoor[0] = 1;
Xcoor[1] = 2;
Xcoor[2] = 3;
Xcoor[3] = 4;
Ycoor[0] = 10;
Ycoor[1] = 20;
Ycoor[2] = 30;
Ycoor[3] = 40;
Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
Myplot.show();

which works as expected.  This and the excessive syntax for imports made
me prefer Beanshell as my favorite scripting environment for talking to
Fiji.

Best,
Stephan




On Mon, 2011-10-31 at 18:26 +0100, Christophe Leterrier wrote:

> Hi (again)
>
> Could someone tell me please why this Javascript code :
>
> Xcoor = [1, 2, 3, 4];
> Ycoor = [10, 20, 30, 40];
> Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
> plot.show();
>
> giving me an error saying :
>
> org.mozilla.javascript.EvaluatorException: Le choix de la méthode Java
> ij.gui.Plot.ij.gui.Plot correspondant aux types d'argument JavaScript
> (string,string,string,object,object) est ambigu. Les méthodes proposées
> sont les suivantes�:
>     Plot(java.lang.String,java.lang.String,java.lang.String,float[],float[])
>
> Plot(java.lang.String,java.lang.String,java.lang.String,double[],double[])
> (New_.js#3)
>
> which tells me (in french) that the types of the arguments is "ambiguous".
> But Javascript doesn't allow me to specify what types are my array numbers ?
>
>
> Christophe
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

lechristophe
In reply to this post by Albert Cardona-2
On Mon, Oct 31, 2011 at 20:17, Albert Cardona <[hidden email]> wrote:

> 2011/10/31 Johannes Schindelin <[hidden email]>:
> > Hi Christophe,
> >
> > On Mon, 31 Oct 2011, Christophe Leterrier wrote:
> >
> >> Could someone tell me please why this Javascript code :
> >>
> >> Xcoor = [1, 2, 3, 4];
> >> Ycoor = [10, 20, 30, 40];
> >> Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
> >> plot.show();
> >>
> >> giving me an error saying :
> >>
> >> org.mozilla.javascript.EvaluatorException: Le choix de la méthode Java
> >> ij.gui.Plot.ij.gui.Plot correspondant aux types d'argument JavaScript
> >> (string,string,string,object,object) est ambigu. Les méthodes proposées
> >> sont les suivantes�:
> >>
> Plot(java.lang.String,java.lang.String,java.lang.String,float[],float[])
> >>
> >>
> Plot(java.lang.String,java.lang.String,java.lang.String,double[],double[])
> >> (New_.js#3)
> >
> > Cast to either float[] or double[] and you're set (Javascript arrays are
> > neither, but I'd expect they are cast'able).
>
>
> Johannes, can one cast in javascript? I don't think so.
> What Christopher can do is use native arrays directly, instead of
> javascript arrays. This is necessary in this case given the
> unfortunate ambiguity in the constructor signature.
>
> http://fiji.sc/wiki/index.php/Javascript_Scripting#Native_Java_arrays
>
>
Indeed, as spotted by Albert, Johannes and Stephan, it is a problem of
variable type. From what I understand there is no way to force numbers in
Javascript into double or float, and I have to use native Java arrays. I
ended up adding a small conversion funtcion a2d ("array to double array")
that takes a Javascript array and returns a proper Java array of doubles,
and calling it right in the Plot declaration :
plot =  new Plot("title", "xlegend", "ylegend", a2d(Xarray), a2d(Yarray));

Cheers,

Christophe.




> Albert
> --
> http://albert.rierol.net
> http://www.ini.uzh.ch/~acardona/
>
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

Cammer, Michael
Would
Xcoor = [1.0, 2, 3, 4];
Ycoor = [10.0, 20, 30, 40];
solve the problem?
-Michael C.

-----Original Message-----

> >> Could someone tell me please why this Javascript code :
> >>
> >> Xcoor = [1, 2, 3, 4];
> >> Ycoor = [10, 20, 30, 40];
> >> Myplot = new Plot("Profile", "xcoor", "ycoor", Xcoor , Ycoor);
> >> plot.show();
> >>
plot =  new Plot("title", "xlegend", "ylegend", a2d(Xarray), a2d(Yarray));

Cheers,

Christophe.




> Albert
> --
> http://albert.rierol.net
> http://www.ini.uzh.ch/~acardona/
>

------------------------------------------------------------
This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain information that is proprietary, confidential, and exempt from disclosure under applicable law. Any unauthorized review, use, disclosure, or distribution is prohibited. If you have received this email in error please notify the sender by return email and delete the original message. Please note, the recipient should check this email and any attachments for the presence of viruses. The organization accepts no liability for any damage caused by any virus transmitted by this email.
=================================
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

dscho
Hi Michael,

On Wed, 2 Nov 2011, Cammer, Michael wrote:

> Would
> Xcoor = [1.0, 2, 3, 4];
> Ycoor = [10.0, 20, 30, 40];
> solve the problem?

Unfortunately not. Try this:

-- snip --
a = [1.0, 2, 3, 4];
importClass(Packages.java.util.Arrays);
print(Arrays.toString(a));
-- snap --

Indeed, a is not even an instance of a Java class. You can verify that by
replacing Arrays.toString(a) with a.getClass().getName().

So: I stand corrected, a cast does not help, since Javascript does not
have casts (as pointed out by Albert).

Ciao,
Johannes
Reply | Threaded
Open this post in threaded view
|

Re: How to draw a plot in Javascript

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Cammer, Michael
On Nov 2, 2011, at 9:11 AM, Cammer, Michael wrote:

> Would
> Xcoor = [1.0, 2, 3, 4];
> Ycoor = [10.0, 20, 30, 40];
> solve the problem?
> -Michael C.

Unfortunately, it doesn't. What does work is converting the JavaScript arrays to Java arrays, as in this example:

  x = [1, 2, 3, 4];
  y = [20, 10, 30, 20];
  plot = new Plot("Plot 1", "X", "Y", a2d(x), a2d(y));
  plot.show();

  function a2d(a) {
     d = new java.lang.reflect.Array.newInstance(java.lang.Double.TYPE,a.length);
     for (var i=0; i<a.length; i++) d[i] = a[i];
     return d;
  }

There are five more example plots in ExamplePlots.js at

   http://imagej.nih.gov/ij/macros/js/ExamplePlots.js

which is a JavaScript version of the ExamplePlots macro set at

   http://imagej.nih.gov/ij/macros/ExamplePlots.txt

ExamplePlots.js requires the ImageJ 1.46a daily build.

-wayne