Example usage for org.jfree.chart ChartFactory createHighLowChart

List of usage examples for org.jfree.chart ChartFactory createHighLowChart

Introduction

In this page you can find the example usage for org.jfree.chart ChartFactory createHighLowChart.

Prototype

public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel,
        OHLCDataset dataset, Timeline timeline, boolean legend) 

Source Link

Document

Creates and returns a default instance of a high-low-open-close chart with a special timeline.

Usage

From source file:org.jfree.chart.demo.SegmentedHighLowChartDemo.java

/**
 * A demonstration application showing a high-low-open-close chart using a
 * segmented or non-segmented axis./*from  w  w  w .  j  av a2  s . com*/
 *
 * @param title  the frame title.
 * @param useSegmentedAxis use a segmented axis for this demo?
 * @param timelineType Type of timeline to use: 1=Monday through Friday, 2=Intraday
 */
public SegmentedHighLowChartDemo(final String title, final boolean useSegmentedAxis, final int timelineType) {

    super(title);

    System.out.println("\nMaking SegmentedHighLowChartDemo(" + title + ")");

    // create a Calendar object with today's date at midnight
    final Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    // create a timeline for the demo
    SegmentedTimeline timeline = null;
    switch (timelineType) {
    case 1:
        timeline = SegmentedTimeline.newMondayThroughFridayTimeline();
        break;

    case 2:
        timeline = SegmentedTimeline.newFifteenMinuteTimeline();

        final Calendar cal2 = (Calendar) cal.clone();
        cal2.add(Calendar.YEAR, 1);

        // add 1 year of baseTimeline's excluded segments (Saturdays and Sundays) as
        // exceptions of the intraday timeline
        timeline.addBaseTimelineExclusions(cal.getTime().getTime(), cal2.getTime().getTime());
        break;

    default:
        System.out.println("Invalid timelineType.");
        System.exit(1);
    }

    // create a data set that has data for trading days (Monday through Friday).
    final DefaultHighLowDataset dataset = DemoDatasetFactory.createSegmentedHighLowDataset(timeline,
            cal.getTime());

    final JFreeChart chart;
    if (useSegmentedAxis) {
        chart = ChartFactory.createHighLowChart(title, "Time", "Value", dataset, timeline, true);
    } else {
        chart = ChartFactory.createHighLowChart(title, "Time", "Value", dataset, true);
    }

    final DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
    axis.setAutoRange(true);
    final TickUnits units = new TickUnits();
    units.add(new DateTickUnit(DateTickUnit.DAY, 1, DateTickUnit.HOUR, 1, new SimpleDateFormat("d-MMM")));
    units.add(new DateTickUnit(DateTickUnit.DAY, 2, DateTickUnit.HOUR, 1, new SimpleDateFormat("d-MMM")));
    units.add(new DateTickUnit(DateTickUnit.DAY, 7, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM")));
    units.add(new DateTickUnit(DateTickUnit.DAY, 15, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM")));
    units.add(new DateTickUnit(DateTickUnit.DAY, 30, DateTickUnit.DAY, 1, new SimpleDateFormat("d-MMM")));
    axis.setStandardTickUnits(units);

    final NumberAxis vaxis = (NumberAxis) chart.getXYPlot().getRangeAxis();
    vaxis.setAutoRangeIncludesZero(false);

    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

    setContentPane(chartPanel);

}