Custom x axis tick labels

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sxyz
Posts: 6
Joined: Sun Jul 17, 2016 11:56 pm
antibot: No, of course not.

Custom x axis tick labels

Post by sxyz » Wed Jul 20, 2016 3:15 pm

Hi,

I am trying to understand how (and which class I could use) to customize x axis tick labels on jfreechart plot from String object.

The data to be plotted is retrieved from a file, for which each line have a time stamp (h:m:s) as I show in the following example:

Code: Select all

10:06:55	00000087454AFF39725137000D008A2B4AFF8FC2
10:07:00	00000087D94AFF38A25570244D008A104AFF8FC2
10:07:05	00000087D94AFF38A25570244D008A104AFF8FC2
10:07:10	000000871E4AFF391E301E2B4D008A474AFF8FC2
and I would set the x ticks labels as 10:06:55, 10:07:00 etc.
How I could customize the x tick labels as the time stamps from each line ?

Thank you.
sxyz

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Custom x axis tick labels

Post by paradoxoff » Wed Jul 20, 2016 3:51 pm

What type of plot are you using?

sxyz
Posts: 6
Joined: Sun Jul 17, 2016 11:56 pm
antibot: No, of course not.

Re: Custom x axis tick labels

Post by sxyz » Wed Jul 20, 2016 4:02 pm

Hi,

Right now I am using (after some test) XYPlot with XYSeries.

I am testing TimeSeries but seems it does not provide any facility for setting tick label as I want.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Custom x axis tick labels

Post by paradoxoff » Wed Jul 20, 2016 4:33 pm

If you use an XYPlot, all you need to do is use a DateAxis with a suitable DateFormat such as new SimpleDateFormat("HH:mm:ss");
First you will have to parse the "10:06:55" Strings to Numbers.

sxyz
Posts: 6
Joined: Sun Jul 17, 2016 11:56 pm
antibot: No, of course not.

Re: Custom x axis tick labels

Post by sxyz » Thu Jul 21, 2016 12:06 am

Solved with the Second class. Here is an example:

Code: Select all

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

import javax.swing.*;
import javax.swing.table.*;

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

import org.jfree.chart.*;
import org.jfree.chart.block.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.labels.*;
import org.jfree.chart.servlet.*;
import org.jfree.chart.annotations.*;
import org.jfree.chart.needle.*;
import org.jfree.chart.event.*;
import org.jfree.chart.imagemap.*;
import org.jfree.chart.encoders.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.title.*;
import org.jfree.chart.util.*;
import org.jfree.chart.resources.*;
import org.jfree.chart.panel.*;
import org.jfree.chart.editor.*;
import org.jfree.chart.demo.*;
import org.jfree.data.*;
import org.jfree.data.time.*;

import org.jfree.chart.plot.*;
import org.jfree.data.xy.*;

import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class SecondDemo extends ApplicationFrame
{
	public SecondDemo(final String title)
   {
   	super(title);

   	NumberAxis xAxis = new NumberAxis("time");
   	XYItemRenderer renderer;
   	TimeSeriesCollection tSeriesDataSet;
		JFreeChart chart;
		TimeSeries series;
		

		final XYPlot xyplot;
		tSeriesDataSet = new TimeSeriesCollection();
   	series = new TimeSeries(title);

    	// Second(int second, int minute, int hour, int day, int month, int year))
    	series.add(new Second(0, 1, 10, 4, 6, 2016), 1);
      series.add(new Second(1, 1, 10, 4, 6, 2016), 1.1);
      series.add(new Second(2, 1, 10, 4, 6, 2016), null);
      series.add(new Second(3, 1, 10, 4, 6, 2016), 1.3);
      series.add(new Second(4, 1, 10, 4, 6, 2016), 1.4);
      series.add(new Second(5, 1, 10, 4, 6, 2016), 1.5);
      series.add(new Second(6, 1, 10, 4, 6, 2016), 1.6);
      series.add(new Second(7, 1, 10, 4, 6, 2016), 1.7);
      series.add(new Second(8, 1, 10, 4, 6, 2016), 1.8);

		tSeriesDataSet.addSeries(series);

   	chart = ChartFactory.createXYLineChart("Plot view", "A", "Time ", tSeriesDataSet, PlotOrientation.VERTICAL, true, true, false);
		
		xyplot = (XYPlot) chart.getXYPlot();

		xyplot.setBackgroundPaint(new Color(211,210,235));
		xyplot.setRangeGridlinePaint(Color.black);
		xyplot.setDomainCrosshairVisible(true);
		xyplot.setRangeCrosshairVisible(true);

		renderer = xyplot.getRenderer();
		renderer.setSeriesPaint(0, Color.green);
		renderer.setSeriesPaint(1, Color.blue);
		renderer.setSeriesPaint(2, Color.red);

		xyplot.setDomainAxis(new DateAxis());
		DateAxis axis = (DateAxis) xyplot.getDomainAxis();
		axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));

		/*range = (NumberAxis) xyplot.getRangeAxis();
		range.setRange(-1.2, 1.2);
		range.setTickUnit(new NumberTickUnit(0.1));*/

		final ChartPanel chartPanel = new ChartPanel(chart);
      setContentPane(chartPanel);
	}

	public static void main(String args[])
	{
	   SecondDemo secondDemo = new SecondDemo("Second Demo");
	   secondDemo.pack();
      secondDemo.setVisible(true);
	}
}
Of course the arguments of the Second class will be from the file, for which I can retrieve the year, month and day from the filename.

Locked