Example usage for org.jfree.chart.axis DateAxis setTimeline

List of usage examples for org.jfree.chart.axis DateAxis setTimeline

Introduction

In this page you can find the example usage for org.jfree.chart.axis DateAxis setTimeline.

Prototype

public void setTimeline(Timeline timeline) 

Source Link

Document

Sets the underlying timeline to use for this axis.

Usage

From source file:OAT.ui.util.UiUtil.java

/**
 * Returns a chart object./*from   w  w  w.  j  a  va 2 s  . c  om*/
 *
 * @param title
 * @param timeAxisLabel
 * @param valueAxisLabel
 * @param dataSet
 * @param timeline
 * @param theme
 * @param legend
 * @return
 */
public static Chart createTimeBasedChart(String title, String timeAxisLabel, String valueAxisLabel,
        ChartDataset dataSet, Timeline timeline, DefaultTheme theme, boolean legend) {

    //x-axis
    DateAxis timeAxis = new DateAxis(timeAxisLabel);
    timeAxis.setTimeline(timeline);
    //timeAxis.setTimeZone(dataSet.getTimeline().timeZone);

    if (dataSet instanceof TickChart || dataSet instanceof ContractChart) {
        timeAxis.setStandardTickUnits(createSimpleTimeTickUnits());
        //timeAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MINUTE, 30,
        //        new SimpleDateFormat("HH:mm")),
        //       false, true);
    }

    //y-axis
    NumberAxis valueAxis = new NumberAxis(valueAxisLabel);
    valueAxis.setAutoRangeStickyZero(false);
    valueAxis.setAutoRangeIncludesZero(false);

    //renderer
    OHLCRenderer renderer = new OHLCRenderer();
    renderer.setBaseToolTipGenerator(new HighLowItemLabelGenerator());

    //Primary plot
    XYPlot plot = new XYPlot(dataSet, timeAxis, valueAxis, renderer);
    plot.setRangeAxisLocation(AxisLocation.TOP_OR_RIGHT, false);

    Chart chart = new Chart(title, Chart.DEFAULT_TITLE_FONT, plot, legend);

    theme.apply(chart);

    return chart;
}

From source file:com.charts.IntradayChart.java

public IntradayChart(YStockQuote currentStock) {

    TimeSeries series = new TimeSeries(currentStock.get_name());
    ArrayList<String> fiveDayData = currentStock.get_one_day_data();
    int length = fiveDayData.size();
    for (int i = 17; i < length; i++) {
        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 ava  2  s.co m*/
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(
            currentStock.get_name() + "(" + currentStock.get_symbol() + ")" + " Intraday", "Date", "Price",
            dataset, true, true, false);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();
    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    xAxis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());
    xAxis.setDateFormatOverride(new SimpleDateFormat("h:m a"));
    xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    //xAxis.setVerticalTickLabels(true);
    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.sunzoft.sunstock.StockMain.java

protected JFreeChart createChart() {
    // 2Chart[??]
    XYDataset dataset = initChartData();
    JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "", dataset);

    XYPlot xyplot = (XYPlot) chart.getPlot();

    //addIndexChart(xyplot);        

    ChartUtils.setAntiAlias(chart);// 
    ChartUtils.setTimeSeriesStyle(xyplot, false, true);
    ChartUtils.setLegendEmptyBorder(chart);

    // X??/*from   www . ja v a 2s.co  m*/
    DateAxis domainAxis = (DateAxis) xyplot.getDomainAxis();
    domainAxis.setAutoTickUnitSelection(false);
    domainAxis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());

    DateTickUnit dateTickUnit;
    if (dataset.getItemCount(0) < 30)
        dateTickUnit = new DateTickUnit(DateTickUnitType.DAY, 5, new SimpleDateFormat("yyyy-MM-dd")); // ??
    else if (dataset.getItemCount(0) < 100)
        dateTickUnit = new DateTickUnit(DateTickUnitType.DAY, 10, new SimpleDateFormat("yyyy-MM-dd")); // ??
    else if (dataset.getItemCount(0) < 200)
        dateTickUnit = new DateTickUnit(DateTickUnitType.MONTH, 1, new SimpleDateFormat("yyyy/MM")); // ??
    else if (dataset.getItemCount(0) < 500)
        dateTickUnit = new DateTickUnit(DateTickUnitType.MONTH, 3, new SimpleDateFormat("yyyy/MM")); // ??
    else if (dataset.getItemCount(0) < 1000)
        dateTickUnit = new DateTickUnit(DateTickUnitType.MONTH, 6, new SimpleDateFormat("yyyy/MM")); // ??
    else
        dateTickUnit = new DateTickUnit(DateTickUnitType.YEAR, 1, new SimpleDateFormat("yyyy")); // ??
    // ??
    domainAxis.setTickUnit(dateTickUnit);
    return chart;
}

