Example usage for org.jfree.chart.axis PeriodAxis PeriodAxis

List of usage examples for org.jfree.chart.axis PeriodAxis PeriodAxis

Introduction

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

Prototype

public PeriodAxis(String label) 

Source Link

Document

Creates a new axis.

Usage

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

private static JFreeChart createChart(IntervalXYDataset intervalxydataset) {
    JFreeChart jfreechart = ChartFactory.createXYBarChart("Maximum Temperature", "Day", true, "Temperature",
            intervalxydataset, PlotOrientation.VERTICAL, true, true, false);
    jfreechart.setBackgroundPaint(Color.white);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setRangeGridlinePaint(Color.white);
    xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
    xyplot.setDomainCrosshairVisible(true);
    xyplot.setRangeCrosshairVisible(true);
    PeriodAxis periodaxis = new PeriodAxis("Day");
    periodaxis.setAutoRangeTimePeriodClass(org.jfree.data.time.Day.class);
    PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[3];
    aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, new SimpleDateFormat("d"));
    aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, new SimpleDateFormat("E"),
            new RectangleInsets(2D, 2D, 2D, 2D), new Font("SansSerif", 1, 10), Color.blue, false,
            new BasicStroke(0.0F), Color.lightGray);
    aperiodaxislabelinfo[2] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class,
            new SimpleDateFormat("MMM"));
    periodaxis.setLabelInfo(aperiodaxislabelinfo);
    xyplot.setDomainAxis(periodaxis);//  ww  w.j  av a 2  s .c  o  m
    return jfreechart;
}

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

private static JFreeChart createChart(XYDataset xydataset) {
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date",
            "Price Per Unit", xydataset, true, true, false);
    jfreechart.setBackgroundPaint(Color.white);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setRangeGridlinePaint(Color.white);
    xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
    xyplot.setDomainCrosshairVisible(true);
    xyplot.setRangeCrosshairVisible(true);
    org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
    if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer;
        xylineandshaperenderer.setBaseShapesVisible(true);
        xylineandshaperenderer.setBaseShapesFilled(true);
        xylineandshaperenderer.setBaseItemLabelsVisible(true);
    }//from  w  w  w.j a v  a2 s .  c  o  m
    PeriodAxis periodaxis = new PeriodAxis("Date");
    periodaxis.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
    periodaxis.setAutoRangeTimePeriodClass(org.jfree.data.time.Day.class);
    PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[3];
    aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, new SimpleDateFormat("d"));
    aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class,
            new SimpleDateFormat("MMM"), new RectangleInsets(2D, 2D, 2D, 2D), new Font("SansSerif", 1, 10),
            Color.blue, false, new BasicStroke(0.0F), Color.lightGray);
    aperiodaxislabelinfo[2] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class,
            new SimpleDateFormat("yyyy"));
    periodaxis.setLabelInfo(aperiodaxislabelinfo);
    xyplot.setDomainAxis(periodaxis);
    return jfreechart;
}

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

private static JFreeChart createChart(XYDataset xydataset) {
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date",
            "Price Per Unit", xydataset, true, true, false);
    jfreechart.setBackgroundPaint(Color.white);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setBackgroundPaint(Color.lightGray);
    xyplot.setDomainGridlinePaint(Color.white);
    xyplot.setRangeGridlinePaint(Color.white);
    xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
    xyplot.setDomainCrosshairVisible(true);
    xyplot.setRangeCrosshairVisible(true);
    org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
    if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer;
        xylineandshaperenderer.setBaseShapesVisible(true);
        xylineandshaperenderer.setBaseShapesFilled(true);
        xylineandshaperenderer.setBaseItemLabelsVisible(true);
    }//from ww w .j a v  a2 s  .c o  m
    PeriodAxis periodaxis = new PeriodAxis("Date");
    periodaxis.setTimeZone(TimeZone.getTimeZone("Pacific/Auckland"));
    periodaxis.setAutoRangeTimePeriodClass(org.jfree.data.time.Month.class);
    periodaxis.setMajorTickTimePeriodClass(org.jfree.data.time.Month.class);
    PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[2];
    aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class,
            new SimpleDateFormat("MMM"), new RectangleInsets(2D, 2D, 2D, 2D), new Font("SansSerif", 1, 10),
            Color.blue, false, new BasicStroke(0.0F), Color.lightGray);
    aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class,
            new SimpleDateFormat("yyyy"));
    periodaxis.setLabelInfo(aperiodaxislabelinfo);
    xyplot.setDomainAxis(periodaxis);
    return jfreechart;
}

From source file:org.webcat.grader.graphs.StackedAreaChart.java

