| 1 |
Data administration |
| 2 |
=================== |
| 3 |
|
| 4 |
The first step, before we are able to plot anything, is to load or create data, |
| 5 |
a process which GRAL provides several ways for. The basic interface that will be |
| 6 |
used when you have to provide data is ``DataSource``. Think of it as a table |
| 7 |
with a (theoretically) arbitrary number of rows and columns. Every column has |
| 8 |
its own data type, but only ``Comparable`` values are allowed. The functions a |
| 9 |
``DataSource`` provides are solely for retrieving data rows, but it also |
| 10 |
supports statistics on the contained data, as well as the capability to listen |
| 11 |
for data changes. The following sections describe the different ways for |
| 12 |
providing and manipulating data. |
| 13 |
|
| 14 |
Creating data |
| 15 |
------------- |
| 16 |
|
| 17 |
Assuming you have computed some values you want to plot, how can you store your |
| 18 |
data in a ``DataSource``? |
| 19 |
|
| 20 |
You can do so through the class ``DataTable``. ``DataTable`` is an |
| 21 |
implementation of ``DataSource`` (or ``AbstractDataSource`` to be more precise) |
| 22 |
and supports operations like adding and removing rows. The values you must match |
| 23 |
the number of columns and their types, which both had already been specified in |
| 24 |
the constructor. |
| 25 |
|
| 26 |
.. code:: java |
| 27 |
|
| 28 |
// Create the table with the specified column types |
| 29 |
DataTable table = new DataTable(Double.class, Double.class); |
| 30 |
|
| 31 |
// Iterate your available data. In this case, an Iterable<Double[]> or Double[][] |
| 32 |
for (Double[] coords : data) { |
| 33 |
double x = coords[0]; |
| 34 |
double y = coords[1]; |
| 35 |
table.add(x, y); |
| 36 |
} |
| 37 |
|
| 38 |
You might want to have a ``DataTable`` for testing purposes only and do not want |
| 39 |
to create random values for it at each start. In this case, the class |
| 40 |
``DummyData`` is your remedy. ``DummyData`` is a ``DataSource`` filled with a |
| 41 |
single value. As I already mentioned, this is not used very often, but suited |
| 42 |
very well for testing due to its efficiency, especially when dealing with large |
| 43 |
tables. |
| 44 |
|
| 45 |
.. code:: java |
| 46 |
|
| 47 |
DummyData data = new DummyData(3, 100, 42.0); |
| 48 |
|
| 49 |
Sorting data |
| 50 |
------------ |
| 51 |
|
| 52 |
Often, data has to be reordered, e.g. for filtering. The sorting criteria in |
| 53 |
GRAL can be handled very flexible: for example the rows of a data source could |
| 54 |
be rearranged so that the first column is sorted ascending and as a second |
| 55 |
criterion the second column is sorted descending when values of the first column |
| 56 |
are equal. GRAL uses two classes to define the sorting options: ``Ascending`` |
| 57 |
and ``Descending``. In order to create one of them you must pass the index of |
| 58 |
the column that should be sorted to the constructor. |
| 59 |
|
| 60 |
.. code:: java |
| 61 |
|
| 62 |
// Sort the primary column (1) ascending, |
| 63 |
// the secondary column (0) descending, and |
| 64 |
// the ternary column (2) ascending |
| 65 |
table.sort(new Ascending(1), new Descending(0), new Ascending(2)); |
| 66 |
|
| 67 |
Filtering data |
| 68 |
-------------- |
| 69 |
|
| 70 |
``DataTables`` are not the only data source for plots. A plot also accepts |
| 71 |
filtered data. In GRAL data can be filtered either by columns or by rows. |
| 72 |
|
| 73 |
Filtering columns |
| 74 |
~~~~~~~~~~~~~~~~~ |
| 75 |
|
| 76 |
The main tasks for filtering by columns are to create subsets (*series*) of |
| 77 |
columns and to reorder columns for certain plot types. Using the class |
| 78 |
``DataSeries`` a data source with four columns *A, B, C, D* could be divided |
| 79 |
into two series: one containing columns *A, B* and one containing columns |
| 80 |
*C, D*. The series could also overlap, for example the first series could |
| 81 |
contain the columns *A, B* and the second the columns *B, C*. Furthermore, data |
| 82 |
series can be used to reorder columns. It is possible to map columns |
| 83 |
*A, B, C, D* to *D, B, C, A* or just *D, C*. |
| 84 |
|
| 85 |
A ``DataSeries`` is created using at least two parameters: the original data |
| 86 |
source and a number of columns, which will appear in the order they are passed. |
| 87 |
Optionally a name can be passed as first argument. In Plots this name is used |
| 88 |
for example to display captions in the plot legend. |
| 89 |
|
| 90 |
.. code:: java |
| 91 |
|
| 92 |
// Create a new series from columns 0 and 1 |
| 93 |
DataSeries series1 = new DataSeries("Series 1", table, 0, 1); |
| 94 |
// Create a new series from columns 2 and 0 |
| 95 |
DataSeries series2 = new DataSeries("Series 2", table, 2, 0); |
| 96 |
// Create a new series from column 1 |
| 97 |
DataSeries series3 = new DataSeries("Series 3", table, 1); |
| 98 |
|
| 99 |
Filtering rows |
| 100 |
~~~~~~~~~~~~~~ |
| 101 |
|
| 102 |
Another way of filtering is to filter data sources by rows. This way a data |
| 103 |
subset which matches certain criteria can be extracted. For example, this could |
| 104 |
be used to form clusters which could then be plotted or processed separately. |
| 105 |
|
| 106 |
In order to use the ``DataSubset`` a new class has to be created which |
| 107 |
implements the method ``accept(Row)``. This method is used to decide whether |
| 108 |
a certain row should be kept in the subset. |
| 109 |
|
| 110 |
.. code:: java |
| 111 |
|
| 112 |
// Keep only rows where the first column has an even value |
| 113 |
RowSubset data = new RowSubset(table) { |
| 114 |
@Override |
| 115 |
public boolean accept(Row row) { |
| 116 |
Number n = (Number) row.get(0); |
| 117 |
return (n.doubleValue() % 2.0) == 0.0; |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
Processing data |
| 122 |
--------------- |
| 123 |
|
| 124 |
An integral part of GRAL's pipeline is preprocessing of data. The simplest case |
| 125 |
would be to extract several statistics per column such as minimum, maximum, |
| 126 |
arithmetic mean, or median. But GRAL also covers more complex cases such as |
| 127 |
generating histograms or convolution filtering of data. The latter can be used |
| 128 |
to smooth or sharpen data in various ways. |
| 129 |
|
| 130 |
Statistics |
| 131 |
~~~~~~~~~~ |
| 132 |
|
| 133 |
The most basic statistical functionality of GRAL is to query various aggregated |
| 134 |
measures for columns using the class Statistics. It is part of every |
| 135 |
``DataSource`` instance and can be easily accessed with the method |
| 136 |
``getStatistics(int)``. |
| 137 |
|
| 138 |
.. code:: java |
| 139 |
|
| 140 |
// Get the maximum for the second column |
| 141 |
double max = table.getColumn(1).getStatistics(Statistics.MAX); |
| 142 |
|
| 143 |
``N`` |
| 144 |
The number of values in the column. |
| 145 |
|
| 146 |
``SUM`` |
| 147 |
The sum of all column values. |
| 148 |
|
| 149 |
``MIN`` |
| 150 |
The smallest value of the column. |
| 151 |
|
| 152 |
``MAX`` |
| 153 |
The largest value of the column. |
| 154 |
|
| 155 |
``MEAN`` |
| 156 |
The arithmetic mean describing the average value of the column. |
| 157 |
|
| 158 |
``MEAN_DEVIATION`` |
| 159 |
The mean deviation describing the dispersion of the column's values. |
| 160 |
|
| 161 |
``MEDIAN`` |
| 162 |
The median value which divides the column values in two equal |
| 163 |
halves. |
| 164 |
|
| 165 |
``VARIANCE`` |
| 166 |
The variance value describing the dispersion of the column's values. |
| 167 |
|
| 168 |
``SKEWNESS`` |
| 169 |
The skewness value describing the asymmetry of the probability |
| 170 |
distribution of the column's values. |
| 171 |
|
| 172 |
``KURTOSIS`` |
| 173 |
The kurtosis value describing the "peakedness" of the probability |
| 174 |
distribution of the column's values. |
| 175 |
|
| 176 |
``QUARTILE_1`` |
| 177 |
The value that delimits the lower 25% of all data values. |
| 178 |
|
| 179 |
``QUARTILE_2`` |
| 180 |
The value that delimits 50% of all data values. This is the same as |
| 181 |
the median value. |
| 182 |
|
| 183 |
``QUARTILE_3`` |
| 184 |
The value that delimits the upper 25% of all data values. |
| 185 |
|
| 186 |
Histograms are a more complex way for aggregating data. In a histogram all |
| 187 |
values are assigned to specified categories. In GRAL's ``Histogram`` class |
| 188 |
categories are defined as value ranges. For example all values from 0 to 5 would |
| 189 |
be in category A and all values from 5 to 10 in category B. Then, the histogram |
| 190 |
would generate two rows for each column that contain the number of values in |
| 191 |
category A and B, respectively. |
| 192 |
|
| 193 |
.. code:: java |
| 194 |
|
| 195 |
// Use 4 equally spaced breaks |
| 196 |
Histogram histogram = new Histogram1D(table, Orientation.VERTICAL, 4); |
| 197 |
|
| 198 |
.. code:: java |
| 199 |
|
| 200 |
// Use custom breaks for each column |
| 201 |
Number[] breaksCol1 = {1.0, 2.0, 3.0, 4.0, 5.0}; |
| 202 |
Number[] breaksCol2 = {1.0, 3.0, 5.0, 7.0, 9.0}; |
| 203 |
Histogram histogram = new Histogram1D(table, Orientation.VERTICAL, |
| 204 |
breaksCol1, breaksCol2); |
| 205 |
|
| 206 |
Convolution |
| 207 |
~~~~~~~~~~~ |
| 208 |
|
| 209 |
Often, it is necessary to change existing data by smoothing it, so that noise or |
| 210 |
fine-scale structures are reduced. Another frequent use case is to boost or |
| 211 |
extract exactly those fine-scale structures. Both cases can be handled in GRAL |
| 212 |
using the convolution operation. Mathematically, convolution is the combination |
| 213 |
of two functions: the data function and a kernel function. By varying th kernel |
| 214 |
function various operations can be achieved: smoothing (low-pass filter), |
| 215 |
deriving (high-pass filter), sharpening, moving average, and much more. |
| 216 |
|
| 217 |
Besides the class ``DataSource`` GRAL provides two additional classes for |
| 218 |
convolution: the class ``Kernel`` defines the kernel function and the class |
| 219 |
``Convolution`` is responsible for processing the data source. The |
| 220 |
``Convolution`` instance can finally be used as a data source for plots. |
| 221 |
|
| 222 |
.. code:: java |
| 223 |
|
| 224 |
// Create a moving average of width 3 |
| 225 |
Kernel kernel = new Kernel(1.0, 1.0, 1.0).normalize(); |
| 226 |
// Filter columns 0 and 1 and omit boundary values if necessary |
| 227 |
Convolution filter = new Convolution(table, kernel, Filter.Mode.OMIT, 0, 1); |
| 228 |
|
| 229 |
.. code:: java |
| 230 |
|
| 231 |
// Create a smoothing kernel with a variance of 2 |
| 232 |
Kernel kernel = KernelUtils.getBinomial(2.0).normalize(); |
| 233 |
// Filter column 1 and start over for boundary values if necessary |
| 234 |
Convolution filter = new Convolution(table, kernel, Filter.Mode.CIRCULAR, 1); |
| 235 |
|
| 236 |
.. code:: java |
| 237 |
|
| 238 |
// Create a smoothing kernel with a variance of 3 |
| 239 |
Kernel kernel = KernelUtils.getBinomial(3.0).normalize() |
| 240 |
// Subtract the original values |
| 241 |
kernel = kernel.negate().add(new Kernel(1.0)); |
| 242 |
// Filter column 1 and repeat boundary values if necessary |
| 243 |
Convolution filter = new Convolution(table, kernel, Filter.Mode.REPEAT, 1); |
| 244 |
|
| 245 |
Exchanging data |
| 246 |
--------------- |
| 247 |
|
| 248 |
GRAL allows you to interchange data values through its extensible plug-in |
| 249 |
system. Arbitrary sources and sinks can be accessed, like plain files, |
| 250 |
databases, or even web services. The current version of GRAL already supports |
| 251 |
file formats like simple CSV files (*Comma-Separated Values*). |
| 252 |
|
| 253 |
Importing |
| 254 |
~~~~~~~~~ |
| 255 |
|
| 256 |
Loading data from a source in GRAL needs two steps: First, you have to get a |
| 257 |
``DataReader`` the desired file format via its MIME type from an instance of |
| 258 |
``DataReaderFactory``. Then, you have to call the method read of the reader with |
| 259 |
two (or more) parameters: an ``InputStream`` instance to read from and the |
| 260 |
column data types. The method finally reads the data and returns a new |
| 261 |
``DataSource`` containing all values that have been extracted from the source. |
| 262 |
|
| 263 |
.. code:: java |
| 264 |
|
| 265 |
DataReader reader = DataReaderFactory.getInstance().get("text/csv"); |
| 266 |
InputStream file = new FileInputStream("foobar.csv"); |
| 267 |
DataSource data = reader.read(file, Integer.class, Double.class, Double.class); |
| 268 |
|
| 269 |
.. code:: java |
| 270 |
|
| 271 |
DataReader reader = DataReaderFactory.getInstance().get("image/png"); |
| 272 |
reader.setSetting("factor", 1.0/255.0); |
| 273 |
reader.setSetting("offset", 1); |
| 274 |
InputStream file = new FileInputStream("foobar.png"); |
| 275 |
DataSource data = reader.read(file); |
| 276 |
|
| 277 |
Exporting |
| 278 |
~~~~~~~~~ |
| 279 |
|
| 280 |
Saving data in GRAL is even easier than loading. First, you need to get an |
| 281 |
instance of ``DataWriterFactory`` and then you fetch a ``DataWriter`` for the |
| 282 |
desired file format via its MIME type. Then, you have to call the method |
| 283 |
``write`` with two parameters: a ``DataSource`` and an ``OutputStream`` instance |
| 284 |
to write to. |
| 285 |
|
| 286 |
.. code:: java |
| 287 |
|
| 288 |
DataWriter writer = DataWriterFactory.getInstance().get("text/csv"); |
| 289 |
FileOutputStream file = new FileOutputStream("foobar.csv"); |
| 290 |
writer.write(table, file); |
| 291 |
|
| 292 |
.. code:: java |
| 293 |
|
| 294 |
DataWriter writer = DataWriterFactory.getInstance().get("image/png"); |
| 295 |
writer.setSetting("factor", 255); |
| 296 |
writer.setSetting("offset", -255); |
| 297 |
FileOutputStream file = new FileOutputStream("foobar.png"); |
| 298 |
writer.write(table, file); |
| 299 |
|
| 300 |
Displaying data |
| 301 |
=============== |
| 302 |
|
| 303 |
The main purpose of GRAL is to plot diagrams. It offers several types of plots |
| 304 |
which can be customized and exported for publishing. In this chapter you will |
| 305 |
find an overview of plot types and their options for customization as well as |
| 306 |
examples how to export the plotted graphics in various formats. |
| 307 |
|
| 308 |
The components of a plot in GRAL are: |
| 309 |
|
| 310 |
- Each plot has one or more instances of ``DataSource`` |
| 311 |
|
| 312 |
- The area where the actual data is plotted is called plot area and the class |
| 313 |
used to display the area is ``PlotArea2D``, correspondingly. |
| 314 |
|
| 315 |
- Depending on its type a plot can have an arbitrary number of axes which are |
| 316 |
created with the class Axis. Each Axis is displayed by an instance of |
| 317 |
``AxisRenderer``. |
| 318 |
|
| 319 |
- Data points on the plot area are rendered by an instance of ``PointRenderer``. |
| 320 |
|
| 321 |
- Connections between data points are rendered by an instance of |
| 322 |
``LineRenderer`` |
| 323 |
|
| 324 |
- In order to fill the area below data points an instance of ``AreaRenderer`` is |
| 325 |
used. |
| 326 |
|
| 327 |
Plot types |
| 328 |
---------- |
| 329 |
|
| 330 |
Currently, GRAL has three main plot types: xy-plot, bar plot, and pie plot. |
| 331 |
This section presents those plot types and show how to adjust their visual |
| 332 |
settings and to derive many more types. For example, xy-plots can be turned into |
| 333 |
line plots, or area plots and a pie chart can be made into a doughnut plot with |
| 334 |
just one command. |
| 335 |
|
| 336 |
XY-Plot |
| 337 |
~~~~~~~ |
| 338 |
|
| 339 |
``XYPlot``: XY-Plots are usually the most common plot type. Used it whenever you |
| 340 |
want to create a line plot, a scatter plot, a bubble plot, or an area plot. |
| 341 |
|
| 342 |
.. code:: java |
| 343 |
|
| 344 |
Plot plot = new XYPlot(series1, series2); |
| 345 |
|
| 346 |
Legends |
| 347 |
^^^^^^^ |
| 348 |
|
| 349 |
Legends explain the symbols used in plot by the symbols and a description in a |
| 350 |
table-like representation. GRAL's ``Legend`` class can be either vertically |
| 351 |
(default) or horizontally oriented. Its options determine the positioning inside |
| 352 |
the plot as well as its background color, its border, or its spacings. |
| 353 |
|
| 354 |
.. code:: java |
| 355 |
|
| 356 |
plot.setLegendVisible(true); |
| 357 |
|
| 358 |
Bar plot |
| 359 |
~~~~~~~~ |
| 360 |
|
| 361 |
Usually, bar plots are used to show rectangular bars with lengths proportional |
| 362 |
to their corresponding values. GRAL provides a ``BarPlot`` class which is in |
| 363 |
fact a special case of an xy-plot. |
| 364 |
|
| 365 |
.. code:: java |
| 366 |
|
| 367 |
Plot plot = new BarPlot(series); |
| 368 |
|
| 369 |
Box-and-whisker plot |
| 370 |
~~~~~~~~~~~~~~~~~~~~ |
| 371 |
|
| 372 |
Box-and-whisker, or short box plots, are used to display the statistics like |
| 373 |
minimum, maximum, median, or quantiles in a concise plot. GRAL's class |
| 374 |
``BoxPlot`` is used to create this type of plot. |
| 375 |
|
| 376 |
.. code:: java |
| 377 |
|
| 378 |
Plot plot = new BoxPlot(series); |
| 379 |
|
| 380 |
The data series must provide six columns for each plot element: |
| 381 |
|
| 382 |
- x position of the box-whisker-plot |
| 383 |
- y position of the center bar (e.g. median) |
| 384 |
- y position of the lower whisker (e.g. minimum) |
| 385 |
- upper edge of the box (e.g. first quartile) |
| 386 |
- lower edge of the box (e.g. third quartile) |
| 387 |
- y position of the upper whisker (e.g. maximum) |
| 388 |
|
| 389 |
A utility method of ``BoxPlot`` can be used to generate a suitable data source |
| 390 |
from an existing data source: |
| 391 |
|
| 392 |
.. code:: java |
| 393 |
|
| 394 |
DataSource series = BoxPlot.createBoxData(data); |
| 395 |
Plot plot = new BoxPlot(series); |
| 396 |
|
| 397 |
Pie plot |
| 398 |
~~~~~~~~ |
| 399 |
|
| 400 |
Pie plots are circles divided into sectors to illustrate the proportions of the |
| 401 |
corresponding data values. GRAL's class ``PiePlot`` is used to create this type |
| 402 |
of plot. |
| 403 |
|
| 404 |
.. code:: java |
| 405 |
|
| 406 |
Plot plot = new PiePlot(series); |
| 407 |
|
| 408 |
Raster plot |
| 409 |
~~~~~~~~~~~ |
| 410 |
|
| 411 |
Raster plots are used to display a two-dimensional grid with filled grid tiles. |
| 412 |
GRAL's class ``RasterPlot`` is used to create this type of plot. |
| 413 |
|
| 414 |
.. code:: java |
| 415 |
|
| 416 |
Plot plot = new RasterPlot(series); |
| 417 |
|
| 418 |
The data series must provide three columns for each grid tile: |
| 419 |
|
| 420 |
- x position of the grid tile |
| 421 |
- y position of the grid tile |
| 422 |
- value of the grid tile |
| 423 |
|
| 424 |
A utility method of ``RasterPlot`` can be used to generate a suitable data |
| 425 |
source from an existing data source: |
| 426 |
|
| 427 |
.. code:: java |
| 428 |
|
| 429 |
DataSource series = RasterPlot.createBoxData(data); |
| 430 |
Plot plot = new RasterPlot(series); |
| 431 |
|
| 432 |
Customization |
| 433 |
------------- |
| 434 |
|
| 435 |
The visual appearance of most classes in GRAL can be queried and changed using |
| 436 |
the ``get`` and ``set`` methods for each property. This way, properties like |
| 437 |
colors, borders, margins, or positions can be easily customized. |
| 438 |
|
| 439 |
Customizing the plot |
| 440 |
~~~~~~~~~~~~~~~~~~~~ |
| 441 |
|
| 442 |
Plots provide the canvas for painting all plot components (see section |
| 443 |
"Plotting"). It controls how the background will be drawn the way the components |
| 444 |
are positioned. Another important setting is the plot title. The following |
| 445 |
example shows how to set the title of an xy-plot. |
| 446 |
|
| 447 |
.. code:: java |
| 448 |
|
| 449 |
Plot plot = new XYPlot(data); |
| 450 |
plot.getTitle().setText("My First XY Plot"); |
| 451 |
|
| 452 |
In the next example you can see how a background gradient can be assigned to the |
| 453 |
whole plot. |
| 454 |
|
| 455 |
.. code:: java |
| 456 |
|
| 457 |
Plot plot = new PiePlot(data); |
| 458 |
Paint gradient = new LinearGradientPaint( |
| 459 |
0f,0f, // Coordinates of gradient start point |
| 460 |
1f,0f, // Coordinates of gradient end point |
| 461 |
new float[] {0f, 1f}, // Relative fractions |
| 462 |
new Color[] {Color.GRAY, Color.WHITE} // Gradient colors |
| 463 |
); |
| 464 |
plot.setBackground(gradient); |
| 465 |
|
| 466 |
The ``PlotArea2D`` is the container for plotting the data. It must be fetched |
| 467 |
from a plot with the method, ``getPlotArea`` as each ``Plot`` type can also have |
| 468 |
its own plot area type. In the following example you can see how to hide the |
| 469 |
plot area itself completely. |
| 470 |
|
| 471 |
.. code:: java |
| 472 |
|
| 473 |
PlotArea2D plotArea = plot.getPlotArea(); |
| 474 |
plotArea.setBackground(null); |
| 475 |
plotArea.setBorder(null); |
| 476 |
|
| 477 |
Often, a legend has to added to a plot. Every plot already has a ``Legend`` |
| 478 |
which just has to be turned on explicitly. Then, the positioning, orientation, |
| 479 |
spacing, as well as the legend background can be changed. The following example |
| 480 |
shows how to add a horizontal legend to the the bottom left corner of a plot. |
| 481 |
|
| 482 |
.. code:: java |
| 483 |
|
| 484 |
plot.setLegendVisible(true); |
| 485 |
plot.setLegendLocation(Location.SOUTH_WEST); |
| 486 |
Legend legend = plot.getLegend(); |
| 487 |
legend.setOrientation(Orientation.HORIZONTAL); |
| 488 |
|
| 489 |
Customizing axes |
| 490 |
~~~~~~~~~~~~~~~~ |
| 491 |
|
| 492 |
Axes of a plot have reasonable defaults for displaying. Sometimes however, it's |
| 493 |
necessary to add an axis title, adjust the spacing of the tick marks, or change |
| 494 |
the formatting of the data values along an axis. All those properties are |
| 495 |
controlled by the interface ``AxisRenderer``. Each axis in a plot has its own |
| 496 |
instance and can have different settings. |
| 497 |
|
| 498 |
.. code:: java |
| 499 |
|
| 500 |
AxisRenderer axisRendererX = plot.getAxisRenderer(XYPlot.AXIS_X); |
| 501 |
axisRendererX.setTickSpacing(5.0); |
| 502 |
|
| 503 |
There are to implementations of ``AxisRenderer``: for axes with a linear scale |
| 504 |
(the default case) ``LinearRenderer2D`` is used; for axes with a logarithmic |
| 505 |
scale the class ``LogarithmicRenderer2D`` is used: |
| 506 |
|
| 507 |
.. code:: java |
| 508 |
|
| 509 |
XYPlot plot = new XYPlot(seriesLog, seriesLin); |
| 510 |
AxisRenderer2D axisRendererX = new LogarithmicRenderer2D(); |
| 511 |
axisRendererX.setLabel("Logarithmic data"); |
| 512 |
plot.setAxisRenderer(XYPlot.AXIS_X, axisRendererX); |
| 513 |
|
| 514 |
.. code:: java |
| 515 |
|
| 516 |
AxisRenderer2D axisRendererX = new LogarithmicRenderer2D(); |
| 517 |
Format dateFormat = DateFormat.getTimeInstance(); |
| 518 |
axisRendererX.setTickLabelFormat(dateFormat); |
| 519 |
|
| 520 |
.. code:: java |
| 521 |
|
| 522 |
Map<Double, String> labels = new HashMap<Double, String>(); |
| 523 |
labels.put(2.0, "Doubled"); |
| 524 |
labels.put(1.5, "One and a half times"); |
| 525 |
axisRendererX.setCustomTicks(labels); |
| 526 |
|
| 527 |
Customizing points |
| 528 |
~~~~~~~~~~~~~~~~~~ |
| 529 |
|
| 530 |
The display of data points in a plot is done by instances of ``PointRenderer``. |
| 531 |
A point renderer defines the shape, the color, the size, and even the position |
| 532 |
of each point. A custom renderer can be implemented using either the interface |
| 533 |
``PointRenderer`` itself or using the abstract class ``AbstractPointRenderer`` |
| 534 |
which is the preferred way. |
| 535 |
|
| 536 |
Every point renderer has to implement two methods: |
| 537 |
``Shape getPointShape(PointData)`` returns the vector shape of a specified data |
| 538 |
point, and ``Drawable getPoint(PointData, Shape)`` returns a drawable component |
| 539 |
which then renders the points. |
| 540 |
|
| 541 |
The class ``AbstractPointRenderer`` implements the interface ``PointRenderer`` |
| 542 |
and additionally it provides everything that's necessary to manage settings and |
| 543 |
draw basic elements. |
| 544 |
|
| 545 |
In the following example you can see how to implement a simple renderer. |
| 546 |
|
| 547 |
.. code:: java |
| 548 |
|
| 549 |
public class SimplePointRenderer extends DefaultPointRenderer2D { |
| 550 |
@Override |
| 551 |
public Drawable getPoint(final PointData data, final Shape shape) { |
| 552 |
Drawable drawable = new AbstractDrawable() { |
| 553 |
@Override |
| 554 |
public void draw(DrawingContext context) { |
| 555 |
Paint paint = SimplePointRenderer.this.getColor(); |
| 556 |
Shape point = getPointShape(data); |
| 557 |
|
| 558 |
// Put your custom code here ... |
| 559 |
|
| 560 |
GraphicsUtils.fillPaintedShape(context.getGraphics(), point, paint, null); |
| 561 |
} |
| 562 |
}; |
| 563 |
return drawable; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
Customizing lines |
| 568 |
~~~~~~~~~~~~~~~~~ |
| 569 |
|
| 570 |
Data points can be connected using lines. To draw these lines instances of |
| 571 |
``LineRenderer`` are used. A line renderer has full control over the line's |
| 572 |
shape, the stroke patterns, and the colors which will be used when drawing. |
| 573 |
Custom renderers can be easily implemented using either the interface |
| 574 |
``LineRenderer`` itself or using the abstract class ``AbstractLineRenderer2D`` |
| 575 |
which is the preferred way for two-dimensional applications. |
| 576 |
|
| 577 |
Every line renderer has to implement two methods: |
| 578 |
``Shape getLineShape(List<DataPoint>)`` returns a vector shape for the line, and |
| 579 |
``Drawable getLine(List<DataPoint>, Shape)`` returns a drawable component to |
| 580 |
display the line. |
| 581 |
|
| 582 |
The class ``AbstractLineRenderer2D`` implements the interface ``LineRenderer`` |
| 583 |
for two-dimensional data and additionally provides everything that's necessary |
| 584 |
to manage settings and draw basic elements. |
| 585 |
|
| 586 |
In the following example you can see how to implement a simple renderer. |
| 587 |
|
| 588 |
.. code:: java |
| 589 |
|
| 590 |
public class SimpleLineRenderer2D extends AbstractLineRenderer2D { |
| 591 |
@Override |
| 592 |
public Shape getLineShape(final List<DataPoint> points) { |
| 593 |
Path2D line = new Path2D.Double(); |
| 594 |
for (DataPoint point : points) { |
| 595 |
Point2D pos = point.position.getPoint2D(); |
| 596 |
if (line.getCurrentPoint() == null) { |
| 597 |
line.moveTo(pos.getX(), pos.getY()); |
| 598 |
} else { |
| 599 |
line.lineTo(pos.getX(), pos.getY()); |
| 600 |
} |
| 601 |
} |
| 602 |
Shape lineShape = punch(line, points); |
| 603 |
return lineShape; |
| 604 |
} |
| 605 |
|
| 606 |
@Override |
| 607 |
public Drawable getLine(final List<DataPoint> points, final Shape shape) { |
| 608 |
Drawable d = new AbstractDrawable() { |
| 609 |
@Override |
| 610 |
public void draw(DrawingContext context) { |
| 611 |
Paint paint = SimpleLineRenderer2D.this.getColor(); |
| 612 |
GraphicsUtils.fillPaintedShape(context.getGraphics(), shape, paint, null); |
| 613 |
} |
| 614 |
}; |
| 615 |
return d; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
Customizing areas |
| 620 |
~~~~~~~~~~~~~~~~~ |
| 621 |
|
| 622 |
In order to display filled or hatched areas in plots so called area renderers |
| 623 |
are used. They all derive from the interface ``AreaRenderer`` and they control |
| 624 |
the colors, the fillings, and also the shape of the rendered area. Custom |
| 625 |
renderers can be easily implemented using either the interface ``AreaRenderer`` |
| 626 |
itself or using the abstract class ``AbstractAreaRenderer`` which is the |
| 627 |
preferred way. |
| 628 |
|
| 629 |
Every area renderer has to implement one method: |
| 630 |
``Shape getAreaShape(List<DataPoint>)`` returns a vector shape for the area, and |
| 631 |
``Drawable getArea(List<DataPoint>, Shape)`` returns a drawable component to |
| 632 |
display the area. |
| 633 |
|
| 634 |
The class ``AbstractAreaRenderer`` implements the interface ``AreaRenderer`` and |
| 635 |
additionally it provides everything that's necessary to manage settings and draw |
| 636 |
basic elements. |
| 637 |
|
| 638 |
In the following example you can see how to implement a simple renderer. |
| 639 |
|
| 640 |
.. code:: java |
| 641 |
|
| 642 |
public class SimpleAreaRenderer extends AbstractAreaRenderer { |
| 643 |
@Override |
| 644 |
public Drawable getArea(final List<DataPoint> points, final Shape shape) { |
| 645 |
Shape path = getAreaShape(points); |
| 646 |
final Shape area = punch(path, points); |
| 647 |
|
| 648 |
return new AbstractDrawable() { |
| 649 |
@Override |
| 650 |
public void draw(DrawingContext context) { |
| 651 |
Paint paint = SimpleAreaRenderer.this.getColor(); |
| 652 |
GraphicsUtils.fillPaintedShape(context.getGraphics(), |
| 653 |
area, paint, area.getBounds2D()); |
| 654 |
} |
| 655 |
}; |
| 656 |
} |
| 657 |
|
| 658 |
public Shape getAreaShape(final List<DataPoint> points) { |
| 659 |
Shape shape = null; |
| 660 |
// Code to construct the shape |
| 661 |
return shape; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
Exporting plot images |
| 666 |
~~~~~~~~~~~~~~~~~~~~~ |
| 667 |
|
| 668 |
Usage similar to data import/export. Bitmap formats like PNG, JPEG, BMP, or GIF |
| 669 |
and vector formats like SVG, PDF, or EPS. |
| 670 |
|
| 671 |
.. code:: java |
| 672 |
|
| 673 |
XYPlot plot = new XYPlot(data); |
| 674 |
DrawableWriter writer = DrawableWriterFactory.getInstance().get("image/svg+xml"); |
| 675 |
FileOutputStream file = new FileOutputStream("xyplot.svg"); |
| 676 |
double width = 320.0, height = 240.0; |
| 677 |
writer.write(plot, file, width, height); |
| 678 |
|
| 679 |
Extending GRAL |
| 680 |
============== |
| 681 |
|
| 682 |
GRAL can be extended in numerous ways to better suit your needs. For example, |
| 683 |
you can write your own plot types, line types, axes, or data exchange plug-ins. |
| 684 |
The following chapter shows you how to use GRAL's application programming |
| 685 |
interface to tailor it for your requirements. |
| 686 |
|
| 687 |
Writing a new plot type |
| 688 |
----------------------- |
| 689 |
|
| 690 |
``Plot`` class, ``DataListener`` interface. |
| 691 |
|
| 692 |
.. code:: java |
| 693 |
|
| 694 |
public class MyPlot extends Plot implements DataListener { |
| 695 |
private double mySetting; |
| 696 |
|
| 697 |
public MyPlot(DataSource data) { |
| 698 |
super(data); |
| 699 |
mySetting = 1.0; |
| 700 |
... |
| 701 |
dataChanged(data); |
| 702 |
data.addDataListener(this); |
| 703 |
} |
| 704 |
|
| 705 |
@Override |
| 706 |
public void dataChanged(DataSource data) { |
| 707 |
... |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
Writing a data importer |
| 712 |
----------------------- |
| 713 |
|
| 714 |
``DataReaderFactory`` and ``DataReader``. |
| 715 |
|
| 716 |
.. code:: java |
| 717 |
|
| 718 |
public class MyReader extends IOCapabilitiesStorage implements DataReader { |
| 719 |
static { |
| 720 |
addCapabilities(new IOCapabilities( |
| 721 |
"My Format", |
| 722 |
"My custom file format", |
| 723 |
"application/x-myformat", |
| 724 |
"myf" |
| 725 |
)); |
| 726 |
} |
| 727 |
|
| 728 |
private final Map<String, Object> settings; |
| 729 |
private final Map<String, Object> defaults; |
| 730 |
private final String mimeType; |
| 731 |
|
| 732 |
public MyReader(String mimeType) { |
| 733 |
this.mimeType = mimeType; |
| 734 |
settings = new HashMap<String, Object>(); |
| 735 |
defaults = new HashMap<String, Object>(); |
| 736 |
defaults.put("my setting", "foobar"); |
| 737 |
} |
| 738 |
|
| 739 |
@Override |
| 740 |
public DataSource read(InputStream input, Class<? extends Number>... types) |
| 741 |
throws IOException, ParseException; |
| 742 |
... |
| 743 |
} |
| 744 |
|
| 745 |
/** |
| 746 |
* Returns the MIME type. |
| 747 |
* @return MIME type string. |
| 748 |
*/ |
| 749 |
public String getMimeType() { |
| 750 |
return mimeType; |
| 751 |
} |
| 752 |
|
| 753 |
@Override |
| 754 |
public <T> T getSetting(String key) { |
| 755 |
if (!settings.containsKey(key)) { |
| 756 |
return (T) defaults.get(key); |
| 757 |
} |
| 758 |
return (T) settings.get(key); |
| 759 |
} |
| 760 |
|
| 761 |
@Override |
| 762 |
public <T> void setSetting(String key, T value) { |
| 763 |
settings.put(key, value); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
Writing a data exporter |
| 768 |
----------------------- |
| 769 |
|
| 770 |
``DataWriterFactory`` and ``DataWriter``. |
| 771 |
|
| 772 |
.. code:: java |
| 773 |
|
| 774 |
public class MyWriter extends IOCapabilitiesStorage implements DataWriter { |
| 775 |
static { |
| 776 |
addCapabilities(new IOCapabilities( |
| 777 |
"My Format", |
| 778 |
"My custom file format", |
| 779 |
"application/x-myformat", |
| 780 |
"myf" |
| 781 |
)); |
| 782 |
} |
| 783 |
|
| 784 |
private final Map<String, Object> settings; |
| 785 |
private final Map<String, Object> defaults; |
| 786 |
private final String mimeType; |
| 787 |
|
| 788 |
public MyWriter(String mimeType) { |
| 789 |
this.mimeType = mimeType; |
| 790 |
settings = new HashMap<String, Object>(); |
| 791 |
defaults = new HashMap<String, Object>(); |
| 792 |
defaults.put("my setting", "foobar"); |
| 793 |
} |
| 794 |
|
| 795 |
@Override |
| 796 |
public void write(DataSource data, OutputStream output) throws IOException { |
| 797 |
... |
| 798 |
} |
| 799 |
|
| 800 |
/** |
| 801 |
* Returns the MIME type. |
| 802 |
* @return MIME type string. |
| 803 |
*/ |
| 804 |
public String getMimeType() { |
| 805 |
return mimeType; |
| 806 |
} |
| 807 |
|
| 808 |
@Override |
| 809 |
public <T> T getSetting(String key) { |
| 810 |
if (!settings.containsKey(key)) { |
| 811 |
return (T) defaults.get(key); |
| 812 |
} |
| 813 |
return (T) settings.get(key); |
| 814 |
} |
| 815 |
|
| 816 |
@Override |
| 817 |
public <T> void setSetting(String key, T value) { |
| 818 |
settings.put(key, value); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
Writing a plot exporter |
| 823 |
----------------------- |
| 824 |
|
| 825 |
``DrawableWriterFactory`` and ``DrawableWriter``. Either ``BitmapWriter`` or |
| 826 |
``VectorWriter``. |
| 827 |
|
| 828 |
Limitations |
| 829 |
=========== |
| 830 |
|
| 831 |
Due to its early stage of development, GRAL still has several limitations: |
| 832 |
|
| 833 |
- At the moment it's not very fast for large data sets |
| 834 |
|
| 835 |
- The API isn't stable yet, and major changes can happen before version 1.0 |
| 836 |
|
| 837 |
- Despite we try our best to ensure code quality there can always be bugs |