JFreeChart + Scrollbar

Discussion about JFreeChart related to stockmarket charts.
Locked
AdamM
Posts: 4
Joined: Tue Nov 18, 2003 9:55 pm

JFreeChart + Scrollbar

Post by AdamM » Tue Nov 18, 2003 10:08 pm

Hi,

I am trying to add a Horizontal Scroll Bar to my Line, High/Low, Candelstick Finencial diagrams. I would like to sroll just the Graphs, the Vertical axis should remain visible on the side(s).

Could someone help me, how to solve this problem with the JFreeChart?

Thanx,

Adam

kingguppy
Posts: 37
Joined: Mon Apr 07, 2003 9:52 pm
Location: Wuppertal, Germany

Post by kingguppy » Wed Nov 19, 2003 3:44 pm

It's not perfect, it's not generic, and it may not work for you, but here's what I'm using for scrolling:

Code: Select all

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

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.event.*;
import org.jfree.chart.plot.*;
import org.jfree.data.*;

public class ChartScrollBar extends JScrollBar implements AdjustmentListener, AxisChangeListener, MouseListener, DatasetChangeListener
{
	private JFreeChart chart;
	private XYPlot plot;
	private double ratio;
	private boolean updating = false;
	
	public ChartScrollBar(int orientation, JFreeChart chart)
	{
		this(orientation, chart, null);
	}
	
	public ChartScrollBar(int orientation, JFreeChart chart, XYPlot plot)
	{
		super(orientation);
		this.chart = chart;
		if (plot == null)
			this.plot = chart.getXYPlot();
		else
			this.plot = plot;
		if (getXYPlot() != null && getValueAxis() != null)
		{
			getValueAxis().addChangeListener(this);
			addAdjustmentListener(this);
			if (getXYPlot().getDataset() != null)
				getXYPlot().getDataset().addChangeListener(this);
			axisUpdate();
			addMouseListener(this);
		}
	}

	public XYPlot getXYPlot()
	{
		return plot;
	}
	
	public ValueAxis getValueAxis()
	{
		if (orientation == VERTICAL)
			return (ValueAxis)getXYPlot().getRangeAxis();
		return (ValueAxis)getXYPlot().getDomainAxis();
	}
	
	public Dataset getDataset()
	{
		return getXYPlot().getDataset();
	}
	
	public Range getDataRange()
	{
		return getXYPlot().getDataRange(getValueAxis());
	}
	
	public double getDataMinimum()
	{
		return getDataRange().getLowerBound();
	}
	
	public double getDataMaximum()
	{
		return getDataRange().getUpperBound();
	}
	
	public double getViewMinimum()
	{
		return getValueAxis().getLowerBound();
	}
	
	public double getViewMaximum()
	{
		return getValueAxis().getUpperBound();
	}
	
	public double getViewLength()
	{
		return getValueAxis().getRange().getLength();
	}
	
	public double getDisplayMaximum()
	{
		return getDataMaximum();//Math.max(getDataMaximum(), getViewMaximum());
	}

	public double getDisplayMinimum()
	{
		return getDataMinimum();//Math.min(getDataMinimum(), getViewMinimum());
	}
	
	private double displayMin;
	private double displayMax;
	private double viewLength;
	static private int STEPS = 100000; // 1000000 could be Integer.MAX_VALUE if you like, but this makes debugging a little easier
	Color oldColor;
	
	public void axisUpdate()
	{
		ValueAxis va = getValueAxis();
		if (va.isAutoRange())
		{
			if (oldColor == null)
				oldColor = getBackground();
			setBackground(oldColor.brighter());
		}
		else if (oldColor != null)
		{
			setBackground(oldColor);
			oldColor = null;
		}
		if (updating)
			return;
		updating = true;
		displayMin = 0;
		displayMax = 0;
		viewLength = 0;
		double viewMin = 0;
		double viewMax = 0;
		ratio = 1;
		Range dataRange = getDataRange();
		if (dataRange != null)
		{
			displayMin = getDisplayMinimum();
			displayMax = getDisplayMaximum();
			viewMin = getViewMinimum();
			viewMax = getViewMaximum();
			//ValueAxis va = getValueAxis();
			if (va instanceof DateAxis)
			{
				Timeline tl = ((DateAxis)va).getTimeline();
				displayMin = tl.toTimelineValue((long)displayMin);
				displayMax = tl.toTimelineValue((long)displayMax);
				viewMin = tl.toTimelineValue((long)viewMin);
				viewMax = tl.toTimelineValue((long)viewMax);
			}
			viewLength = viewMax - viewMin;
			ratio = STEPS / (displayMax - displayMin);
		}
		
		int newMin = 0;
		int newMax = STEPS;
		int newExtent = (int)(viewLength * ratio);
		int newValue;
		if (orientation == VERTICAL)
			newValue = (int)((displayMax - viewMax) * ratio);
		else
			newValue = (int)((viewMin - displayMin) * ratio);
		//System.out.println("ChartScrollBar.axisUpdate(): newValue: " + newValue + " newExtent: " + newExtent + " newMin: " + newMin + " newMax: " + newMax);
		setValues(newValue, newExtent, newMin, newMax);
		updating = false;
	}
	
