set default date for HorizontalDateAxis

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

set default date for HorizontalDateAxis

Post by bob yang » Thu Feb 20, 2003 2:40 am

hi, I have coding list below, I like to set xAxis starts from 2003/1/1 then how can I do so? thank you.

when I run following codes, it start from 12/31/1969



xAxis= new HorizontalDateAxis("Time");

DateTickUnit dateTickUnit=new DateTickUnit(DateTickUnit.MILLISECOND,2,new SimpleDateFormat("MM/dd/yyyy hh:mm:ss"));

xAxis.setTickUnit(dateTickUnit);

morpheus

Re: set default date for HorizontalDateAxis

Post by morpheus » Thu Feb 20, 2003 1:01 pm

I think u want to display every 2 days value then :

DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnit.DATE,2,new SimpleDateFormat("MM/dd/yyyy") )

would do it...

hope this helps


Regards
Morpheus

bob yang

Re: set default date for HorizontalDateAxis

Post by bob yang » Thu Feb 20, 2003 9:54 pm

thank you.
I try this

DateTickUnit dateTickUnit = new DateTickUnit(DateTickUnit.DAY,2,new SimpleDateFormat("MM/dd/yyyy") );

but all my time label are gone from the chart

I just wonder can I use something like .addToDate(date) to set the starting day as 2003 instead of 1969?
thank you

David Gilbert

Re: set default date for HorizontalDateAxis

Post by David Gilbert » Fri Feb 21, 2003 12:55 pm

Hi Bob,

By default, the range showing on your date axis is driven by the data, not by settings on the axis. The x-values from your dataset are interpreted by the date axis as "milliseconds since 1-Jan-1970" (the encoding used by java.util.Date). So you need to look at how you created your dataset...

Regards,

Dave Gilbert

Joachim

Re: set default date for HorizontalDateAxis

Post by Joachim » Fri Feb 21, 2003 12:58 pm

You have to set the Axis Range like that:

theDateAxis = new HorizontalDateAxis(null);
theDateAxis.setRange(startTime, endTime);

Joachim

bob yang

Re: set default date for HorizontalDateAxis

Post by bob yang » Fri Feb 21, 2003 9:35 pm

David,
thank you for the hint. I will try to change the dataset value.

Joachim,
I add this
xAxis.setRange(new java.util.Date(2003,1,1,1,1,1),new java.util.Date(2003,2,1,1,1,1));
to replace my
xAxis.setRange(10,1000);

I have no problems to compile it, but I cannot really see it. it sounds like the applet is busy at somewhere, and there is no error. do you know the reason? thank you.


private XYSeries xYSeries1= new XYSeries("xYSeries1");;
private XYSeriesCollection dataset = new XYSeriesCollection(); private private HorizontalDateAxis xAxis= new HorizontalDateAxis("Time");
private VerticalNumberAxis yAxis= new VerticalNumberAxis("yAxis");

for (double i=1; i<=200; i++) {
xYSeries1.add(i*1000,11+i);
}


dataset.addSeries(xYSeries1);
DateTickUnit dateTickUnit=new DateTickUnit(DateTickUnit.MILLISECOND,2,new SimpleDateFormat("MM/dd/yyyy hh:mm:ss"));
xAxis.setTickUnit(dateTickUnit);
XYPlot xyplot = new XYPlot(dataset, xAxis, yAxis);
xyplot.setBackgroundPaint(Color.black);
xAxis.setRange(new java.util.Date(2003,1,1,1,1,1),new java.util.Date(2003,2,1,1,1,1));
xAxis.setTickLabelsVisible(true);
JFreeChart chart = new JFreeChart("Data", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
ChartPanel chartPanel = new ChartPanel(chart);

bob yang

Re: set default date for HorizontalDateAxis

Post by bob yang » Sat Feb 22, 2003 3:30 am

I found something funny...

if I add this line

xyplot.zoom(1);

then the time is correct as current...

bob yang

Re: set default date for HorizontalDateAxis

Post by bob yang » Mon Feb 24, 2003 11:41 pm

ok, problem solved.
1) before xAxis.setRange(date,date), you must have xAxis.setAutoRange(true);
2) when set up date range must using the date difference from 1900/1/1,0:0:0 so this is used
xAxis.setRange(new java.util.Date((intsetMinimumAxisValue-1900),(3-1),(1-1),(1-1),(1-1),(1-1)),new java.util.Date((intsetMinimumAxisValue-1900)+1,(3-1),(1-1),1,1,1));
3) it doesn't work with xyplot for me in this case, so I create createTimeSeriesChart first, then get xplot from TimeSeriesChart to make it work.



private JButton jBPlay = new JButton();
private TimeSeries xYSeries1 = new TimeSeries("xYSeries1");
private TimeSeriesCollection dataset = new TimeSeriesCollection();
private BasicStroke seriesStroke;
private HorizontalDateAxis xAxis= new HorizontalDateAxis("Time");
private VerticalNumberAxis yAxis= new VerticalNumberAxis("yAxis");
private XYPlot xyplot = new XYPlot(dataset, xAxis, yAxis);
private JFreeChart chart = ChartFactory.createTimeSeriesChart("Test", "Day", "Value", dataset, true, true, true);
private ChartPanel chartPanel = new ChartPanel(chart);
private int intsetMaximumAxisValue=2003;
private int intsetMinimumAxisValue=1990;
private Applet1.TimeMoveingSpeed subClass= new Applet1.TimeMoveingSpeed();
private JButton jStop = new JButton();
private JButton jBZoom = new JButton();
private Day current = new Day(new java.util.Date((1990-1900),(3-1),(1-1),(1-1),(1-1),(1-1))); //debug
private double value = 100.0; //debug


public Applet1()
{
xyplot = chart.getXYPlot();
}

public void init()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}


public void setData()
{

for (int i = 0; i < 8000; i++) {
try {
value = value + Math.random() - 0.5;
xYSeries1.add(current, new Double(value));
current = (Day) current.next();
}
catch (SeriesException e) {
System.err.println("Error adding to series");
}
}
}


private void jbInit() throws Exception
{
setData();
dataset.addSeries(xYSeries1);

xAxis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH,2,new SimpleDateFormat("MM/dd/yyyy")));
xAxis.setAutoRange(true); // must be here, otherwise, setrange(date,date) will not function correctly
//xAxis.setRange(new java.util.Date((1990-1900),1,1,1,1,1),new java.util.Date((2003-1900),2,1,1,1,1));
xAxis.setRange(new java.util.Date((intsetMinimumAxisValue-1900),(3-1),(1-1),(1-1),(1-1),(1-1)),new java.util.Date((intsetMinimumAxisValue-1900)+1,(3-1),(1-1),1,1,1));
xyplot.setDomainAxis(xAxis);

yAxis.setRange(0,200);
xyplot.setRangeAxis(yAxis);

xyplot.setBackgroundPaint(Color.black);

xyplot.getRenderer().setSeriesPaint(0, Color.orange); //set the line in the chart as orange color

chartPanel.setPopupMenu(null);
chartPanel.setSize(new Dimension(377, 285));
chartPanel.setBounds(new Rectangle(0, 0, 655, 280));

jBZoom.setText("Zoom");
jBZoom.setBounds(new Rectangle(220, 320, 73, 27));
jBZoom.setActionCommand("jBZoom");
jBZoom.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
jBZoom_mouseClicked(e);
}
});
jStop.setText("Stop");
jStop.setBounds(new Rectangle(120, 320, 90, 30));
jStop.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
jStop_mouseClicked(e);
}
});
}

Locked