Combine Candlestick and Line Charts

Discussion about JFreeChart related to stockmarket charts.
Locked
Sianis
Posts: 4
Joined: Tue Aug 11, 2009 11:35 am
antibot: No, of course not.

Combine Candlestick and Line Charts

Post by Sianis » Tue Aug 11, 2009 11:43 am

Hi!

Is it possible to combine Candlestick and Line Charts in same chart? I saw that Candlestick is type XY as Line too, but they have different renderer.

Thank you for your help!

Sianis
Posts: 4
Joined: Tue Aug 11, 2009 11:35 am
antibot: No, of course not.

Re: Combine Candlestick and Line Charts

Post by Sianis » Tue Aug 11, 2009 2:02 pm

Okey, I solved it.

Key is:
XYPlot.setDataset
XYPlot.setRenderer

ravi.network
Posts: 1
Joined: Thu Sep 24, 2009 5:39 pm
antibot: No, of course not.

Re: Combine Candlestick and Line Charts

Post by ravi.network » Thu Sep 24, 2009 6:01 pm

Hello Sianis,

I am facing problem in drawing line charts on candle sticks, could you please post the example.
It will be great help.

Thanks,
Ravi

abhay.asingh
Posts: 58
Joined: Mon Dec 15, 2008 7:07 am
Location: India
Contact:

Re: Combine Candlestick and Line Charts

Post by abhay.asingh » Mon Dec 28, 2009 6:29 am

hi
here is example for that

Code: Select all

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.xy.*;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.text.*;
import java.util.*;
import java.util.List;

public class CandlestickDemo extends JFrame {
        public CandlestickDemo(String stockSymbol) {
                super("CandlestickDemo");
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                DateAxis    domainAxis       = new DateAxis("Date");
                NumberAxis  rangeAxis        = new NumberAxis("Price");
                CandlestickRenderer renderer = new CandlestickRenderer();
                XYDataset   dataset          = getDataSet(stockSymbol);

                XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

                //Do some setting up, see the API Doc
                renderer.setSeriesPaint(0, Color.BLACK);
                renderer.setDrawVolume(false);
                rangeAxis.setAutoRangeIncludesZero(false);
                domainAxis.setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() );

                //Now create the chart and chart panel
                JFreeChart chart = new JFreeChart(stockSymbol, null, mainPlot, false);
                ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(new Dimension(600, 300));

                this.add(chartPanel);
                this.pack();
        }
        protected AbstractXYDataset getDataSet(String stockSymbol) {
                //This is the dataset we are going to create
                DefaultOHLCDataset result = null;
                //This is the data needed for the dataset
                OHLCDataItem[] data;

                //This is where we go get the data, replace with your own data source
                data = getData(stockSymbol);

                //Create a dataset, an Open, High, Low, Close dataset
                result = new DefaultOHLCDataset(stockSymbol, data);

                return result;
        }
        //This method uses yahoo finance to get the OHLC data
        protected OHLCDataItem[] getData(String stockSymbol) {
                List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
                try {
                        String strUrl= "http://ichart.finance.yahoo.com/table.csv?s="+stockSymbol+"&a=0&b=1&c=2008&d=3&e=30&f=2008&ignore=.csv";
                        URL url = new URL(strUrl);
                        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                        DateFormat df = new SimpleDateFormat("y-M-d");

                        String inputLine;
                        in.readLine();
                        while ((inputLine = in.readLine()) != null) {
                                StringTokenizer st = new StringTokenizer(inputLine, ",");

                                Date date       = df.parse( st.nextToken() );
                                double open     = Double.parseDouble( st.nextToken() );
                                double high     = Double.parseDouble( st.nextToken() );
                                double low      = Double.parseDouble( st.nextToken() );
                                double close    = Double.parseDouble( st.nextToken() );
                                double volume   = Double.parseDouble( st.nextToken() );
                                double adjClose = Double.parseDouble( st.nextToken() );

                                OHLCDataItem item = new OHLCDataItem(date, open, high, low, close, volume);
                                dataItems.add(item);
                        }
                        in.close();
                }
                catch (Exception e) {
                        e.printStackTrace();
                }
                //Data from Yahoo is from newest to oldest. Reverse so it is oldest to newest
                Collections.reverse(dataItems);

                //Convert the list into an array
                OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]);

                return data;
        }

        public static void main(String[] args) {
                new CandlestickDemo("MSFT").setVisible(true);
        }
}

