Using symbols in Legends

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Emily Dickinson

Using symbols in Legends

Post by Emily Dickinson » Tue Aug 13, 2002 10:28 pm

Is there a way to make the Legend class
output SYMBOLS rather than only colors?

I'm creating a scatterplot for some series data where
there are 4 series. 4 symbols are used for each of
the series: triangle, circle, square, diamond.
However, the Legend class only uses a single symbol:
the square. You can differentiate one series from
another only by the color. This looks fine until
you print it out in black and white; and then there's no way to
differentiate one series from another.

Any help greatly appreciated.

--Emily

Harald Faber

Re: Using symbols in Legends

Post by Harald Faber » Wed Aug 14, 2002 8:37 am

IMO you have to customize the StandardLegend class in the draw(...) method. So either you modify the original StandardLegend or you create your own MyFreeChart extends JFreeChart where in the constructor you change

if (createLegend) {
this.legend = Legend.createInstance(this);

into

if (createLegend) {
this.legend = new MyStandardLegend(this);

where your MyStandardLegend of course extends StandardLegend etc., see http://www.object-refinery.com/phorum-3 ... 908&t=3908


So in your case I think you will have to change this paragraph

// Draw individual series elements
for (int i=0; i<items.length; i++) {
g2.setPaint(chart.getPlot().getSeriesPaint(i));
Shape keyBox = items.getMarker();
g2.fill(keyBox);
if (this.outlineKeyBoxes) {
g2.setPaint(this.keyBoxOutlinePaint);
g2.setStroke(this.keyBoxOutlineStroke);
g2.draw(keyBox);
}
g2.setPaint(itemPaint);
g2.drawString(items.label, (float)items.labelPosition.getX(), (float)items.labelPosition.getY());
}

to your own wishes. You might draw some Images by g2.drawImage(...) instead or g2.drawRect(...) or whatever.

Hope this helps.

David Gilbert

Re: Using symbols in Legends

Post by David Gilbert » Wed Aug 14, 2002 9:36 am

Hi Emily,

The current legend code has some limitations, most of it was written a long time ago.

Harald's post regarding enhancements is correct...I'm hoping that someone will contribute a new legend class that is better than the existing one. Otherwise, at some point, I will have a go at it.

Regards,

DG.

Emily Dickinson

Re: Using symbols in Legends

Post by Emily Dickinson » Wed Aug 14, 2002 9:00 pm

Worked great. Thanks for the quick response!
--Emily

Mark Cutler

Re: Using symbols in Legends

Post by Mark Cutler » Thu Oct 03, 2002 2:46 pm

I did it a little differently.
I added a parameter to the createLegendItem method of the index number of the series and I added:

Shape mark = chart.getPlot().getShape(index,null,xloc,yloc,boxDim);
item.setMarker(mark);

instead of item.setMarker(new Rectangle2D.Float(xloc, yloc, boxDim, boxDim));

I then added the i field in the draw method when it calls createLegendItem
items = createLegendItem(g2, legendItemLabels.get(i).toString(), offset, totalHeight, i);

It works for me. That way I get the corresponding shape in the legend as was used in the graph.

Locked