Using Legend to Configure Series Shapes and Colors

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
fowlerje
Posts: 3
Joined: Mon Aug 08, 2005 6:58 pm

Using Legend to Configure Series Shapes and Colors

Post by fowlerje » Mon Aug 08, 2005 7:16 pm

In my project, I have a simple XY plot with single range and domain, but multiple series. This works great, but I have a requirement that the user be able to configure the shape and color used to plot each series, and I'm trying to find the easiest way to fufil this requirement.

When you right click the chart, and open the Chart Properties dialog, I see in the Other tab configuration panes for Series Paint and Series Stroke with the dreaded gray "No Editor Implemented" text next to them. Does anybody know when these will be implemented and if they wil do what I need?

My fallback solution is to disable the jFreeChart legend and implement my own legend that displays each legend item (label and shape), a dropdown for shape selection, and a launcher for a Color Picker. These controls the would use Plot.getRenderer().setSeriesShape() and Plot.getRenderer().setSeriesPaint() to update the Chart to stay in sync with the legend.

Is there some better way to configure chart shapes and color at runtime, or does one of my solutions sound like they're on track?

Thanks in Advance.

uli
Posts: 24
Joined: Sat Mar 14, 2009 3:22 pm
Location: Berlin

Re: Using Legend to Configure Series Shapes and Colors

Post by uli » Sat Mar 14, 2009 3:39 pm

I was quite happy to find this question in the forum, since I have exactly the same problem. But unfortunatelly nobody answered it! Maybe anyone knows how to enable the editor for series paint and series color in the Chart Properies dialog (maybe even for the renderer, to change e.g. frome lines to bar)

Thanks,
Julia

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: Using Legend to Configure Series Shapes and Colors

Post by MitchBuell » Mon Mar 16, 2009 1:38 pm

The built in chart editor is rather simple and doesn't provide this functionality at all. However, you can replace or extend the built in chart editor to add this functionality.

The following code shows how to get and set a series color and shape. All that is left to implement is a user interface to change these.

Code: Select all

// Get the desired dataset and renderer
XYDataset dataset = chart.getXYPlot().getDataset(datasetIndex);
XYItemRenderer renderer = chart.getXYPlot().getRenderer(datasetIndex);

// Get current values
for (int seriesIndex = 0; seriesIndex < dataset.getSeriesCount(); ++seriesIndex)
{
	Comparable seriesKey = dataset.getSeriesKey(seriesIndex);
	Paint seriesPaint = renderer.getSeriesPaint(seriesIndex);
	Shape seriesShape = renderer.getSeriesShape(seriesIndex);
}

// Provide some way for user to edit the seriesPaint and seriesShape for each series
// I use a Vector to hold the values
Vector userPaint = new Vector();
Vector userShape = new Vector();

// CODE THAT LETS USER EDIT userPaint AND userShape GOES HERE

// Set the user values
for (int seriesIndex = 0; seriesIndex < dataset.getSeriesCount(); ++seriesIndex)
{
	renderer.setSeriesPaint(seriesIndex, userPaint.get(seriesIndex));
	renderer.setSeriesShape(seriesIndex, userShape.get(seriesIndex));
}
If you want to add this series/renderer editor to the built in chart editor, see here on how to modify the chart editor: http://www.jfree.org/phpBB2/viewtopic.p ... 702#p74702

Locked