OK, I've got a ChartPanel displaying time-based graphs of one family of variable, and I want to look at another. Now suppose my user chooses a different family of variables, such that the chart's *Title* should be updated.
I see a setTitles() method, but no "plain vanilla" setTitle() . There must be an easy way to do this???
Thanks,
Irv Thomae
Changing Title (in Mid-Stream)?
Re: Changing Title (in Mid-Stream)?
Sorry - of course I meant to refer not to class ChartPanel but to class JFreeChart. I did experiment with addTitle(), but that quite literally added a second title below (and in a smaller font than) the first.
Re: Changing Title (in Mid-Stream)?
Hi Irv,
Since adding the multiple titles feature to JFreeChart, I've been conscious that it is more difficult to handle the common case where the chart has just one title. Perhaps the solution is to replace the title list with two things: the main title and a subtitle list.
Anyway, for now if your chart has a single title and you want to replace it, you need to create a new title, add it to a list, then call the setTitles(...) method in the JFreeChart class.
Regards,
DG.
Since adding the multiple titles feature to JFreeChart, I've been conscious that it is more difficult to handle the common case where the chart has just one title. Perhaps the solution is to replace the title list with two things: the main title and a subtitle list.
Anyway, for now if your chart has a single title and you want to replace it, you need to create a new title, add it to a list, then call the setTitles(...) method in the JFreeChart class.
Regards,
DG.
Re: Changing Title (in Mid-Stream)?
David,
You're right that "it has become more difficult" to deal with the single-title case. I implemented your suggestion as follows (the method "buildTitle()" returns String):
// Code to change the displayed Title of a JFreeChart object "chart":
// (List is an interface, so use a class that implements it:)
Vector titleVec = new Vector(1);
TextTitle revised = new TextTitle(buildTitle());
vec.addElement(revised);
java.util.List titleList = (java.util.List)titleVec;
chart.setTitles(titleList);
It does work, but the first time that a change is made, the appearance changes markedly. Apparently, the initial Title (whose value is set when its passed as an argument to the JFreeChart constructor) has special status: it gets displayed using a larger font size than title(s) set by setTitles(). On the other hand, the smaller-font title does indeed replace the original, so maybe I don't really understand what's happening.
You're right that "it has become more difficult" to deal with the single-title case. I implemented your suggestion as follows (the method "buildTitle()" returns String):
// Code to change the displayed Title of a JFreeChart object "chart":
// (List is an interface, so use a class that implements it:)
Vector titleVec = new Vector(1);
TextTitle revised = new TextTitle(buildTitle());
vec.addElement(revised);
java.util.List titleList = (java.util.List)titleVec;
chart.setTitles(titleList);
It does work, but the first time that a change is made, the appearance changes markedly. Apparently, the initial Title (whose value is set when its passed as an argument to the JFreeChart constructor) has special status: it gets displayed using a larger font size than title(s) set by setTitles(). On the other hand, the smaller-font title does indeed replace the original, so maybe I don't really understand what's happening.
Re: Changing Title (in Mid-Stream)?
The TextTitle class carries around it's own font information. It has a default font set as follows:
/** The default font. */
public static final Font DEFAULT_FONT = new Font("SansSerif", Font.BOLD, 12);
But the JFreeChart class accepts a title string in the constructor, and creates a default title (an instance of TextTitle) using another default font (which should probably be removed) defined in JFreeChartConstants.java:
/** The default font for titles. */
public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);
Now the possible solutions are:
1) make these defaults the same;
2) eliminate the DEFAULT_TITLE_FONT setting, and rely on the DEFAULT_FONT in the TextTitle class;
3) do nothing, because it is OK that the first title on the chart is big, and the subsequent titles (subtitles generally) are smaller;
Either 2) or 3) seems best to me.
Regards,
DG
/** The default font. */
public static final Font DEFAULT_FONT = new Font("SansSerif", Font.BOLD, 12);
But the JFreeChart class accepts a title string in the constructor, and creates a default title (an instance of TextTitle) using another default font (which should probably be removed) defined in JFreeChartConstants.java:
/** The default font for titles. */
public static final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);
Now the possible solutions are:
1) make these defaults the same;
2) eliminate the DEFAULT_TITLE_FONT setting, and rely on the DEFAULT_FONT in the TextTitle class;
3) do nothing, because it is OK that the first title on the chart is big, and the subsequent titles (subtitles generally) are smaller;
Either 2) or 3) seems best to me.
Regards,
DG
Re: Changing Title (in Mid-Stream)?
Comment on 3: When both a title and one or more subtitles are bing displayed simultaneously, it is indeed appropriate that the "first" title shoud use a larger font.
But when, as in my application, the intent was to replace the chronologically "first" title, rather than to add a "sub"title, the change in font size falls closer to the "b" end of the old "That's not a bug, it's a feature!" spectrum.
The solution, I submit, is either to add a setMainTitle() method to class JFreeChart, or else modify the code such that if JFreeChart.setTitles() has been called, the _first_ element in that List will be displayed using the DEFAULT_TITLE_FONT, and DEFAULT_FONT will be used for the others.
But when, as in my application, the intent was to replace the chronologically "first" title, rather than to add a "sub"title, the change in font size falls closer to the "b" end of the old "That's not a bug, it's a feature!" spectrum.
The solution, I submit, is either to add a setMainTitle() method to class JFreeChart, or else modify the code such that if JFreeChart.setTitles() has been called, the _first_ element in that List will be displayed using the DEFAULT_TITLE_FONT, and DEFAULT_FONT will be used for the others.
Re: Changing Title (in Mid-Stream)?
Hi Irv,
I think you are right, I will probably make every chart have a main title (optional, but always text) plus a list of subtitles (also optional, since the list will be empty by default, but capable of incorporating text titles, image titles and whatever else).
The main title can have simple methods to set the text, font, color, and position, which will make life easier for everyone.
Regards,
DG
I think you are right, I will probably make every chart have a main title (optional, but always text) plus a list of subtitles (also optional, since the list will be empty by default, but capable of incorporating text titles, image titles and whatever else).
The main title can have simple methods to set the text, font, color, and position, which will make life easier for everyone.
Regards,
DG