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
Using symbols in Legends
Re: Using symbols in Legends
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.
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.
Re: Using symbols in Legends
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.
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.
Re: Using symbols in Legends
Worked great. Thanks for the quick response!
--Emily
--Emily
Re: Using symbols in Legends
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.
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.