@Override
protected JFreeChart generateChart(WCChartTheme chartTheme) {
    JFreeChart chart = ChartFactory.createStackedXYAreaChart(null, xAxisLabel(), yAxisLabel(), tableXYDataset(),
            orientation(), true, false, false);

    XYPlot plot = chart.getXYPlot();/*from  ww w .  jav  a 2s .  c o  m*/

    long diff = (long) tableXYDataset().getXValue(0, tableXYDataset().getItemCount() - 1)
            - (long) tableXYDataset().getXValue(0, 0);

    GregorianCalendar calDiff = new GregorianCalendar();
    calDiff.setTime(new NSTimestamp(diff));

    // Set the time axis
    PeriodAxis axis = new PeriodAxis(null); // ( "Date" );
    PeriodAxisLabelInfo labelinfo[] = new PeriodAxisLabelInfo[2];

    if (calDiff.get(Calendar.DAY_OF_YEAR) > 1) {
        axis.setTimeZone(TimeZone.getTimeZone(user().timeZoneName()));
        axis.setAutoRangeTimePeriodClass(org.jfree.data.time.Day.class);
        axis.setMajorTickTimePeriodClass(org.jfree.data.time.Week.class);

        labelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, new SimpleDateFormat("d"),
                PeriodAxisLabelInfo.DEFAULT_INSETS, chartTheme.smallFont(), chartTheme.textColor(), true,
                PeriodAxisLabelInfo.DEFAULT_DIVIDER_STROKE, PeriodAxisLabelInfo.DEFAULT_DIVIDER_PAINT);

        labelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class, new SimpleDateFormat("MMM"),
                PeriodAxisLabelInfo.DEFAULT_INSETS, chartTheme.smallFont(), chartTheme.textColor(), true,
                PeriodAxisLabelInfo.DEFAULT_DIVIDER_STROKE, PeriodAxisLabelInfo.DEFAULT_DIVIDER_PAINT);
    } else {
        axis.setAutoRangeTimePeriodClass(org.jfree.data.time.Hour.class);

        labelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class, new SimpleDateFormat("ha"),
                PeriodAxisLabelInfo.DEFAULT_INSETS, chartTheme.smallFont(), chartTheme.textColor(), true,
                PeriodAxisLabelInfo.DEFAULT_DIVIDER_STROKE, PeriodAxisLabelInfo.DEFAULT_DIVIDER_PAINT);

        labelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class, new SimpleDateFormat("MMM-d"),
                PeriodAxisLabelInfo.DEFAULT_INSETS, chartTheme.smallFont(), chartTheme.textColor(), true,
                PeriodAxisLabelInfo.DEFAULT_DIVIDER_STROKE, PeriodAxisLabelInfo.DEFAULT_DIVIDER_PAINT);
    }

    axis.setLabelInfo(labelinfo);
    plot.setDomainAxis(axis);

    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    NumberTickUnit tickUnit = new NumberTickUnit(5);
    rangeAxis.setTickUnit(tickUnit);

    XYAreaRenderer2 renderer = (XYAreaRenderer2) plot.getRenderer();
    renderer.setOutline(true);
    renderer.setAutoPopulateSeriesOutlinePaint(true);

    plot.setDomainMinorGridlinesVisible(false);
    plot.setRangeMinorGridlinesVisible(false);

    if (markValue != null) {
        plot.setDomainCrosshairVisible(true);
        plot.setDomainCrosshairValue(markValue.doubleValue());
        plot.setDomainCrosshairPaint(Color.red);
        plot.setDomainCrosshairStroke(MARKER_STROKE);
    }

    chart.getLegend().setBorder(0, 0, 0, 0);

    return chart;
}

From source file:org.sakaiproject.sitestats.impl.chart.ChartServiceImpl.java

