How do I use HorizontalDateAxis?

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

How do I use HorizontalDateAxis?

Post by Jason » Sat Oct 07, 2000 12:32 am

Hi,

I'm trying to customize Horizontal Axis by using HorizontalDateAxis
but I'm not having any luck. Can some tell me what I'm doing wrong?

Thanks.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


XYDataSource xyData = createXYDataSource();
JFreeChart myChart = JFreeChart.createTimeSeriesChart(xyData);
StandardTitle title = (StandardTitle)myChart.getTitle();
title.setTitle("ARTSAC II");
myChart.setChartBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
myChart.setAntiAlias(false);

Plot myPlot = myChart.getPlot();
myPlot.setInsets(new Insets(0, 0, 0, 3)); //???????????

Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Seconds");

Axis myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("Hours of Day");

HorizontalDateAxis myHDAxis = new HorizontalDateAxis(myPlot,
"What the hell!",
myHorizontalAxis.getLabelFont(),
myHorizontalAxis.getLabelPaint(),
myHorizontalAxis.getLabelInsets(),
true,
myHorizontalAxis.getTickLabelFont(),
myHorizontalAxis.getTickLabelPaint(),
myHorizontalAxis.getTickLabelInsets(),
true,
true,
myHorizontalAxis.getTickMarkStroke(),
true,
createDate( 0, 0, 18),
createDate(23, 19, 39),
true,
new DateUnit(Calendar.HOUR, 1),
new SimpleDateFormat("K:mm a"),
true,
myHorizontalAxis.getTickMarkStroke(),
myHorizontalAxis.getTickLabelPaint());
//java.awt.Stroke gridStroke,
//java.awt.Paint gridPaint);

//myPlot.axisChanged(new AxisChangeEvent(myHorizontalAxis));

frame = new JFrame();
frame.addNotify();

Image image = frame.createImage(700, 500);
if (image == null)
System.out.println("null image");

g = image.getGraphics();

myChart.draw((Graphics2D) g, new Rectangle(700, 500));

output = new FileOutputStream ("./chart.gif");

GifEncoder encoder = new GifEncoder(image, output, true);
encoder.encode();

Private Heath

RE: How do I use HorizontalDateAxis?

Post by Private Heath » Sat Oct 07, 2000 12:48 am

Jason,
It is like we are trying to figure this stuff out at the same time.. I as well am having trouble. Your code is very similar to mine, but no progress. Hopefully someone who is further down the progress line can help out.

Cheers
Private Heath

Jason

RE: How do I use HorizontalDateAxis?

Post by Jason » Mon Oct 09, 2000 11:13 pm

From my original code above, I've added myPlot.setHorizontalAxis(myHorizontalDateAxis);
to make HorizontalDateAxis() work.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Axis myHorizontalDateAxis = new HorizontalDateAxis(myPlot,
"What the hell!",
myHorizontalAxis.getLabelFont(),
myHorizontalAxis.getLabelPaint(),
myHorizontalAxis.getLabelInsets(),
true,
myHorizontalAxis.getTickLabelFont(),
myHorizontalAxis.getTickLabelPaint(),
myHorizontalAxis.getTickLabelInsets(),
true,
true,
myHorizontalAxis.getTickMarkStroke(),
true,
createDate( 0, 0, 18),
createDate(23, 19, 39),
true,
new DateUnit(Calendar.HOUR, 4),
new SimpleDateFormat("K:mm a"),
true,
myHorizontalAxis.getTickMarkStroke(),
myHorizontalAxis.getTickLabelPaint());
//java.awt.Stroke gridStroke,
//java.awt.Paint gridPaint);

myPlot.setHorizontalAxis(myHorizontalDateAxis);

Jason

RE: How do I use HorizontalDateAxis?

Post by Jason » Tue Oct 10, 2000 1:53 am

Here is the complete program using HorizontalDateAxis that I got it to work now.


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import java.io.*;
import java.text.*;
import java.util.*;
import java.math.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;

import Acme.JPM.Encoders.GifEncoder;

import com.jrefinery.chart.*;
import com.jrefinery.chart.event.*;
import com.jrefinery.chart.ui.*;
import com.jrefinery.util.ui.*;


