0
# Plots and Customization
1
2
Plot management and customization for controlling visual appearance, axes, renderers, and plot-specific features. Plots are the core components that manage how data is displayed, providing extensive customization options for professional chart appearance.
3
4
## Capabilities
5
6
### Plot Base Class
7
8
Common functionality available to all plot types for managing appearance, backgrounds, and event handling.
9
10
```java { .api }
11
/**
12
* Abstract base class for all plot types
13
*/
14
public abstract class Plot implements Drawable, Cloneable, PublicCloneable, Serializable {
15
16
// Background and appearance
17
public Paint getBackgroundPaint();
18
public void setBackgroundPaint(Paint paint);
19
public Image getBackgroundImage();
20
public void setBackgroundImage(Image image);
21
public RectangleInsets getInsets();
22
public void setInsets(RectangleInsets insets);
23
24
// Outline
25
public boolean isOutlineVisible();
26
public void setOutlineVisible(boolean visible);
27
public Stroke getOutlineStroke();
28
public void setOutlineStroke(Stroke stroke);
29
public Paint getOutlinePaint();
30
public void setOutlinePaint(Paint paint);
31
32
// Transparency
33
public float getForegroundAlpha();
34
public void setForegroundAlpha(float alpha);
35
public float getBackgroundAlpha();
36
public void setBackgroundAlpha(float alpha);
37
38
// Event handling
39
public void addChangeListener(PlotChangeListener listener);
40
public void removeChangeListener(PlotChangeListener listener);
41
42
// Abstract methods implemented by subclasses
43
public abstract void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState, PlotRenderingInfo info);
44
public abstract String getPlotType();
45
public abstract LegendItemCollection getLegendItems();
46
}
47
```
48
49
### CategoryPlot
50
51
Plot for displaying category-based data such as bar charts, line charts, and area charts. Manages datasets, axes, renderers, and category-specific features.
52
53
```java { .api }
54
/**
55
* A plot for displaying data in categories (bar charts, line charts, etc.)
56
*/
57
public class CategoryPlot extends Plot implements ValueAxisPlot, Pannable, Zoomable, Cloneable {
58
59
// Dataset management
60
public CategoryDataset getDataset();
61
public CategoryDataset getDataset(int index);
62
public void setDataset(CategoryDataset dataset);
63
public void setDataset(int index, CategoryDataset dataset);
64
public int getDatasetCount();
65
public List getDatasets();
66
67
// Domain axis (categories)
68
public CategoryAxis getDomainAxis();
69
public CategoryAxis getDomainAxis(int index);
70
public void setDomainAxis(CategoryAxis axis);
71
public void setDomainAxis(int index, CategoryAxis axis);
72
public int getDomainAxisCount();
73
public List getDomainAxes();
74
75
// Range axis (values)
76
public ValueAxis getRangeAxis();
77
public ValueAxis getRangeAxis(int index);
78
public void setRangeAxis(ValueAxis axis);
79
public void setRangeAxis(int index, ValueAxis axis);
80
public int getRangeAxisCount();
81
public List getRangeAxes();
82
83
// Renderer management
84
public CategoryItemRenderer getRenderer();
85
public CategoryItemRenderer getRenderer(int index);
86
public void setRenderer(CategoryItemRenderer renderer);
87
public void setRenderer(int index, CategoryItemRenderer renderer);
88
public int getRendererCount();
89
public List getRenderers();
90
91
// Plot orientation
92
public PlotOrientation getOrientation();
93
public void setOrientation(PlotOrientation orientation);
94
95
// Axis positioning
96
public AxisLocation getDomainAxisLocation();
97
public AxisLocation getDomainAxisLocation(int index);
98
public void setDomainAxisLocation(AxisLocation location);
99
public void setDomainAxisLocation(int index, AxisLocation location, boolean notify);
100
public AxisLocation getRangeAxisLocation();
101
public AxisLocation getRangeAxisLocation(int index);
102
public void setRangeAxisLocation(AxisLocation location);
103
public void setRangeAxisLocation(int index, AxisLocation location, boolean notify);
104
105
// Gridlines
106
public boolean isDomainGridlinesVisible();
107
public void setDomainGridlinesVisible(boolean visible);
108
public Paint getDomainGridlinePaint();
109
public void setDomainGridlinePaint(Paint paint);
110
public Stroke getDomainGridlineStroke();
111
public void setDomainGridlineStroke(Stroke stroke);
112
public boolean isRangeGridlinesVisible();
113
public void setRangeGridlinesVisible(boolean visible);
114
public Paint getRangeGridlinePaint();
115
public void setRangeGridlinePaint(Paint paint);
116
public Stroke getRangeGridlineStroke();
117
public void setRangeGridlineStroke(Stroke stroke);
118
119
// Markers and annotations
120
public void addRangeMarker(Marker marker);
121
public void addRangeMarker(int index, Marker marker, Layer layer);
122
public void clearRangeMarkers();
123
public void addDomainMarker(CategoryMarker marker);
124
public void addDomainMarker(int index, CategoryMarker marker, Layer layer);
125
public void clearDomainMarkers();
126
public void addAnnotation(CategoryAnnotation annotation);
127
public void removeAnnotation(CategoryAnnotation annotation);
128
public List getAnnotations();
129
}
130
```
131
132
**Usage Example:**
133
134
```java
135
import org.jfree.chart.*;
136
import org.jfree.chart.plot.*;
137
import org.jfree.chart.renderer.category.*;
138
139
// Get the plot from a chart
140
JFreeChart chart = ChartFactory.createBarChart(
141
"Sales", "Month", "Revenue", dataset);
142
CategoryPlot plot = chart.getCategoryPlot();
143
144
// Customize appearance
145
plot.setBackgroundPaint(Color.LIGHT_GRAY);
146
plot.setRangeGridlinePaint(Color.WHITE);
147
plot.setRangeGridlinesVisible(true);
148
149
// Customize renderer
150
BarRenderer renderer = (BarRenderer) plot.getRenderer();
151
renderer.setSeriesPaint(0, Color.BLUE);
152
renderer.setSeriesOutlinePaint(0, Color.BLACK);
153
renderer.setDrawBarOutline(true);
154
155
// Add range marker
156
ValueMarker marker = new ValueMarker(100.0);
157
marker.setPaint(Color.RED);
158
marker.setLabel("Target");
159
plot.addRangeMarker(marker);
160
```
161
162
### XYPlot
163
164
Plot for displaying XY coordinate data such as scatter plots, line charts, and time series. Supports continuous data with numeric axes.
165
166
```java { .api }
167
/**
168
* A plot for displaying XY data (scatter plots, time series, etc.)
169
*/
170
public class XYPlot extends Plot implements ValueAxisPlot, Pannable, Zoomable, Cloneable {
171
172
// Dataset management
173
public XYDataset getDataset();
174
public XYDataset getDataset(int index);
175
public void setDataset(XYDataset dataset);
176
public void setDataset(int index, XYDataset dataset);
177
public int getDatasetCount();
178
public List getDatasets();
179
180
// Domain axis (X axis)
181
public ValueAxis getDomainAxis();
182
public ValueAxis getDomainAxis(int index);
183
public void setDomainAxis(ValueAxis axis);
184
public void setDomainAxis(int index, ValueAxis axis);
185
public int getDomainAxisCount();
186
public List getDomainAxes();
187
188
// Range axis (Y axis)
189
public ValueAxis getRangeAxis();
190
public ValueAxis getRangeAxis(int index);
191
public void setRangeAxis(ValueAxis axis);
192
public void setRangeAxis(int index, ValueAxis axis);
193
public int getRangeAxisCount();
194
public List getRangeAxes();
195
196
// Renderer management
197
public XYItemRenderer getRenderer();
198
public XYItemRenderer getRenderer(int index);
199
public void setRenderer(XYItemRenderer renderer);
200
public void setRenderer(int index, XYItemRenderer renderer);
201
public int getRendererCount();
202
public List getRenderers();
203
204
// Plot orientation
205
public PlotOrientation getOrientation();
206
public void setOrientation(PlotOrientation orientation);
207
208
// Axis positioning
209
public AxisLocation getDomainAxisLocation();
210
public AxisLocation getDomainAxisLocation(int index);
211
public void setDomainAxisLocation(AxisLocation location);
212
public void setDomainAxisLocation(int index, AxisLocation location);
213
public AxisLocation getRangeAxisLocation();
214
public AxisLocation getRangeAxisLocation(int index);
215
public void setRangeAxisLocation(AxisLocation location);
216
public void setRangeAxisLocation(int index, AxisLocation location);
217
218
// Gridlines
219
public boolean isDomainGridlinesVisible();
220
public void setDomainGridlinesVisible(boolean visible);
221
public Paint getDomainGridlinePaint();
222
public void setDomainGridlinePaint(Paint paint);
223
public Stroke getDomainGridlineStroke();
224
public void setDomainGridlineStroke(Stroke stroke);
225
public boolean isDomainMinorGridlinesVisible();
226
public void setDomainMinorGridlinesVisible(boolean visible);
227
public boolean isRangeGridlinesVisible();
228
public void setRangeGridlinesVisible(boolean visible);
229
public Paint getRangeGridlinePaint();
230
public void setRangeGridlinePaint(Paint paint);
231
public Stroke getRangeGridlineStroke();
232
public void setRangeGridlineStroke(Stroke stroke);
233
public boolean isRangeMinorGridlinesVisible();
234
public void setRangeMinorGridlinesVisible(boolean visible);
235
236
// Zero baselines
237
public boolean isDomainZeroBaselineVisible();
238
public void setDomainZeroBaselineVisible(boolean visible);
239
public boolean isRangeZeroBaselineVisible();
240
public void setRangeZeroBaselineVisible(boolean visible);
241
242
// Markers and annotations
243
public void addRangeMarker(Marker marker);
244
public void addRangeMarker(int index, Marker marker, Layer layer);
245
public void clearRangeMarkers();
246
public void addDomainMarker(ValueMarker marker);
247
public void addDomainMarker(int index, ValueMarker marker, Layer layer);
248
public void clearDomainMarkers();
249
public void addAnnotation(XYAnnotation annotation);
250
public void removeAnnotation(XYAnnotation annotation);
251
public List getAnnotations();
252
}
253
```
254
255
**Usage Example:**
256
257
```java
258
import org.jfree.chart.*;
259
import org.jfree.chart.plot.*;
260
import org.jfree.chart.renderer.xy.*;
261
262
// Get the plot from a chart
263
JFreeChart chart = ChartFactory.createXYLineChart(
264
"Temperature", "Time", "°C", dataset);
265
XYPlot plot = (XYPlot) chart.getPlot();
266
267
// Customize appearance
268
plot.setBackgroundPaint(Color.WHITE);
269
plot.setDomainGridlinesVisible(true);
270
plot.setRangeGridlinesVisible(true);
271
plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
272
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
273
274
// Customize renderer
275
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
276
renderer.setSeriesPaint(0, Color.RED);
277
renderer.setSeriesStroke(0, new BasicStroke(2.0f));
278
renderer.setSeriesShapesVisible(0, true);
279
plot.setRenderer(renderer);
280
281
// Add domain marker for specific time
282
ValueMarker timeMarker = new ValueMarker(System.currentTimeMillis());
283
timeMarker.setPaint(Color.BLUE);
284
timeMarker.setLabel("Current Time");
285
plot.addDomainMarker(timeMarker);
286
```
287
288
### PiePlot
289
290
Plot for displaying pie chart data showing proportional relationships between categories.
291
292
```java { .api }
293
/**
294
* A plot for displaying pie chart data
295
*/
296
public class PiePlot<K> extends Plot implements Cloneable {
297
298
// Dataset management
299
public PieDataset<K> getDataset();
300
public void setDataset(PieDataset<K> dataset);
301
302
// Label generation
303
public PieSectionLabelGenerator getLabelGenerator();
304
public void setLabelGenerator(PieSectionLabelGenerator generator);
305
public boolean getLabelsVisible();
306
public void setLabelsVisible(boolean visible);
307
public Font getLabelFont();
308
public void setLabelFont(Font font);
309
public Paint getLabelPaint();
310
public void setLabelPaint(Paint paint);
311
312
// Tooltip and URL generation
313
public PieToolTipGenerator getToolTipGenerator();
314
public void setToolTipGenerator(PieToolTipGenerator generator);
315
public PieURLGenerator getURLGenerator();
316
public void setURLGenerator(PieURLGenerator generator);
317
318
// Section appearance
319
public Paint getSectionPaint(K key);
320
public void setSectionPaint(K key, Paint paint);
321
public void clearSectionPaints(boolean notify);
322
public Paint getSectionOutlinePaint(K key);
323
public void setSectionOutlinePaint(K key, Paint paint);
324
public void clearSectionOutlinePaints(boolean notify);
325
public Stroke getSectionOutlineStroke(K key);
326
public void setSectionOutlineStroke(K key, Stroke stroke);
327
public void clearSectionOutlineStrokes(boolean notify);
328
public boolean getSectionOutlinesVisible();
329
public void setSectionOutlinesVisible(boolean visible);
330
331
// Pie appearance
332
public double getStartAngle();
333
public void setStartAngle(double angle);
334
public Rotation getDirection();
335
public void setDirection(Rotation direction);
336
public double getInteriorGap();
337
public void setInteriorGap(double percent);
338
public boolean getCircular();
339
public void setCircular(boolean flag);
340
341
// Data handling
342
public double getMinimumArcAngleToDraw();
343
public void setMinimumArcAngleToDraw(double angle);
344
public boolean isIgnoreNullValues();
345
public void setIgnoreNullValues(boolean flag);
346
public boolean isIgnoreZeroValues();
347
public void setIgnoreZeroValues(boolean flag);
348
}
349
```
350
351
**Usage Example:**
352
353
```java
354
import org.jfree.chart.*;
355
import org.jfree.chart.plot.*;
356
357
// Get the plot from a pie chart
358
JFreeChart chart = ChartFactory.createPieChart(
359
"Market Share", dataset);
360
PiePlot plot = (PiePlot) chart.getPlot();
361
362
// Customize appearance
363
plot.setStartAngle(90);
364
plot.setDirection(Rotation.CLOCKWISE);
365
plot.setInteriorGap(0.05);
366
plot.setSectionOutlinesVisible(true);
367
368
// Customize individual sections
369
plot.setSectionPaint("Chrome", Color.BLUE);
370
plot.setSectionPaint("Firefox", Color.ORANGE);
371
plot.setSectionPaint("Safari", Color.GRAY);
372
373
// Set explode factor for specific section
374
plot.setExplodePercent("Chrome", 0.10);
375
376
// Hide small sections
377
plot.setMinimumArcAngleToDraw(0.02); // 2% minimum
378
plot.setIgnoreZeroValues(true);
379
```
380
381
### Multi-Dataset and Multi-Axis Support
382
383
Support for complex charts with multiple datasets, axes, and renderers.
384
385
```java { .api }
386
// CategoryPlot multi-dataset methods
387
public void mapDatasetToDomainAxis(int index, int axisIndex);
388
public void mapDatasetToRangeAxis(int index, int axisIndex);
389
public int getDomainAxisIndex(CategoryAxis axis);
390
public int getRangeAxisIndex(ValueAxis axis);
391
392
// XYPlot multi-dataset methods
393
public void mapDatasetToDomainAxis(int index, int axisIndex);
394
public void mapDatasetToRangeAxis(int index, int axisIndex);
395
public int getDomainAxisIndex(ValueAxis axis);
396
public int getRangeAxisIndex(ValueAxis axis);
397
398
// Renderer mapping
399
public CategoryItemRenderer getRendererForDataset(CategoryDataset dataset);
400
public XYItemRenderer getRendererForDataset(XYDataset dataset);
401
```
402
403
**Usage Example:**
404
405
```java
406
// Create plot with multiple datasets and axes
407
CategoryPlot plot = new CategoryPlot();
408
409
// Add primary dataset and axis
410
plot.setDataset(0, primaryDataset);
411
plot.setRangeAxis(0, new NumberAxis("Primary Values"));
412
413
// Add secondary dataset and axis
414
plot.setDataset(1, secondaryDataset);
415
plot.setRangeAxis(1, new NumberAxis("Secondary Values"));
416
plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
417
418
// Map datasets to axes
419
plot.mapDatasetToRangeAxis(0, 0); // Primary dataset to primary axis
420
plot.mapDatasetToRangeAxis(1, 1); // Secondary dataset to secondary axis
421
422
// Set different renderers
423
plot.setRenderer(0, new BarRenderer());
424
plot.setRenderer(1, new LineAndShapeRenderer());
425
```
426
427
### Plot Utilities and Advanced Features
428
429
Additional plot functionality for specialized use cases and advanced customization.
430
431
```java { .api }
432
// Crosshair support (XYPlot)
433
public boolean isDomainCrosshairVisible();
434
public void setDomainCrosshairVisible(boolean flag);
435
public double getDomainCrosshairValue();
436
public void setDomainCrosshairValue(double value);
437
public boolean isRangeCrosshairVisible();
438
public void setRangeCrosshairVisible(boolean flag);
439
public double getRangeCrosshairValue();
440
public void setRangeCrosshairValue(double value);
441
442
// Zooming and panning support
443
public void zoomDomainAxes(double factor, PlotRenderingInfo info, Point2D source);
444
public void zoomRangeAxes(double factor, PlotRenderingInfo info, Point2D source);
445
public void zoomDomainAxes(double lowerPercent, double upperPercent, PlotRenderingInfo info, Point2D source);
446
public void zoomRangeAxes(double lowerPercent, double upperPercent, PlotRenderingInfo info, Point2D source);
447
public void panDomainAxes(double percent, PlotRenderingInfo info, Point2D source);
448
public void panRangeAxes(double percent, PlotRenderingInfo info, Point2D source);
449
450
// Drawing supplier for consistent colors/shapes across renderers
451
public DrawingSupplier getDrawingSupplier();
452
public void setDrawingSupplier(DrawingSupplier supplier);
453
454
// Dataset ordering for CategoryPlot
455
public DatasetRenderingOrder getDatasetRenderingOrder();
456
public void setDatasetRenderingOrder(DatasetRenderingOrder order);
457
458
// Series rendering order for CategoryPlot
459
public SortOrder getColumnRenderingOrder();
460
public void setColumnRenderingOrder(SortOrder order);
461
public SortOrder getRowRenderingOrder();
462
public void setRowRenderingOrder(SortOrder order);
463
```
464
465
**Usage Example:**
466
467
```java
468
// Enable crosshairs on XY plot
469
XYPlot plot = (XYPlot) chart.getPlot();
470
plot.setDomainCrosshairVisible(true);
471
plot.setRangeCrosshairVisible(true);
472
plot.setDomainCrosshairPaint(Color.RED);
473
plot.setRangeCrosshairPaint(Color.RED);
474
475
// Configure zooming
476
plot.setDomainZoomable(true);
477
plot.setRangeZoomable(true);
478
479
// Set rendering order for multiple datasets
480
CategoryPlot categoryPlot = (CategoryPlot) chart.getPlot();
481
categoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
482
categoryPlot.setRowRenderingOrder(SortOrder.ASCENDING);
483
```