3 questions about TimeSeriesChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
manutudescends
Posts: 7
Joined: Mon Dec 18, 2006 7:14 pm

3 questions about TimeSeriesChart

Post by manutudescends » Wed Jan 03, 2007 10:52 am

Hi,

I have a chart but i want to personalize it better.

Image

1. How can i automatically display the YLabel fully ?
2. How can i have only 2 digits in my YAxis?
3. How can i change the XAxis display ? (i want to show: 04:00:00)

Thanks.

linhvn0110
Posts: 3
Joined: Thu Jan 04, 2007 5:24 am
Location: Vietnam
Contact:

Post by linhvn0110 » Thu Jan 04, 2007 5:41 am

You can use the NumberAxis class of JFreeChart with NumberFormat for Y axis to format the values as desired and also DateAxis class of JFreeChart with SimpleDateFormat to format the value of X axis
Let's take an example below:

2/
private void buildRangeAxis(JFreeChart chart) {
XYPlot plot = chart.getXYPlot();
ValueAxis valueAxis = plot.getRangeAxis();
if (valueAxis instanceof NumberAxis) {
NumberAxis axis = (NumberAxis) plot.getRangeAxis();
NumberFormat numberFormat = null;
double maxValue = graph.getMaxValue();
if (maxValue < 1000) { // Bps
numberFormat = new DecimalFormat("0 B");
} else if (maxValue >= 1000 && maxValue <= 999999) { // Kbps
numberFormat = new DecimalFormat("0.0 K");
} else if (maxValue >= 1000000 && maxValue <= 999999999) { // Mbps
numberFormat = new DecimalFormat("0.0 M");
} else { // Gbps
numberFormat = new DecimalFormat("0.0 G");
}
axis.setNumberFormatOverride(numberFormat);
valueAxis.setTickLabelFont(new Font(graphInfo.getFont(), Font.PLAIN , 8));
valueAxis.setAutoRange(true);
}
}

3/
private void buildDomainAxis(JFreeChart chart) {
XYPlot plot = chart.getXYPlot();
ValueAxis domainAxis = plot.getDomainAxis();
if (domainAxis instanceof DateAxis) {
//MyDateAxis dateAxis = new MyDateAxis(null);
DateAxis dateAxis = (DateAxis) domainAxis;
String dateFormat = graph.getDomainAxisFormat();
// Set the date format of domain axis
dateAxis.setDateFormatOverride(new SimpleDateFormat(dateFormat));
dateAxis.setTickMarkPosition(DateTickMarkPosition.START);
dateAxis.setLowerMargin(0.05D);
dateAxis.setUpperMargin(0.05D);
// Set the tick unit
DateFormat formatter = new SimpleDateFormat(graph.getDomainAxisFormat());
DateTickUnit unit = null;
if (graph.getDomainAxisType() == DomainAxisType.SECOND) { // daily graph
unit = new DateTickUnit(DateTickUnit.HOUR, 4, formatter);
} else if (graph.getDomainAxisType() == DomainAxisType.DAY) { // weekly graph
unit = new DateTickUnit(DateTickUnit.DAY, 1, formatter);
} else if (graph.getDomainAxisType() == DomainAxisType.WEEK) { // monthly graph
unit = new DateTickUnit(DateTickUnit.DAY, 7, formatter);
} else if (graph.getDomainAxisType() == DomainAxisType.MONTH) { // yearly graph
unit = new DateTickUnit(DateTickUnit.DAY, 30, formatter);
} else { // default graph
unit = new DateTickUnit(DateTickUnit.HOUR, 2, formatter);
}
dateAxis.setTickUnit(unit);
/** Set the font */
dateAxis.setTickLabelFont(new Font(graphInfo.getFont(), Font.PLAIN , 8));
dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
plot.setDomainAxis(dateAxis);
}
}
Hi all

manutudescends
Posts: 7
Joined: Mon Dec 18, 2006 7:14 pm

Post by manutudescends » Thu Jan 04, 2007 9:46 am

thank you.

Locked