class A2chart
{
public static void main(String[] args)
{
FileOutputStream output = null;
try
{
XYDataSource xyData = createXYDataSource();
JFreeChart myChart = JFreeChart.createTimeSeriesChart(xyData);
StandardTitle title = (StandardTitle)myChart.getTitle();
title.setTitle("ARTSAC II");
myChart.setChartBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
myChart.setAntiAlias(false);

Plot myPlot = myChart.getPlot();

Axis myVerticalAxis = myPlot.getAxis(Plot.VERTICAL_AXIS);
myVerticalAxis.setLabel("Seconds");

Axis myHorizontalAxis = myPlot.getAxis(Plot.HORIZONTAL_AXIS);
myHorizontalAxis.setLabel("Hours of Day");

Stroke gridStroke = new BasicStroke(0.25f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0.0f, new float[] {2.0f, 2.0f}, 0.0f);
Paint gridPaint = Color.gray;

Axis myHorizontalDateAxis =
new HorizontalDateAxis(myPlot,
myHorizontalAxis.getLabel(),
myHorizontalAxis.getLabelFont(),
myHorizontalAxis.getLabelPaint(),
myHorizontalAxis.getLabelInsets(),
true,
myHorizontalAxis.getTickLabelFont(),
myHorizontalAxis.getTickLabelPaint(),
myHorizontalAxis.getTickLabelInsets(),
true,
true,
myHorizontalAxis.getTickMarkStroke(),
true,
createDate( 0, 0, 0),
createDate(24, 0, 0),
false,
new DateUnit(Calendar.HOUR_OF_DAY, 1),
new SimpleDateFormat("K:mm a"),
true,
gridStroke,
gridPaint);

myPlot.setHorizontalAxis(myHorizontalDateAxis);
myPlot.setInsets(new Insets(0, 0, 0, 3)); //???????????

frame = new JFrame();
frame.addNotify();

Image image = frame.createImage(700, 500);
if (image == null)
System.out.println("null image");

g = image.getGraphics();

myChart.draw((Graphics2D) g, new Rectangle(700, 500));

output = new FileOutputStream ("./chart.gif");

GifEncoder encoder = new GifEncoder(image, output, true);
encoder.encode();
}
catch (AxisNotCompatibleException err1)
{
System.out.println("AxisNotCompatibleException error!");
}
catch (IOException err3)
{
}
finally
{
if (g != null)
g.dispose();
if (frame != null)
frame.removeNotify();
try
{
output.flush();
output.close();
}
catch (IOException err)
{
}
}

System.exit(0);
} // public void main()

public static Date createDate(int hour, int min, int sec)
{
GregorianCalendar calendar = new GregorianCalendar(2000, 9, 3, hour, min, sec);
return calendar.getTime();
} // private Date createDate(int year, int month, int day)

public static XYDataSource createXYDataSource()
{
Object[][][] data = new Object[][][] { {

{ createDate( 0, 0, 18), new Double( 0.1) },
{ createDate( 0, 12, 56), new Double( 0.9) },
{ createDate( 0, 18, 34), new Double( 1.8) },
{ createDate( 0, 55, 52), new Double( 2.1) },
{ createDate( 1, 1, 40), new Double( 0.3) },
{ createDate( 1, 1, 49), new Double( 1.2) },
{ createDate( 2, 1, 9), new Double(13.1) },
{ createDate( 2, 1, 20), new Double(10.4) },
{ createDate( 3, 1, 39), new Double( 3.8) },
{ createDate( 4, 1, 11), new Double( 8.1) },
{ createDate( 4, 31, 48), new Double( 1.9) },
{ createDate( 5, 10, 01), new Double( 2.9) },
{ createDate( 5, 20, 11), new Double( 2.0) },
{ createDate( 5, 50, 01), new Double( 3.9) },
{ createDate( 6, 18, 44), new Double( 0.2) },
{ createDate( 6, 38, 44), new Double( 0.6) },
{ createDate( 7, 38, 44), new Double( 0.8) },
{ createDate( 8, 1, 19), new Double( 0.22) },
{ createDate( 9, 29, 28), new Double( 0.3) },
{ createDate(10, 43, 8), new Double( 2.8) },
{ createDate(13, 31, 34), new Double( 3.3) },
{ createDate(17, 52, 42), new Double( 4.9) },
{ createDate(18, 9, 22), new Double( 1.11) },
{ createDate(19, 21, 13), new Double( 9.8) },
{ createDate(22, 1, 19), new Double( 2.4) },
{ createDate(23, 19, 39), new Double( 0.7) } } };

return new DefaultXYDataSource(new String[] { "Max" }, data);
} // public XYDataSource createXYDataSource()

private static JFrame frame = null;
private static Graphics g = null;
} // public class A2chart

Locked