Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

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.

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

                         

» View all related tutorials
Related Tags: java c data 3d object dataset io help method char default value chart this set for add e bar section

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.