Candle Stick Chart Example using JFreeChart

Ads
 

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.

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

Ads

Ads