Example usage for org.jfree.chart.axis SegmentedTimeline newFifteenMinuteTimeline

List of usage examples for org.jfree.chart.axis SegmentedTimeline newFifteenMinuteTimeline

Introduction

In this page you can find the example usage for org.jfree.chart.axis SegmentedTimeline newFifteenMinuteTimeline.

Prototype

public static SegmentedTimeline newFifteenMinuteTimeline() 

Source Link

Document

Factory method to create a 15-min, 9:00 AM thought 4:00 PM, Monday through Friday SegmentedTimeline.

Usage

From source file:com.charts.FiveDayChart.java

public FiveDayChart(YStockQuote currentStock) {
    TimeSeries series = new TimeSeries(currentStock.get_name());
    ArrayList<String> fiveDayData = currentStock.get_five_day_data();
    int length = fiveDayData.size();
    for (int i = 22; i < length; i += 5) {
        String[] data = fiveDayData.get(i).split(",");
        Date time = new Date((long) Integer.parseInt(data[0]) * 1000);
        DateFormat df = new SimpleDateFormat("MM-dd-yyyy-h-m");
        series.addOrUpdate(new Minute(time), Double.parseDouble(data[1]));
    }/*from w  w w  .  j a va  2  s.  c  om*/
    String[] data = fiveDayData.get(length - 1).split(",");
    Date time = new Date((long) Integer.parseInt(data[0]) * 1000);
    DateFormat df = new SimpleDateFormat("MM-dd-yyyy-h-m");
    series.addOrUpdate(new Minute(time), Double.parseDouble(data[1]));

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            currentStock.get_name() + "(" + currentStock.get_symbol() + ")" + " Five Day", "Date", "Price",
            dataset, true, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();

    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newFifteenMinuteTimeline();
    segmentedTimeline.addBaseTimelineExclusions(segmentedTimeline.getStartTime(), now.getTime());
    Calendar[][] holidays = DayRange.getHolidayDates();
    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance();
        day.set(Calendar.YEAR, holidays[0][i].get(Calendar.YEAR));
        day.set(Calendar.MONTH, holidays[0][i].get(Calendar.MONTH));
        day.set(Calendar.DAY_OF_MONTH, holidays[0][i].get(Calendar.DAY_OF_MONTH));
        day.set(Calendar.HOUR_OF_DAY, 9);
        segmentedTimeline.addException(day.getTimeInMillis(), day.getTimeInMillis() + 21600000);
    }
    xAxis.setTimeline(segmentedTimeline);
    xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MM-dd"));
    xAxis.setAutoTickUnitSelection(false);
    xAxis.setAutoRange(false);

    StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
    renderer1.setSeriesPaint(0, Color.BLUE);
    TimeSeries movingAverage5 = MovingAverage.createMovingAverage(series, "MA(5)", 30, 0);
    Double currMA5 = (Double) movingAverage5.getDataItem(movingAverage5.getItemCount() - 1).getValue();
    currMA5 = Math.round(currMA5 * 100.0) / 100.0;
    movingAverage5.setKey("MA(5): " + currMA5);
    TimeSeriesCollection collection = new TimeSeriesCollection();
    collection.addSeries(movingAverage5);
    plot.setDataset(1, collection);
    plot.setRenderer(1, renderer1);

    plot.setBackgroundPaint(Color.WHITE);

    chartPanel = new ChartPanel(chart);
    chart.setBackgroundPaint(chartPanel.getBackground());
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);
    chartPanel.setVisible(true);
    chartPanel.revalidate();
    chartPanel.repaint();
}

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  a va2s  .  c o  m
 *
 * @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);

}