From source file:org.activequant.util.charting.Chart.java

/**
 * helper method to indicate a monday to friday timeline. 
 *///from   w w  w.j  a v a  2 s . com
public void setSuppressWeekends() {
    SegmentedTimeline timeline = SegmentedTimeline.newMondayThroughFridayTimeline();
    final DateAxis axis = (DateAxis) chart.getXYPlot().getDomainAxis();
    axis.setAutoRange(true);
    axis.setTimeline(timeline);
}

From source file:ch.algotrader.client.chart.ChartTab.java

private void resetAxis() {

    // set a default timeline in order to compute the maximum date correctly
    DateAxis domainAxis = (DateAxis) getPlot().getDomainAxis();
    domainAxis.setTimeline(new DefaultTimeline());

    // reset value axis
    ValueAxis rangeAxis = getPlot().getRangeAxis();
    rangeAxis.setAutoRange(true);/*from  ww  w .ja  v a 2s .com*/
}

From source file:ch.agent.crnickl.demo.stox.Chart.java

private JFreeChart makeChart() throws KeyedException {

    if (chartSeries.size() == 0)
        throw new IllegalStateException("addChartSeries() not called");

    if (range == null) {
        for (ChartSeries s : chartSeries) {
            if (range == null)
                range = s.getTimeSeries().getRange();
            else//  w  w w .  j a v a2 s. c om
                range = range.union(s.getTimeSeries().getRange());
        }
    }

    // use number axis for dates, with special formatter
    DateAxis dateAxis = new DateAxis();
    dateAxis.setDateFormatOverride(new CustomDateFormat("M/d/y"));
    if (range.getTimeDomain().getLabel().equals("workweek"))
        dateAxis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());

    // combined plot with shared date axis
    CombinedDomainXYPlot plot = new CombinedDomainXYPlot(dateAxis);

    for (ChartSeries s : chartSeries) {
        makeSubPlot(plot, s);
    }

    // make the chart, remove the legend, set the title
    JFreeChart chart = new JFreeChart(plot);
    if (!withLegend)
        chart.removeLegend();
    chart.setBackgroundPaint(Color.white);
    chart.setTitle(new TextTitle(title));

    return chart;
}

From source file:edu.dlnu.liuwenpeng.ChartFactory.ChartFactory.java

/**    
 * Creates and returns a default instance of a high-low-open-close chart     
 * with a special timeline. This timeline can be a     
 * {@link org.jfree.chart.axis.SegmentedTimeline} such as the Monday     
 * through Friday timeline that will remove Saturdays and Sundays from    
 * the axis.    //from ww w.  j a va 2 s .  c  o  m
 *    
 * @param title  the chart title (<code>null</code> permitted).    
 * @param timeAxisLabel  a label for the time axis (<code>null</code>     
 *                       permitted).    
 * @param valueAxisLabel  a label for the value axis (<code>null</code>     
 *                        permitted).    
 * @param dataset  the dataset for the chart (<code>null</code> permitted).    
 * @param timeline  the timeline.    
 * @param legend  a flag specifying whether or not a legend is required.    
 *    
 * @return A high-low-open-close chart.    
 */
public static JFreeChart createHighLowChart(String title, String timeAxisLabel, String valueAxisLabel,
        OHLCDataset dataset, Timeline timeline, boolean legend) {

    DateAxis timeAxis = new DateAxis(timeAxisLabel);
    timeAxis.setTimeline(timeline);
    NumberAxis valueAxis = new NumberAxis(valueAxisLabel);
    HighLowRenderer renderer = new HighLowRenderer();
    renderer.setBaseToolTipGenerator(new HighLowItemLabelGenerator());
    XYPlot plot = new XYPlot(dataset, timeAxis, valueAxis, renderer);
    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, legend);
    return chart;

}

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  ww w .  j av  a  2s . 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:com.charts.ThreeMonthChart.java

