How to replace tooltip & legend strings?

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

How to replace tooltip & legend strings?

Post by Lisa Drebendorf » Thu Aug 08, 2002 4:04 pm

Hello David and all the others,

I need to replace both tooltip and legend strings and don't know how to do it. This seems to be quite simple, but as I am new to Java and JFreeChart...

Here is the code-snippet:

CategoryDataset cdata= new DefaultCategoryDataset(data);
JFreeChart jfchart = ChartFactory.createVerticalBarChart(title, category, value, cdata, true);

CategoryPlot plot = jfchart.getCategoryPlot();

java.util.List legend = plot.getLegendItemLabels();
legend.set(0, "hi there!"); //Does not change the legend's text?!

CategoryItemRenderer renderer = plot.getRenderer();
//renderer.?? //don't know how to get to change the tooltip's text

ChartPanel chartPanel = new ChartPanel(jfchart);

Maybe I'll have to set up my own renderer or overwrite some methods, but I do not know how to... [code example?]

Any help ist appreciated!

Thanks in advance!

Lisa

Harald Faber

Re: How to replace tooltip & legend strings?

Post by Harald Faber » Thu Aug 08, 2002 4:33 pm

I have implemented a derived JFreeChart class and changed the legend call:

public class MyFreeChart extends JFreeChart and in this constructor I have changed the legend call:

public MyFreeChart(String title, Font titleFont,
Plot plot,
boolean createLegend) {
SNIP
if (createLegend) {
// this.legend = Legend.createInstance(this);
this.legend = new MyStandardLegend(this);
this.legend.addChangeListener(this);
}

and in the MyStandardLegend the draw(...) method has to be customized a bit at the position where you see

g2.drawString(items.getLabel(),
//(float)items.labelPosition.getX(),
(float)items.getLabelPosition().getX(),
//(float)items.labelPosition.getY());
(float)items.getLabelPosition().getY());

where you have to define your own MyLegendItem class (extends LegendItem) and therein implement
public String getLabel(){return label;}
public Point2D getLabelPosition(){return labelPosition;}
public void setLabelPosition(Point2D newPosition){labelPosition = newPosition;}

because the returned attributes are protected so that you cannot use them directly in MyStandardLegend where you have to change

LegendItem[] items = new LegendItem[legendItemLabels.size()];

into

MyLegendItem[] items = new MyLegendItem[legendItemLabels.size()];

And the setLabelPosition(...) in the MyLegendItem is necessary because in MyStandardLegend there is also
item.labelPosition = new Point2D.Float(xloc, yloc); in the createLegendItem(...) method which has to be changed to
item.setLabelPosition(new Point2D.Float(xloc, yloc));

That is what works here. Hope that helps and that I haven't forgotten something. :-)

Lisa Drebendorf

Re: How to replace tooltip & legend strings?

Post by Lisa Drebendorf » Mon Aug 19, 2002 9:18 am

Hello Harald,

thank you for your quick response and sorry about my late answer.
I've tried for several days to follow your suggestions but without success. This seems much to complicate for a java-beginner like me.

But I am happy to say, that I was able to reformat my tooltip-strings (while overwriting StandardCategoryToolTipGenerator) this weekend. So this problem is solved at least.

Currently, printing the verticalBarChart on my printer is at the top of my todo-list; I am able to print the chart, but (maybe because it is Graphics2D - object) quality is suffering (blurred printout, no clear shapes) still...

I will go back on replacing my legend strings some time later.

Nevertheless thank you very much for your help, Harald!

So long,
Lisa

Jason Childers

Re: How to replace tooltip & legend strings?

Post by Jason Childers » Fri Aug 23, 2002 2:02 am

Lisa,

I'm trying to do much the same as you, but instead of extending JFreeChart and dealing with the Legend there, I'm extending PiePlot and overriding getLegendItemLabels(). I've had good success with getting the LegendItems in the form I want them in that way, but for some reason this is causing my JFrame to not properly paint the rest of my components. I'm going to have to post to the list about this issue. But if you want to try extending PiePlot and overriding the getLegendItemLabels() in whatever you're doing. That seems to be the way you might want to do it.

For a good example of what you want to do check out PiePlot.getLegendItemLabels().

Although, I do have to post to the list to see if what I'm trying to do is a good approach.

Cheers,
-Jason

Lisa Drebendorf

Re: How to replace tooltip & legend strings?

Post by Lisa Drebendorf » Mon Aug 26, 2002 1:46 pm

Hi Jason,

thanks for your response! This seems to be the way to do it. I'll give it a try when I have solved my printing prob! (see my other posting)

Thanks for your help!

Lisa

anurag

Re: How to replace tooltip

Post by anurag » Fri Aug 30, 2002 5:02 am

Hi Lisa
I am trying to change the tooltip texts of piechart. But i am not able to do that.Could u please provide me the code or any clue, i will appreciate that.

thanks
anurag

Locked