DMelt:General/8 Using Other Languages

From HandWiki
Member


Using BeanShell

Instead Jython, BeanShell. Create a file "histo1.bsh" with the content and load to the DataMelt IDE. Use the run button to execute it. One can also use the BeanShell prompt to type the program line by line.

import java.awt.Color;
import java.util.Random;
import jhplot.HPlot;
import jhplot.H1D;

c1 = new HPlot("Canvas");
c1.setGTitle("Global title for F_{2} and x_{γ} ");
c1.visible(true);
c1.setAutoRange();

h1 = new H1D("Simple1",20, -2.0, 2.0);
rand = new Random();
for (i=0; i<100; i++)  h1.fill(rand.nextGaussian());

c1.draw(h1);
h1.setPenWidthErr(2);
c1.setNameX("Xaxis");
c1.setNameY("Yaxis");
c1.setName("Canvas title");
c1.drawStatBox(h1);
c1.update();

// export to some image (png,eps,pdf,jpeg...)
c1.export(Editor.DocMasterName()+".png");


Using Jython

This is the most common way to run DataMelt - most DataMelt examples are written in Jython. The above example looks as:


from java.awt import Color
from java.util import Random
from jhplot  import *

c1 = HPlot("Canvas")
c1.setGTitle("Global labels: F_{2},  x_{&gamma;}")
c1.visible(1)
c1.setAutoRange()

h1 = H1D("Simple1",100, -2, 2.0)
rand = Random()
# fill histogram
for i in range(100):
      h1.fill(rand.nextGaussian())

c1.draw(h1)

c1.setAutoRange()
h1.setPenWidthErr(2)
c1.setNameX("Xaxis")
c1.setNameY("Yaxis")
c1.setName("Canvas title")
c1.drawStatBox(h1)
c1.update()

Using Java

Same code using Java looks as:

import java.awt.Color;
import java.util.Random;
import jhplot.*;

class histo1 {
public static void main(String[] args) {
HPlot c1 = new HPlot("Canvas",600,400,1,1);
c1.setGTitle("Global title for F_{2} and x_{&gamma;} ");
c1.visible(true);
c1.setAutoRange();

H1D h1 = new H1D("Simple1",20, -2.0, 2.0);
Random rand = new Random();
for (int i=0; i<100; i++)  h1.fill(rand.nextGaussian());

c1.draw(h1);
h1.setColor(Color.blue);
h1.setPenWidthErr(2);
c1.setNameX("Xaxis");
c1.setNameY("Yaxis");
c1.setName("Canvas title");
c1.drawStatBox(h1);
c1.update();

// make png figure
// c1.export("test.png");
   }
}