public ThreeMonthChart(YStockQuote currentStock) throws ParseException {

    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis rangeAxis = new NumberAxis("Price");
    CandlestickRenderer renderer = new CandlestickRenderer();
    XYDataset dataset = getDataSet(currentStock);

    XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

    //Do some setting up, see the API Doc
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setDrawVolume(false);//from w w  w .  ja  v a2s .  co m
    rangeAxis.setAutoRangeIncludesZero(false);

    domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

    //Now create the chart and chart panel
    JFreeChart chart = new JFreeChart(currentStock.get_name(), null, mainPlot, false);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 300));

    XYPlot plot = (XYPlot) chart.getPlot();
    LegendTitle legend = new LegendTitle(plot);
    chart.addLegend(legend);
    chart.getLegend().setVisible(true);
    chart.getLegend().setPosition(RectangleEdge.BOTTOM);
    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();
    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
    Calendar[][] holidays = DayRange.getHolidayDates();
    int len = holidays.length;

    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance(TimeZone.getTimeZone("EST"));
        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.getTime());

    }

    xAxis.setTimeline(segmentedTimeline);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MMM y"));
    xAxis.setAutoTickUnitSelection(true);
    xAxis.setAutoRange(true);
    renderer.setAutoWidthFactor(0.5);
    renderer.setUpPaint(Color.green);
    renderer.setDownPaint(new Color(0xc0, 0x00, 0x00));
    renderer.setSeriesPaint(0, Color.black);

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

    chartPanel.revalidate();
    chartPanel.repaint();
    chartPanel.revalidate();
    chartPanel.repaint();
}

From source file:com.charts.OneMonthChart.java

public OneMonthChart(YStockQuote currentStock) throws ParseException {

    DateAxis domainAxis = new DateAxis("Date");
    NumberAxis rangeAxis = new NumberAxis("Price");
    CandlestickRenderer renderer = new CandlestickRenderer();
    XYDataset dataset = getDataSet(currentStock);

    XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

    //Do some setting up, see the API Doc
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setDrawVolume(false);//from  w w  w.  ja v  a 2  s  .co m
    rangeAxis.setAutoRangeIncludesZero(false);

    domainAxis.setVerticalTickLabels(true);
    domainAxis.setDateFormatOverride(new SimpleDateFormat("dd-MM-yy"));
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

    //Now create the chart and chart panel
    JFreeChart chart = new JFreeChart(currentStock.get_name(), null, mainPlot, false);
    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 300));

    XYPlot plot = (XYPlot) chart.getPlot();
    LegendTitle legend = new LegendTitle(plot);
    chart.addLegend(legend);
    chart.getLegend().setVisible(true);
    chart.getLegend().setPosition(RectangleEdge.BOTTOM);

    ValueAxis yAxis = (ValueAxis) plot.getRangeAxis();
    DateAxis xAxis = (DateAxis) plot.getDomainAxis();
    Date now = new Date();
    SegmentedTimeline segmentedTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
    Calendar[][] holidays = DayRange.getHolidayDates();
    int len = holidays.length;

    for (int i = 0; i < holidays[0].length; i++) {
        Calendar day = Calendar.getInstance(TimeZone.getTimeZone("EST"));
        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.getTime());

    }

    xAxis.setTimeline(segmentedTimeline);
    //xAxis.setVerticalTickLabels(true);
    xAxis.setDateFormatOverride(new SimpleDateFormat("MMM-dd"));
    xAxis.setAutoTickUnitSelection(false);
    xAxis.setAutoRange(false);
    renderer.setAutoWidthFactor(0.5);
    renderer.setUpPaint(Color.green);
    renderer.setDownPaint(new Color(0xc0, 0x00, 0x00));
    renderer.setSeriesPaint(0, Color.black);

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

    chartPanel.revalidate();
    chartPanel.repaint();
    chartPanel.revalidate();
    chartPanel.repaint();
}