x-axis and y-axis won't start with zero

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Jorne
Posts: 10
Joined: Fri Mar 18, 2016 2:21 pm
antibot: No, of course not.

x-axis and y-axis won't start with zero

Post by Jorne » Fri Mar 18, 2016 2:54 pm

Hi,

First of all thanks for reading my post.

I have an java swing application where I have panel with a chart on it. This chart made from the library JFreeChart displays a dynamic XYLineChart.
My application gets data over a COM port. this data comes in approx. every 20ms.
I save a time stamp for every data that comes in trough the COM port. with this timestamp and the value from the COM port i create a XY line chart.
as you can see in my code I set the setFixedAutoRange(5000) for the y-axis (which are the values from the COM port) and setFixedAutoRange(10000) for the x-axis (which are the timestamps i create).
i set this x-axis with a fixed range because then x-axis is "dynamically" which you can see in the pictures below.

Now my problem is in the start state of the chart the zero's of my y- and x-axis are aligned top and right with negative numbers aligned to the bottom/left.
and I want to have the zero aligned bottom and left in the start state like normal chart do.

How can i set this up without change my "dynamical" x-axis?

Image
as you can see here the x-axis starts with -9000 instead of 0
and the y-axis starts with -4800 instead of 0

Image
in this image you can see what i mean with dynamic axis, they move along with the data.

Image
and on this image are the axis like I want them to be only positive integer.

here is the code of the chart I create (its the sample code from the developer guid but i altered it):

Code: Select all

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class MemoryUsageDemo extends JPanel {
	private static final long serialVersionUID = 1L;

	/** time series for total memory used */
	private XYSeries analogue1;
	XYSeriesCollection dataset;

	/**
	 * creates a new application
	 * 
	 * @param maxAge
	 *            the maximum age (in milliseconds).
	 */
	public MemoryUsageDemo(int maxAge) {
		super(new BorderLayout());

		// create two series that automatically discard data more than 30
		// seconds old..
		this.analogue1 = new XYSeries("Total Memory");
		dataset = new XYSeriesCollection();
		dataset.addSeries(this.analogue1);
		
		//JFreeChart chart = new JFreeChart("JVM Memory Usage", new Font("SansSerif", Font.BOLD, 24), plot, true);
		JFreeChart chart = ChartFactory.createXYLineChart("Analogue", "Time (ms)", "Analogue (mV)", dataset, PlotOrientation.VERTICAL, false, false, false);
		chart.setBackgroundPaint(Color.white);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder(Color.black)));
		
		XYPlot plot = chart.getXYPlot();
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesPaint(0, Color.red);
		renderer.setSeriesStroke(0, new BasicStroke(1.0f));
		
		NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
		yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		yAxis.setFixedAutoRange(5000);
		
		NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
		xAxis.setTickUnit(new NumberTickUnit(1000));
		xAxis.setFixedAutoRange(10000);
		
		plot.setRenderer(renderer);
		add(chartPanel);
	}
	
	/**
	 * Adds an value with time stamp to the data set to draw the graph.
	 * 
	 * @param millis	the current time stamp.
	 * @param value		the value matching the time stamp
	 */
	public void addValuesObservation(double millis, double value){
		this.analogue1.add(millis, value);
	}
Thanks in advance

Locked