Home Chartgraphs Candle Stick Chart Example using JFreeChart
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Candle Stick Chart Example using JFreeChart

Advertisement
This Example shows you how to create a candle stick chart using JFreeChart. Code of the chart given below shows price value on the different dates.

Candle Stick Chart Example using JFreeChart

     

This Example shows you how to create a candle stick chart using JFreeChart. Code of the chart given below shows price value on the different dates.

In the example given below we have extended class ApplicationFrame to create a frame and also pass a string value to the constructor of ApplicationFrame class by using super keyword that will be name of the created frame.

The method used in this example are described below:

pack(): This method invokes the layout manager.

centerFrameOnScreen(): This method is used for the position of the frame in the middle of the screen.

setVisible(): This method is used for display frame on the screen.

DefaultHighLowDataset(): DefaultHighLowDataset class object takes series Key, array of date type, double type array of high values, double type array of low values, double type array of open values, double type array of close values, double type array of volume values

createBoxAndWhiskerChart(): This method is used to create box and whisker chart for given values. It takes title, domain axis label, range axis label, dataset and legend as parameters.

setBackgroundPaint(): This method is used to set the paint used to fill the chart background.

BoxAndWhiskerChart.java

import java.util.Calendar;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class CandleStickChart extends ApplicationFrame {

  public CandleStickChart(String titel) {
  super(titel);

  final DefaultHighLowDataset dataset = createDataset();
  final JFreeChart chart = createChart(dataset);
  final ChartPanel chartPanel = new ChartPanel(chart);
  chartPanel.setPreferredSize(new java.awt.Dimension(600350));
  setContentPane(chartPanel);
  }

  private DefaultHighLowDataset createDataset() {

  int serice = 15;

  Date[] date = new Date[serice];
  double[] high = new double[serice];
  double[] low = new double[serice];
  double[] open = new double[serice];
  double[] close = new double[serice];
  double[] volume = new double[serice];



  Calendar calendar = Calendar.getInstance();
  calendar.set(200851);

 for (int i = 0; i < serice; i++) {
  date[i= createData(20088, i + 1);
  high[i30 + Math.round(10new Double(Math.random() 20.0);
  low[i30 + Math.round(10new Double(Math.random() 20.0);
  open[i10 + Math.round(10new Double(Math.random() 20.0);
    close[i10 + Math.round(10new Double(Math.random() 20.0);
  volume[i10.0 new Double(Math.random() 20.0);
  }

  DefaultHighLowDataset data = new DefaultHighLowDataset(
  "", date, high, low, open, close, volume);
  return data;
  }

  private Date createData(int year, int month, int date) {
  Calendar calendar = Calendar.getInstance();
  calendar.set(year, month - 1, date);
  return calendar.getTime();
  }

  private JFreeChart createChart(final 
 
DefaultHighLowDataset dataset) {
  final JFreeChart chart = ChartFactory.createCandlestickChart(
  "Candlestick Demo""Time""Price", dataset, false);
  return chart;
  }

  public static void main(String args[]) {
 CandleStickChart chart = new CandleStickChart("Candle Stick Chart");
  chart.pack();
  RefineryUtilities.centerFrameOnScreen(chart);
  chart.setVisible(true);
  }
}

Output:



Download code
Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 1, 2008

Ask Questions?    Discuss: Candle Stick Chart Example using JFreeChart  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
forex candlesticks
April 18, 2011
forex candlesticks

Great website excellent content.
Pradeep Bansal
June 5, 2013
Dynamic Candle stick chart

How to convert this example for dynamic candlestick chart where data will be adding to it every 1 sec. Problem is ChartFactory.createCandlestickChart uses OHLCDataset which doesnot have any add method to add new items which needs to be added for dynamic updates. so what should I do
Aman
February 24, 2012
CandleStick chart

How can we give the range in y axis. i.e if I want my y axis of candlestick to start with different value other than 0. Can we give range in y axis which will be displayed in the graph.
dpkdn
March 21, 2012
candlestick jfreechart

I want to run this code as applet.... pls help
ash
January 3, 2013
Candle Stick Chart Example using JFreeChart

which portion of the code shall i place the connection to database and which portion shall i eliminate my pproject is a web application and i'm doing it in jsp,so how can i make the graph plot within the browser
DMCA.com