Hello David,
My management required that the dates on charts use an ISO format YYYY-MM-DD rather than your current DD-MMM-YY format (which I do not mind). Below is the rather hacky code I used to achieve this result.
The first observation is your heavy use of initializer parameters. I sugest that you limit their use to very commonly used ones, and use set*() to override properties on the initialized objects instead. The reason can be seen from my code below -- in order to override an inner initializer I needed to copy out other initializers, not good.
The second is that my fix is obviously a hack! If there is a better way to do it I could not see it. My guess is that you need to create your own ChartDateFormatter which has methods for estimating the size required for the marks, swapping between times and dates etc. That should then be the (only) parameter a user can set. Such a class would in turn depend on SimpleDateFormatter. It would be simple and easy to customerize.
Thanks for your previous help,
Anthony
/**
* Copied from JFreeChart...ChartFactory.java in order to control
* date formatting.<p>
*
* Creates and returns a time series chart. A time series chart
* is an XYPlot with a date axis (horizontal) and a number axis
* (vertical), and each data item is connected with a line. <P>
* Note that you can supply a TimeSeriesDataset to this method as
* it is a subclass of XYDataset.
* @param title The chart title.
* @param timeAxisLabel A label for the time axis.
* @param valueAxisLabel A label for the value axis.
* @param data The dataset for the chart.
* @param legend A flag specifying whether or not a legend is required. */
public JFreeChart myCreateTimeSeriesChart(
String title, String timeAxisLabel,
String valueAxisLabel, XYDataset data,
boolean legend) {
ValueAxis timeAxis = myNewHorizontalDateAxis(timeAxisLabel);
//timeAxis.setCrosshairLockedOnData(false);
NumberAxis valueAxis = new VerticalNumberAxis(valueAxisLabel);
valueAxis.setAutoRangeIncludesZero(false); // override default
//valueAxis.setCrosshairLockedOnData(false);
XYPlot plot = new XYPlot(timeAxis, valueAxis);
plot.setXYItemRenderer(new StandardXYItemRenderer(StandardXYItemRenderer.LINES));
JFreeChart chart = new JFreeChart(data, plot, title, JFreeChart.DEFAULT_TITLE_FONT, legend);
return chart;
}
/** Copied from HorizontalDateAxis
* Constructs a horizontal date axis, using default values where necessary.
* @param label The axis label.
*/
private ValueAxis myNewHorizontalDateAxis(String label) {
return new HorizontalDateAxis(
label,
Axis.DEFAULT_AXIS_LABEL_FONT,
Axis.DEFAULT_AXIS_LABEL_PAINT,
Axis.DEFAULT_AXIS_LABEL_INSETS,
true, // tick labels visible
Axis.DEFAULT_TICK_LABEL_FONT,
Axis.DEFAULT_TICK_LABEL_PAINT,
Axis.DEFAULT_TICK_LABEL_INSETS,
false, // vertical tick labels
true, // tick marks visible
Axis.DEFAULT_TICK_STROKE,
ValueAxis.DEFAULT_AUTO_RANGE,
null, null,
true, // auto tick unit selection off
new DateUnit(Calendar.DATE, 1),
new mySimpleDateFormat(),
true,
ValueAxis.DEFAULT_GRID_LINE_STROKE,
ValueAxis.DEFAULT_GRID_LINE_PAINT,
ValueAxis.DEFAULT_CROSSHAIR_VISIBLE,
DateAxis.DEFAULT_CROSSHAIR_DATE,
ValueAxis.DEFAULT_CROSSHAIR_STROKE,
ValueAxis.DEFAULT_CROSSHAIR_PAINT);
}
private class mySimpleDateFormat extends java.text.SimpleDateFormat {
mySimpleDateFormat() {
super("yyyy-MM-dd hh:mm");
}
public StringBuffer format(
Date date, StringBuffer toAppendTo,
java.text.FieldPosition fieldPosition) {
if (toLocalizedPattern().equals("d-MMM-yyyy"))
applyPattern("yyyy-MM-dd");
if (toLocalizedPattern().equals("MMM-yyyy"))
applyPattern("yyyy-MM");
StringBuffer sb = super.format(date, toAppendTo, fieldPosition);
//System.out.println("mySDF " + sb.toString());
return sb;
//return new StringBuffer("Yipes!");
}
}
Changing Date Axis
Re: Changing Date Axis
Hi Anthony,
In the NumberAxis class there is a setTickUnit(NumberTickUnit unit) method and the tick unit includes a number formatter. I plan to rewrite the DateAxis class and include a setTickUnit(DateTickUnit unit) method where the tick unit will include a date formatter...hopefully that will make it easier to customise the axis.
Regards,
DG.
In the NumberAxis class there is a setTickUnit(NumberTickUnit unit) method and the tick unit includes a number formatter. I plan to rewrite the DateAxis class and include a setTickUnit(DateTickUnit unit) method where the tick unit will include a date formatter...hopefully that will make it easier to customise the axis.
Regards,
DG.