	public void axisChanged(AxisChangeEvent event)
	{
		//System.out.println("ChartScrollBar.axisChanged()");
		axisUpdate();
	}

	public void datasetChanged(DatasetChangeEvent event)
	{
		//System.out.println("ChartScrollBar.datasetChanged()");
		axisUpdate();
	}

	public void adjustmentValueChanged(AdjustmentEvent e)
	{
		if (updating)
			return;
		updating = true;
		double start, end;
		if (orientation == VERTICAL)
		{
			end = displayMax - (getValue() / ratio);
			start = end - viewLength;
		}
		else
		{
			start = getValue() / ratio + displayMin;
			end = start + viewLength;
		}
		
		if (end > start)
		{
			ValueAxis va = getValueAxis();
			if (va instanceof DateAxis)
			{
				Timeline tl = ((DateAxis)va).getTimeline();
				start = tl.toMillisecond((long)start);
				end = tl.toMillisecond((long)end);
				//System.out.println("********** converting start=" + new java.util.Date((long)start) + " end=" + new java.util.Date((long)end) + " **********");
			}
			getValueAxis().setRange(start, end);
		}
		updating = false;
	}
	
	public void zoomFull()
	{
		getValueAxis().setAutoRange(true);
		//getValueAxis().autoAdjustRange();
	}
	
	public void mouseClicked(MouseEvent e)
	{
		if (e.getButton() == MouseEvent.BUTTON3)
		{
			zoomFull();
		}
	}

	public void mouseEntered(MouseEvent e)
	{
	}

	public void mouseExited(MouseEvent e)
	{
	}

	public void mousePressed(MouseEvent e)
	{
	}

	public void mouseReleased(MouseEvent e)
	{
	}
}

TNT

work very well

Post by TNT » Mon Dec 08, 2003 2:14 pm

i found your script very well, good ..

i use it with candelstick chart .. and a lot of values ..

My problem, I need to change both values axis ( domain axis, and the range axis )

Have you any solution ??

thanks

s.manchikalapudi
Posts: 11
Joined: Sun Feb 06, 2005 2:05 pm
Location: Hyderabad, India
Contact:

response

Post by s.manchikalapudi » Fri Mar 11, 2005 1:26 pm

SAMBASIVARAO MANCHIKALAPUDI

Ramesh1980

Post by Ramesh1980 » Wed Mar 30, 2005 6:30 pm

hello,
Can any one tell me how can i use the above code ?
can any one give me a class with main method ?

Pls help me
Regards,
Ramesh

ratna

Post by ratna » Fri Sep 23, 2005 3:11 pm

I have tried, but i can't see the scroll bar, i just see my CandleStick and my moving average:(.

JFreeChart jfreechart1 = new JFreeChart("TES",JFreeChart.DEFAULT_TITLE_FONT, plot, true );
ChartScrollBar chart1=new ChartScrollBar(0,jfreechart1);


What's wrong.Anybody know?.Thank u

Wolfenberg
Posts: 1
Joined: Tue Aug 22, 2006 4:15 pm

Post by Wolfenberg » Tue Aug 22, 2006 4:18 pm

The suggested scrollbar works fine. Thanks a lot!

Any idea on how to automatically adjust the size of the window according to the shown part of the chart while scrolling it? I mean, like in most software for tech.analysis?

Thanks in advance!

string
Posts: 2
Joined: Mon Dec 31, 2007 5:02 pm

Check the demo source....

Post by string » Mon Oct 27, 2008 3:40 am

...of CompassFormatDemo2.java.

It shows a great example of using a slider to change the range axis.

Should be easy to apply the same principal to the domain axis.

timeseries
Posts: 1
Joined: Thu Oct 30, 2008 7:03 pm

Scrollable Time Series Chart

Post by timeseries » Thu Oct 30, 2008 7:05 pm

I have a time series chart that I would like to add a scroll bar to.

I have tried the example given with no success.

Since data is continually being added to the Time Series chart
over time, I would like to scroll back in time to see what the values
are.

Is this possible?

Locked