Skip to content
Erich Seifert edited this page Sep 21, 2017 · 2 revisions

Plotting a 2D sine graph

This page will show a step by step example on the basic usage of GRAL. What we want to achieve is small line plot that looks like this:

image

Before we begin

Before we can plot something, we have to decide how we want to output it: on the screen or to a file. For this example we'll use a simple Swing frame to display our plot on the screen:

How do I feed GRAL with data?

First, we need data that our plot should display. All plots in GRAL need an instance of de.erichseifert.gral.data.DataSource as input. GRAL can import data from various sources like plain text files, or also images or audio files. However, for now we create the data by hand. A basic implementation of DataSource that allows to create data by hand is de.erichseifert.gral.data.DataTable:

Which plot do i have to create?

Next, we have to create a line plot. In GRAL a 2D line plot is created as a x-y plot with a special styling. So we have create a x-y plot first (de.erichseifert.gral.plots.XYPlot) and tell it which data it should display:

How do I display it?

The plot has to be shown in our frame. To achieve this, we simply add the plot to our frame by wrapping it in an de.erichseifert.gral.ui.InteractivePanel instance, a ready-to-use Swing component provided by GRAL:

This is the first time we can run our application. We should see something like this:

image

The InteractivePanel allows you to use the mouse wheel to zoom in and out and the left mouse button to move the plot. It also provides a context menu where you can reset the view or export the current plot area to a file.

How can i make it look better?

As mentioned at the beginning, we want a line plot, so we have to change the appearance of out plot. To show connecting lines on our data we have to use a line renderer (de.erichseifert.gral.plots.lines.LineRenderer) and we can use a very basic renderer (de.erichseifert.gral.plots.lines.DefaultLineRenderer2D):

Finally, to make the plot more beautiful we change the color of all points and lines:

So this is the final code of our small example:

How can I use this with my data?

To adjust this example to other data only the data source data has to be changed. You can populate it with your own values. You can do this manually by using the data.add() method of DataTable or you can use one of the built-in classes to import data from external sources. If you want to import data from a file with tab separated values you could use the following code:

After you have prepared your data and look at your plot you will see that GRAL automatically chose the number of tick marks along each axis. If it doesn't suit your need you can also set the spacing manually:

Be aware however that the spacing won't be adjusted anymore when you zoom in and out.

What more?

This is just a very basic example and there are many things you can change on your plot. This may include having different axis scalings—maybe even a logarithmic axis—a legend, or add color gradients, change line widths, change the shape and size of the points, and much more. If GRAL doesn't fully suit your needs you could also create your own renderers or plot types.

I hope you got a small impression how easy it is to create plots using GRAL. For more examples see the small applications that are delivered with the gral-examples package or read the documentation.