A discussion forum for JFreeChart (a 2D chart library for the Java platform).
-
dweems
- Posts: 12
- Joined: Sat Feb 20, 2010 9:59 pm
- antibot: No, of course not.
Post
by dweems » Tue Mar 30, 2010 3:35 am
I have an application where I'd like to dynamically change the legend text on an XYPlot using XYLineAndShapeRenderer and TimeSeriesCollection. I'm only defining one TimeSeries on the TimeSeriesCollection and simply running a variety of models to generate the data. It's the name of the model that I want on the legend. I see that the actual text appearing on the legend is equivalent to the name of the TimeSeries when it is instantiated. So I figured I'd change the name of the TimeSeries. But the API provides no such function (that I can find). Indeed, after creating the TimeSeries, you can't even get the name. You can call setDescription, but that has no effect. So I looked at XYLineAndShapeRenderer and found method setBaseItemLegendLabelGenerator. This method accepts one argument, an XYItemLabelGenerator, which is an interface. I found StandardXYItemLabelGenerator() implements the interface and it contains a single method: generateLabel, which returns the label. That looked promising, so I wrote the following code:
Code: Select all
renderer.setBaseItemLabelGenerator( new StandardXYItemLabelGenerator() {
public String generateLabel( XYDataset dataset,
int series,
int item ) {
String label = "label";
if ( MarketPlot.movingAverageType == MovingAverageType.SIMPLE )
label = "SMA";
else if ( MarketPlot.movingAverageType == MovingAverageType.WEIGHTED )
label = "WMA";
else if ( MarketPlot.movingAverageType == MovingAverageType.EXPONENTIAL )
label = "EMA";
return label;
}
}
);
but this didn't work either. I can't find anything in the API, Developer Guide or Demo Collection to indicate how to change a legend. I hope one of you knows how.
Thanks for your help.
-
dweems
- Posts: 12
- Joined: Sat Feb 20, 2010 9:59 pm
- antibot: No, of course not.
Post
by dweems » Wed Mar 31, 2010 10:16 pm
Ok, so I've learned a bit more but am still frustrated. I learned that the XYPlot has a method called getLegendItems() which returns a LegendItemCollection and within there is a get( int index ) method that allows me to get a LegendItem. That class has a getLabel() method but there is no setLabel() method. I can change the label font and paint but not the label itself. Another thought would be to use this LegendItem to create a new one and then replace the previous one with this new one. But the LegendItemCollection only has an add( LegendItem) method but I see no mechanism for replacing one LegendItem with another. What am I missing?
-
paradoxoff
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Post
by paradoxoff » Thu Apr 01, 2010 9:32 pm
You were already quite close

.
Instead of setting the ItemLabelGenerator, set the LegendItemLabelGenerator (which is an instance of XYSeriesLabelGenerator).
Fortunately, there is a standard implementation available, and the method declared by this interface has the same name and a similar argument list as an ItemLabelGenerator.
-
dweems
- Posts: 12
- Joined: Sat Feb 20, 2010 9:59 pm
- antibot: No, of course not.
Post
by dweems » Thu Apr 01, 2010 10:31 pm
I missed that method in between all the deprecated ones.

Here is what I wrote for a similar case. Do you see why this is not working?
Code: Select all
XYBarRenderer xyBarRenderer = new XYBarRenderer();
xyBarRenderer.setDrawBarOutline( false );
xyBarRenderer.setBaseToolTipGenerator( StandardXYToolTipGenerator.getTimeSeriesInstance() );
xyBarRenderer.setLegendItemLabelGenerator(
new StandardXYSeriesLabelGenerator() {
public String generateLabel( TimeSeriesCollection dataset, int series) {
String label = "Volume";
return label;
}
}
);
-
paradoxoff
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Post
by paradoxoff » Fri Apr 02, 2010 6:58 am
The generateLabel method expects an XYDataset parameter, you have used a TimeSeriesCollection in your method.
Even if the supplied XYDataset parameter might be a TimeSeriesCollection, your generator is not aware of it and calls the generateLabel method of its superclass.
Exchange the argument type and do the type checking/casting to a TimeSeriesCollection inside the method body of your generateLabel method.
-
dweems
- Posts: 12
- Joined: Sat Feb 20, 2010 9:59 pm
- antibot: No, of course not.
Post
by dweems » Fri Apr 02, 2010 6:04 pm
Sometimes you get bit by the simplest things. My compiler didn't like XYDataset but since TimeSeriesCollection is an XYDataset I thought that was what it was looking for. Instead I simply left out the proper import statement to get to the XYDataset.

Everything is copacetic now. Thanks a million.