private byte[] generateTimeSeriesChart(String siteId, IntervalXYDataset dataset, int width, int height,
        boolean renderBar, float transparency, boolean itemLabelsVisible, boolean smallFontInDomainAxis,
        String timePeriod, Date firstDate, Date lastDate) {
    JFreeChart chart = null;//from w  ww.  j  ava 2s .  c o m
    if (!renderBar) {
        chart = ChartFactory.createTimeSeriesChart(null, null, null, dataset, true, false, false);
    } else {
        chart = ChartFactory.createXYBarChart(null, null, true, null, dataset, PlotOrientation.VERTICAL, true,
                false, false);
    }
    XYPlot plot = (XYPlot) chart.getPlot();

    // set transparency
    plot.setForegroundAlpha(transparency);

    // set background
    chart.setBackgroundPaint(parseColor(M_sm.getChartBackgroundColor()));

    // set chart border
    chart.setPadding(new RectangleInsets(10, 5, 5, 5));
    chart.setBorderVisible(true);
    chart.setBorderPaint(parseColor("#cccccc"));

    // set antialias
    chart.setAntiAlias(true);

    // set domain axis font size
    if (smallFontInDomainAxis && !canUseNormalFontSize(width)) {
        plot.getDomainAxis().setTickLabelFont(new Font("SansSerif", Font.PLAIN, 8));
    }

    // configure date display (localized) in domain axis
    Locale locale = msgs.getLocale();
    PeriodAxis periodaxis = new PeriodAxis(null);
    Class timePeriodClass = null;
    if (dataset instanceof TimeSeriesCollection) {
        TimeSeriesCollection tsc = (TimeSeriesCollection) dataset;
        if (tsc.getSeriesCount() > 0) {
            timePeriodClass = tsc.getSeries(0).getTimePeriodClass();
        } else {
            timePeriodClass = org.jfree.data.time.Day.class;
        }
        periodaxis.setAutoRangeTimePeriodClass(timePeriodClass);
    }
    PeriodAxisLabelInfo aperiodaxislabelinfo[] = null;
    if (StatsManager.CHARTTIMESERIES_WEEKDAY.equals(timePeriod)) {
        aperiodaxislabelinfo = new PeriodAxisLabelInfo[2];
        aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class,
                new SimpleDateFormat("E", locale));
        aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class,
                new SimpleDateFormat("d", locale));
    } else if (StatsManager.CHARTTIMESERIES_DAY.equals(timePeriod)) {
        aperiodaxislabelinfo = new PeriodAxisLabelInfo[3];
        aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class,
                new SimpleDateFormat("d", locale));
        aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class,
                new SimpleDateFormat("MMM", locale));
        aperiodaxislabelinfo[2] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class,
                new SimpleDateFormat("yyyy", locale));
    } else if (StatsManager.CHARTTIMESERIES_MONTH.equals(timePeriod)) {
        aperiodaxislabelinfo = new PeriodAxisLabelInfo[2];
        aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Month.class,
                new SimpleDateFormat("MMM", locale));
        aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class,
                new SimpleDateFormat("yyyy", locale));
    } else if (StatsManager.CHARTTIMESERIES_YEAR.equals(timePeriod)) {
        aperiodaxislabelinfo = new PeriodAxisLabelInfo[1];
        aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Year.class,
                new SimpleDateFormat("yyyy", locale));
    }
    periodaxis.setLabelInfo(aperiodaxislabelinfo);
    // date range
    if (firstDate != null || lastDate != null) {
        periodaxis.setAutoRange(false);
        if (firstDate != null) {
            if (StatsManager.CHARTTIMESERIES_MONTH.equals(timePeriod)
                    || StatsManager.CHARTTIMESERIES_YEAR.equals(timePeriod)) {
                periodaxis.setFirst(new org.jfree.data.time.Month(firstDate));
            } else {
                periodaxis.setFirst(new org.jfree.data.time.Day(firstDate));
            }
        }
        if (lastDate != null) {
            if (StatsManager.CHARTTIMESERIES_MONTH.equals(timePeriod)
                    || StatsManager.CHARTTIMESERIES_YEAR.equals(timePeriod)) {
                periodaxis.setLast(new org.jfree.data.time.Month(lastDate));
            } else {
                periodaxis.setLast(new org.jfree.data.time.Day(lastDate));
            }
        }
    }
    periodaxis.setTickMarkOutsideLength(0.0F);
    plot.setDomainAxis(periodaxis);

    // set outline
    AbstractXYItemRenderer renderer = (AbstractXYItemRenderer) plot.getRenderer();
    if (renderer instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) renderer;
        r.setDrawSeriesLineAsPath(true);
        r.setShapesVisible(true);
        r.setShapesFilled(true);
    } else if (renderer instanceof XYBarRenderer) {
        //XYBarRenderer r = (XYBarRenderer) renderer;
        ClusteredXYBarRenderer r = new ClusteredXYBarRenderer();
        r.setDrawBarOutline(true);
        if (smallFontInDomainAxis && !canUseNormalFontSize(width))
            r.setMargin(0.05);
        else
            r.setMargin(0.10);
        plot.setRenderer(r);
        renderer = r;
    }

    // item labels
    if (itemLabelsVisible) {
        plot.getRangeAxis().setUpperMargin(0.2);
        renderer.setItemLabelGenerator(new XYItemLabelGenerator() {
            private static final long serialVersionUID = 1L;

            public String generateLabel(XYDataset dataset, int series, int item) {
                Number n = dataset.getY(series, item);
                if (n.doubleValue() != 0)
                    return n.toString();
                return "";
            }

        });
        renderer.setItemLabelFont(new Font("SansSerif", Font.PLAIN, 8));
        renderer.setItemLabelsVisible(true);
    }

    BufferedImage img = chart.createBufferedImage(width, height);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        ImageIO.write(img, "png", out);
    } catch (IOException e) {
        LOG.warn("Error occurred while generating SiteStats chart image data", e);
    }
    return out.toByteArray();
}