How to Plot a Graph With Nebula XYGraph
Check out this tutorial to learn how to plot a graph with the Eclipse project Nebula XYGraph. Click here for step-by-step instructions that can help you get started!
Join the DZone community and get the full member experience.
Join For Freenebula is an eclipse project that allows you to use hundreds of widgets to make your application. xygraph is one of these widgets, and you can use it to plot a graph with plenty of options. let’s take a look at how to do that!
in the eclipse menu, select help, then click install new software. next, you will need to put the following update site in the work with field: http://download.eclipse.org/nebula/snapshot .
then, you will need to expand the nebula release individual widgets node. you can do this by selecting the nebula visualization widgets, finishing the installation, and letting eclipse restart.
now, you can create a new application with file, new, project, plug-in development, plug-in project, next.
give the project a name, e.g. it.rcpvision.nebula.xygraph. next, select:
- generate an activator
- this plug-in will make contributions to the ui
- would you like to create a rich client application: yes
next, select the eclipse 4 rcp application template. next, select create sample content …, finish. to confirm opening a plug-in development perspective is more of a matter of taste. for this example, let's say yes.
now, navigate and open the following page: /it.rcpvision.nebula.xygraph/src/it/rcpvision/nebula/xygraph/parts/samplepart.java
you will need to remove the following parts:
private text txtinput;
private tableviewer tableviewer;
@inject
private mpart part;
txtinput = new text(parent, swt.border);
txtinput.setmessage("enter text to mark part as dirty");
txtinput.addmodifylistener(e -> part.setdirty(true));
txtinput.setlayoutdata(new griddata(griddata.fill_horizontal));
tableviewer = new tableviewer(parent);
tableviewer.setcontentprovider(arraycontentprovider.getinstance());
tableviewer.setinput(createinitialdatamodel());
tableviewer.gettable().setlayoutdata(new griddata(griddata.fill_both));
tableviewer.gettable().setfocus();
@persist
public void save() {
part.setdirty(false);
}
private list<string> createinitialdatamodel() {
return arrays.aslist("sample item 1", "sample item 2", "sample item 3", "sample item 4", "sample item 5");
}
press ctrl-shift-o to remove unused imports. once you click save, you’ll get an empty part.
you can test it by selecting it.rcpvision.nebula.xygraph.product, right click, run as, eclipse application: your empty application will pop-up. now, close it.
it is time to add the right dependencies. to do this, open (double-click) the manifest.mf, dependencies tab, then add:
- org.eclipse.draw2d
- org.eclipse.nebula.visualization.xygraph
in the required plug-ins, select save. then, open it.rcpvision.nebula.xygraph.product in the contents tab. next, add the same two plug-ins, and click save.
again, go into samplepart.java. after the following line:
parent.setlayout(new gridlayout(1, false));
in the
createcomposite()
method, add the following lines:
canvas canvas = new canvas(parent, swt.none);
canvas.setlayoutdata(new griddata(swt.fill, swt.fill, true, true, 1, 1));
lightweightsystem lws = new lightweightsystem(canvas);
xygraph xygraph = new xygraph();
toolbararmedxygraph toolbararmedxygraph = new toolbararmedxygraph(xygraph);
lws.setcontents(toolbararmedxygraph);
circularbufferdataprovider tracedataprovidertraining = new circularbufferdataprovider(false);
tracedataprovidertraining.setbuffersize(100);
trace tracetraining = new trace("trace legenda", xygraph.getprimaryxaxis(), xygraph.getprimaryyaxis(),tracedataprovidertraining);
xygraph.addtrace(tracetraining);
xygraph.settitle("sigmoid function");
xygraph.getprimaryyaxis().setscalelinevisible(true);
xygraph.getprimaryxaxis().setshowmajorgrid(true);
xygraph.getprimaryyaxis().setshowmajorgrid(true);
xygraph.getprimaryxaxis().setvisible(true);
tracetraining.setpointstyle(pointstyle.bar);
tracetraining.settracecolor(xygraphmediafactory.getinstance().getcolor(xygraphmediafactory.color_red));
xygraph.getprimaryxaxis().settitle("x axis");
xygraph.getprimaryyaxis().settitle("y axis");
xygraph.getprimaryyaxis().setdashgridline(true);
//plot our xy function
for (int x = -20; x < 20; x++) {
double y = 1.0 / (1.0 + math.exp(-x));
tracedataprovidertraining.addsample(new sample(x,y));
xygraph.performautoscale();
}
in the end, we are ready to relaunch our application (right-click it.rcpvision.nebula.xygraph.product, run as, eclipse application). now, you can view and play with our new graph!
Published at DZone with permission of Vincenzo Caselli, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments