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

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

Introduction

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

Prototype

public void setLabelInfo(PeriodAxisLabelInfo[] info) 

Source Link

Document

Sets the array of label info records and sends an AxisChangeEvent to all registered listeners.

Usage

From source file:org.jfree.chart.demo.QuarterDateFormatDemo.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();
    PeriodAxis periodaxis = new PeriodAxis("Quarter", new Quarter(), new Quarter());
    periodaxis.setAutoRangeTimePeriodClass(org.jfree.data.time.Quarter.class);
    PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[1];
    aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Quarter.class,
            new QuarterDateFormat(TimeZone.getDefault(), QuarterDateFormat.ROMAN_QUARTERS));
    periodaxis.setLabelInfo(aperiodaxislabelinfo);
    xyplot.setDomainAxis(periodaxis);/* ww w. j a  va  2s  . c  o m*/
    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);
    }
    return jfreechart;
}

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

private static JFreeChart createChart(XYDataset xydataset) {
    JFreeChart jfreechart = ChartFactory.createXYLineChart("Marker Demo 2", "X", "Temperature", xydataset,
            PlotOrientation.VERTICAL, false, true, false);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setDomainGridlinePaint(Color.lightGray);
    xyplot.setDomainGridlineStroke(new BasicStroke(1.0F));
    xyplot.setRangeGridlinePaint(Color.lightGray);
    xyplot.setRangeGridlineStroke(new BasicStroke(1.0F));
    xyplot.setRangeTickBandPaint(new Color(240, 240, 240));
    PeriodAxis periodaxis = new PeriodAxis(null, new Hour(0, 30, 6, 2005), new Hour(23, 30, 6, 2005));
    PeriodAxisLabelInfo aperiodaxislabelinfo[] = new PeriodAxisLabelInfo[2];
    aperiodaxislabelinfo[0] = new PeriodAxisLabelInfo(org.jfree.data.time.Hour.class,
            new SimpleDateFormat("HH"));
    aperiodaxislabelinfo[1] = new PeriodAxisLabelInfo(org.jfree.data.time.Day.class,
            new SimpleDateFormat("dd-MMM"));
    periodaxis.setLabelInfo(aperiodaxislabelinfo);
    xyplot.setDomainAxis(periodaxis);/*from  w w  w .  j  a v  a 2s  .  c o  m*/
    ValueAxis valueaxis = xyplot.getRangeAxis();
    valueaxis.setRange(0.0D, 100D);
    XYItemRenderer xyitemrenderer = xyplot.getRenderer();
    xyitemrenderer.setSeriesPaint(0, Color.green);
    xyitemrenderer.setSeriesStroke(0, new BasicStroke(2.0F));
    ValueMarker valuemarker = new ValueMarker(80D);
    valuemarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
    valuemarker.setPaint(Color.red);
    valuemarker.setStroke(new BasicStroke(2.0F));
    valuemarker.setLabel("Temperature Threshold");
    valuemarker.setLabelFont(new Font("SansSerif", 0, 11));
    valuemarker.setLabelPaint(Color.red);
    valuemarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    valuemarker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
    xyplot.addRangeMarker(valuemarker);
    Hour hour = new Hour(18, 30, 6, 2005);
    Hour hour1 = new Hour(20, 30, 6, 2005);
    double d = hour.getFirstMillisecond();
    double d1 = hour1.getFirstMillisecond();
    IntervalMarker intervalmarker = new IntervalMarker(d, d1);
    intervalmarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
    intervalmarker.setPaint(new Color(150, 150, 255));
    intervalmarker.setLabel("Automatic Cooling");
    intervalmarker.setLabelFont(new Font("SansSerif", 0, 11));
    intervalmarker.setLabelPaint(Color.blue);
    intervalmarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    intervalmarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
    xyplot.addDomainMarker(intervalmarker, Layer.BACKGROUND);
    ValueMarker valuemarker1 = new ValueMarker(d, Color.blue, new BasicStroke(2.0F));
    ValueMarker valuemarker2 = new ValueMarker(d1, Color.blue, new BasicStroke(2.0F));
    xyplot.addDomainMarker(valuemarker1, Layer.BACKGROUND);
    xyplot.addDomainMarker(valuemarker2, Layer.BACKGROUND);
    return jfreechart;
}

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);//from  w  w w .  j  a  v  a2s  .c  o  m
    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);
    }/*  w w w .  j  a  v  a 2s  . 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.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  ww  w  . ja v a2  s .c om*/
    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.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 w  w w.  j  av a 2 s.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;/*  w ww  .j a  v  a 2 s.  c  om*/
    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();
}