SETTING PERIOD INTERVALS IN MULTIPLE AXIS TIME CHART ??

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

SETTING PERIOD INTERVALS IN MULTIPLE AXIS TIME CHART ??

Post by Guest » Wed Mar 24, 2004 9:09 pm

hi ,
I wrote this program for generating multipel axis chart . I dound that if i have less values i dont retain the interval between the periods . in my program below i have the values for the years 1997 and 1999 but the chart thats getting generated for the intervals of 2 months . how do i set the interval to be in years rather than in 2 months



import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.Spacer;
import org.jfree.chart.TextTitle;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.data.XYDataset;
import org.jfree.data.time.Minute;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class MultipleAxisDemo1 extends ApplicationFrame {

public MultipleAxisDemo1(String title) {

super(title);
JFreeChart chart = createChart();
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(600, 270));
chartPanel.setHorizontalZoom(true);
chartPanel.setVerticalZoom(true);
setContentPane(chartPanel);

}

private JFreeChart createChart() {

XYDataset dataset1 = createXYDataset1("Agricultural Land(Hectares)");


JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Multiple Axis Demo 1",
"Time of Day",
"Primary Range Axis",
dataset1,
true,
true,
false
);

chart.setBackgroundPaint(Color.white);
chart.addSubtitle(new TextTitle("Four datasets and four range axes."));
XYPlot plot = chart.getXYPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));

StandardXYItemRenderer renderer = (StandardXYItemRenderer) plot.getRenderer();
renderer.setPaint(Color.black);


// AXIS 3
NumberAxis axis3 = new NumberAxis("Range Axis 3");
axis3.setLabelPaint(Color.blue);
axis3.setTickLabelPaint(Color.blue);
plot.setSecondaryRangeAxis(1, axis3);
plot.setSecondaryRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);

XYDataset dataset3 = createXYDataset2("Agricultural machinery, tractors");

plot.setSecondaryDataset(1, dataset3);
plot.mapSecondaryDatasetToRangeAxis(1, new Integer(1));

plot.setSecondaryRenderer(1, new StandardXYItemRenderer());
plot.getSecondaryRenderer(1).setSeriesPaint(0, Color.blue);



return chart;
}



private XYDataset createXYDataset1(String name) {


TimeSeries series = null;
RegularTimePeriod period = null;
double[] value={7910,8000};
int year =1997;
series = new TimeSeries(name, (new org.jfree.data.time.Year()).getClass());
TimeSeriesCollection dataset = new TimeSeriesCollection();

for (int i = 0; i < 2; i++) {

year = year+2;
period = new org.jfree.data.time.Year(year);
series.add(period, value);


;
System.out.println("period="+period);
System.out.println("value="+value);

}
dataset.addSeries(series);




return dataset;

}

private XYDataset createXYDataset2(String name) {


TimeSeries series = null;
RegularTimePeriod period = null;
double[] value={2000,5000};
int year =1997;
series = new TimeSeries(name, (new org.jfree.data.time.Year()).getClass());
TimeSeriesCollection dataset = new TimeSeriesCollection();

for (int i = 0; i < 2; i++) {

year = year+2;
period = new org.jfree.data.time.Year(year);
series.add(period, value);


;
System.out.println("period="+period);
System.out.println("value="+value);

}
dataset.addSeries(series);




return dataset;

}

public static void main(String[] args) {

MultipleAxisDemo1 demo = new MultipleAxisDemo1("Multiple Axis Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);

}

}


Thanks
Murugan

[/img]

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Thu Mar 25, 2004 10:08 am

See the tick unit collection that is created in the createStandardDateTickUnits() method of the DateAxis class. Create something similar in your own code, but without the units less than 1 year, then install it using the setStandardTickUnits() method.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

amurugan
Posts: 5
Joined: Thu May 01, 2003 8:38 pm
Contact:

TRIED AND TESTED

Post by amurugan » Thu Mar 25, 2004 4:30 pm

hi david ,
Thanks for your reply . I tried with the following code

DateAxis daxis = (DateAxis) plot.getDomainAxis();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
DateTickUnit unit = new DateTickUnit(DateTickUnit.YEAR,1 , sdf);

daxis.setTickUnit(unit);

and then i found that the in the x axis i had gthe years 1999 2000 and 2001 . All i want is the years 1999 and 2001 to be displayed bcos thats for which i am generating the graph .

kindly request you to reply at your earliest

Thanks ,
Murugan

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Mar 26, 2004 6:43 pm

Try changing the '1' in your code to a '2'. Something tells me that might give you tick marks of '1998', '2000', '2002', but try it anyway.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Guest

YES IT DID WORKED THANKS

Post by Guest » Fri Mar 26, 2004 9:09 pm

hi david ,
I did change it and the periods r getting rendered as u predicted .

Thanks ,
Murugan

Locked