JFreeChart: CandlestickChart example

From time to time I use JFreeChart library. This is a CandlestickChart sample to copy paste from here in future.

Maven dependencies in pom.xml

<dependency>
   <groupId>jfree</groupId>
   <artifactId>jcommon</artifactId>
   <version>1.0.16</version>
</dependency>
<dependency>
   <groupId>jfree</groupId>
   <artifactId>jfreechart</artifactId>
   <version>1.0.13</version>
</dependency>
 

 

CandlestickChartSample.java

 

package JavaApplication1;

import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
 
/**
jfreechart sample with OHLC candlestick data
@author Dmitry
*/
public class CandlestickChartSample extends JPanel {
 
  /**

   * This application entry point.
   * @param args
   */
  
public static void main(final String[] args) {

      // Create and run chart in JFrame
      new Runnable(){
         @Override
         public void run() {
            // Create new chart
            CandlestickChartSample chart = new CandlestickChartSample();
            chart.setVisible(true);
            // Create and run jframe to host our chart
            JFrame frame = new JFrame();
            frame.setBounds(100, 100, 676, 449);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(chart);
            frame.setVisible(true);
         }
       }.run();
   }
   
   /**
   * OHLC data for the chart. Stored in class field to add/remove something in
   * future
   */
   private OHLCSeries ohlcSeries;
   
   
/**
   * Constructor
   @param title
   */
   public CandlestickChartSample() {
      // Create OHLC series for the chart
      OHLCSeriesCollection seriesCollection = createSeriesCollection();
       // Store to variable for being accessible from other methods 
       // in future
      ohlcSeries = seriesCollection.getSeries(0);
       
       // Create chart itself
      final JFreeChart chart = createChart(seriesCollection);
      chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);
      final ChartPanel chartPanel = new ChartPanel(chart);
      this.add(chartPanel); // Fill chart with test data
      createTestData(seriesCollection);
   }
 
   /**
   * Create a chart.
   @param dataset the dataset.
   @return created chart.
   */
   private JFreeChart createChart(final OHLCSeriesCollection dataset) {
      final JFreeChart chart = ChartFactory.createCandlestickChart(
         "Candlestick Demo""Time""Value", dataset, true);
      return chart;
   }
 
   /**
   * Create OHLC data for chart
   */
   OHLCSeriesCollection createSeriesCollection() {
      OHLCSeries series = new OHLCSeries("Test data");
      OHLCSeriesCollection seriesCollection = new OHLCSeriesCollection();
      seriesCollection.addSeries(series);
      return seriesCollection;
   }

   /**
   * Fill given series with test data
   @param seriesCollection
   * collection with one series element to fill with data
   */
   void createTestData(OHLCSeriesCollection seriesCollection) {
      OHLCSeries series = seriesCollection.getSeries(0);
      for (int i = 0; i < 10; i++) {
         // Generate new bar time
         Calendar cal = Calendar.getInstance();
         cal.add(Calendar.MINUTE, i);
         FixedMillisecond fm = new FixedMillisecond(cal.getTime());
         // Add bar to the data. Let's repeat the same bar
         series.add(fm, 100, 110, 90, 105);
      }
   }
}

 

Leave a Reply

Your email address will not be published.