Abhay Singh

theAnonymous
Posts: 11
Joined: Tue Feb 22, 2011 8:15 pm
antibot: No, of course not.

Re: Combine Candlestick and Line Charts

Post by theAnonymous » Mon Apr 11, 2011 6:20 pm

it is not working. when i ran the code, it only showed a JFrame, but no charts.

a System.out.println() told me that the whole code ran successfully.

itsmaddy
Posts: 10
Joined: Wed Dec 15, 2010 1:05 pm
antibot: No, of course not.

Re: Combine Candlestick and Line Charts

Post by itsmaddy » Tue Apr 19, 2011 3:04 pm

the below example will show u to combine Candlestick and Line Charts in same chart
and also to change the renderer of the series plus how you can update the timeseries

Code: Select all


import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Minute;
import org.jfree.data.time.Month;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.xy.XYDataset;


/**
 * Example to combine Candlestick and Line Charts in same chart
 * and also convert candlestick to line.
 * plus to update the timeseries.
 * 
 * @author Maneesh Nanu
 *
 */
public class OHLC extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;

	private OHLCDataset dataset;
	public static OHLCSeries ohlcSeries;
	private CandlestickRenderer CandleStickRenderer;
	private DateAxis domainAxis;
	private NumberAxis rangeAxis;
	public static XYPlot plot;
	private JFreeChart jfreechart;
	public ChartPanel chartPanel;
	private JPanel panel;
	private XYLineAndShapeRenderer LineRenderer;
	private HighLowRenderer OHLCRenderer;
	protected static int datasetcnt = 1;
	private JButton btnRen;
	private JButton btnAvg;

	public static ArrayList<Month> Date = new ArrayList<Month>();
	public static ArrayList<Double> Close = new ArrayList<Double>();
	public static ArrayList<Double> Open = new ArrayList<Double>();
	public static ArrayList<Double> High = new ArrayList<Double>();
	public static ArrayList<Double> Low = new ArrayList<Double>();

	public OHLC()
	{
		setBackground(Color.WHITE);
		getContentPane().setLayout(new BorderLayout(0, 0));
		dataset = createDataset();
		JFreeChart chart = createChart(dataset);
		chartPanel = new ChartPanel(chart);
		chartPanel.setMouseZoomable(true);

		panel = new JPanel(new BorderLayout());
		panel.add(chartPanel, BorderLayout.CENTER);
		getContentPane().add(panel);

		chart.setBackgroundPaint(Color.WHITE);
		chartPanel.setBackground(Color.WHITE);
		panel.setBackground(Color.WHITE);

		btnRen = new JButton("renderer");
		panel.add(btnRen, BorderLayout.NORTH);
		btnRen.addActionListener(this);

		btnAvg = new JButton("Average");
		panel.add(btnAvg, BorderLayout.SOUTH);
		btnAvg.addActionListener(this);
		this.setVisible(true);
		this.setSize(400, 400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public OHLCDataset createDataset()
	{

		try
		{
			ohlcSeries = new OHLCSeries("");

			Calendar date[] = new Calendar[Date.size()];

			int i;
			Minute minute;
			Second secondOfDay;
			double open, high, low, close;
			try
			{
				for (i = 0; i<Date.size(); i++)
				{
					date[i] = Calendar.getInstance();
					date[i].setTimeInMillis(Date.get(i).getFirstMillisecond());
					minute = new Minute(date[i].get(Calendar.MINUTE),
							date[i].get(Calendar.HOUR_OF_DAY),
							date[i].get(Calendar.DAY_OF_MONTH),
							date[i].get(Calendar.MONTH) + 1,
							date[i].get(Calendar.YEAR));

					secondOfDay = new Second(date[i].get(Calendar.SECOND),
							minute);
					open = Open.get(i);
					high = High.get(i);
					low = Low.get(i);
					close = Close.get(i);
					System.out
							.println(open + "" + high + "" + low + "" + close);
					ohlcSeries.add(secondOfDay, open, high, low, close);

				}
			} catch (Exception e)
			{
				System.out.println(e.getMessage());
			}
			OHLCSeriesCollection ohlcCollection = new OHLCSeriesCollection();
			ohlcSeries.getItemCount();

			ohlcCollection.addSeries(ohlcSeries);
			return ohlcCollection;
		} catch (Exception e)
		{
			System.out.println("Error: " + e);
		} 
		return null;
	}

	@SuppressWarnings("deprecation")
	private JFreeChart createChart(final OHLCDataset dataset)
	{
		CandleStickRenderer = new CandlestickRenderer();
		CandleStickRenderer.setSeriesStroke(0, new BasicStroke(1.0f,
				BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
		CandleStickRenderer.setSeriesPaint(0, Color.black);

		LineRenderer = new XYLineAndShapeRenderer(true, false);
		LineRenderer.setStroke(new BasicStroke(1f, BasicStroke.CAP_BUTT,
				BasicStroke.JOIN_BEVEL));
		LineRenderer.setSeriesPaint(0, Color.BLACK);
		LineRenderer.setSeriesPaint(1, Color.BLUE);
		LineRenderer.setSeriesPaint(2, Color.RED);

		OHLCRenderer = new HighLowRenderer();
		OHLCRenderer.setSeriesStroke(0, new BasicStroke(1.0f,
				BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
		OHLCRenderer.setSeriesPaint(0, Color.black);

		domainAxis = new DateAxis();
		domainAxis.setAutoRange(true);
		domainAxis.setTickLabelsVisible(true);
		domainAxis.setAutoTickUnitSelection(true);

		rangeAxis = new NumberAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
//		rangeAxis.setFixedAutoRange(50.0);
		rangeAxis.setAutoRange(true);

		plot = new XYPlot(dataset, domainAxis, rangeAxis, LineRenderer);
		plot.setBackgroundPaint(Color.LIGHT_GRAY);
		plot.setDomainGridlinePaint(Color.WHITE);
		plot.setRangeGridlinePaint(Color.WHITE);
		plot.setDomainGridlinesVisible(true);
		plot.setRangeGridlinesVisible(true);

		jfreechart = new JFreeChart("Stock Exchange price (IBM)", new Font(
				"SansSerif", Font.BOLD, 24), plot, false);
		return jfreechart;
	}

	public static void main(String[] args)
	{
		TAFunction();
		new OHLC();
	}
	
	public void generateDate()
	{
		try{
		s1.add(new Month(7, 2002), 132.8);
		}
		catch(Exception e){}
	}

	@Override
	public void actionPerformed(ActionEvent ae)
	{
		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		if (ae.getSource() == btnAvg)
		{
			XYDataset collec = createMADataset();
			// XYDataset dset = new TimeSeriesCollection();
			xyplot.setDataset(2, collec);
			xyplot.setRenderer(2, LineRenderer);

		} else if (ae.getSource() == btnRen)
		{
			xyplot.setRenderer(CandleStickRenderer);
			try{
				s1.addOrUpdate(new Month(7, 2002), a=a+10);
				}
				catch(Exception e){
//					e.printStackTrace();
					System.out.println(e.getMessage());
				}
		}
	}
	static int a = 143;
	TimeSeries s1;
	@SuppressWarnings("deprecation")
	private TimeSeriesCollection createMADataset()
	{
		s1 = new TimeSeries("Budget", "Year", "$ Million",
				Month.class);
		try
		{
			s1.add(new Month(2, 2001), 181.8);
			s1.add(new Month(3, 2001), 167.3);
			s1.add(new Month(4, 2001), 153.8);
			s1.add(new Month(5, 2001), 167.6);
			s1.add(new Month(6, 2001), 158.8);
			s1.add(new Month(7, 2001), 148.3);
			s1.add(new Month(8, 2001), 153.9);
			s1.add(new Month(9, 2001), 142.7);
			s1.add(new Month(10, 2001), 123.2);
			s1.add(new Month(11, 2001), 131.8);
			s1.add(new Month(12, 2001), 139.6);
			s1.add(new Month(1, 2002), 142.9);
			s1.add(new Month(2, 2002), 138.7);
			s1.add(new Month(3, 2002), 137.3);
			s1.add(new Month(4, 2002), 143.9);
			s1.add(new Month(5, 2002), 139.8);
			s1.add(new Month(6, 2002), 137.0);
			s1.add(new Month(7, 2002), 132.8);
		} catch (Exception exception)
		{
			System.err.println(exception.getMessage());
		}
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(s1);
		return timeseriescollection;
	}

	public static void TAFunction()
	{
		

		Date.add(new Month(2, 2001));
		Date.add(new Month(3, 2001));
		Date.add(new Month(4, 2001));
		Date.add(new Month(5, 2001));
		Date.add(new Month(6, 2001));
		Date.add(new Month(7, 2001));
		Date.add(new Month(8, 2001));
		Date.add(new Month(9, 2001));
		Date.add(new Month(10, 2001));
		Date.add(new Month(11, 2001));
		Date.add(new Month(12, 2001));
		Date.add(new Month(1, 2002));
		Date.add(new Month(2, 2002));
		Date.add(new Month(3, 2002));
		Date.add(new Month(4, 2002));
		Date.add(new Month(5, 2002));
		Date.add(new Month(6, 2002));
		Date.add(new Month(7, 2002));
		Date.add(new Month(8, 2002));
		Date.add(new Month(9, 2002));
		
		
		Close.add(5.0);
		Close.add(40.0);
		Close.add(52.0);
		Close.add(13.0);
		Close.add(13.0);
		Close.add(14.0);
		Close.add(22.0);
		Close.add(40.0);
		Close.add(30.0);
		Close.add(54.0);
		Close.add(46.0);
		Close.add(41.0);
		Close.add(13.0);
		Close.add(57.0);
		Close.add(74.0);
		Close.add(29.0);
		Close.add(34.0);
		Close.add(46.0);
		Close.add(45.0);
		Close.add(73.0);
		Close.add(64.0);
		Close.add(92.0);

		Open.add(10.0);
		Open.add(50.0);
		Open.add(62.0);
		Open.add(23.0);
		Open.add(23.0);
		Open.add(5.0);
		Open.add(32.0);
		Open.add(45.0);
		Open.add(32.0);
		Open.add(45.0);
		Open.add(67.0);
		Open.add(43.0);
		Open.add(23.0);
		Open.add(67.0);
		Open.add(84.0);
		Open.add(23.0);
		Open.add(34.0);
		Open.add(76.0);
		Open.add(45.0);
		Open.add(73.0);
		Open.add(67.0);
		Open.add(96.0);

		High.add(20.0);
		High.add(60.0);
		High.add(72.0);
		High.add(33.0);
		High.add(33.0);
		High.add(15.0);
		High.add(22.0);
		High.add(55.0);
		High.add(42.0);
		High.add(55.0);
		High.add(77.0);
		High.add(53.0);
		High.add(33.0);
		High.add(77.0);
		High.add(94.0);
		High.add(33.0);
		High.add(44.0);
		High.add(86.0);
		High.add(55.0);
		High.add(83.0);
		High.add(77.0);
		High.add(106.0);

		Low.add(10.0);
		Low.add(50.0);
		Low.add(62.0);
		Low.add(23.0);
		Low.add(23.0);
		Low.add(5.0);
		Low.add(32.0);
		Low.add(45.0);
		Low.add(32.0);
		Low.add(45.0);
		Low.add(67.0);
		Low.add(43.0);
		Low.add(23.0);
		Low.add(67.0);
		Low.add(84.0);
		Low.add(23.0);
		Low.add(34.0);
		Low.add(76.0);
		Low.add(45.0);
		Low.add(73.0);
		Low.add(67.0);
		Low.add(96.0);
	}
}

itsmaddy
Posts: 10
Joined: Wed Dec 15, 2010 1:05 pm
antibot: No, of course not.

Re: Combine Candlestick and Line Charts

Post by itsmaddy » Mon Apr 25, 2011 7:00 am

The exception that usually comes is "timeseries cannot be converted to ohlcseries".
to overcome this you have to set renderer for that series as XYLineAndShapeRenderer.
XYDataset collec = createMADataset();
xyplot.setDataset(2, collec);
xyplot.setRenderer(2, new XYLineAndShapeRenderer());
The above program shows how to add a timeseries on candlestick chart.
Try running the above program .It has the code that you require.

Locked