Example usage for org.jfree.chart.axis DateTickMarkPosition MIDDLE

List of usage examples for org.jfree.chart.axis DateTickMarkPosition MIDDLE

Introduction

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

Prototype

DateTickMarkPosition MIDDLE

To view the source code for org.jfree.chart.axis DateTickMarkPosition MIDDLE.

Click Source Link

Document

Middle of period.

Usage

From source file:ucar.unidata.idv.control.chart.TimeSeriesChart.java

/**
 * Make the plot//  w w  w  .j a va  2s  .c  om
 *
 * @return The plot_
 */
public Plot doMakePlot() {

    IdvPreferenceManager pref = control.getControlContext().getIdv().getPreferenceManager();
    TimeZone timeZone = pref.getDefaultTimeZone();
    NumberAxis valueAxis = new FixedWidthNumberAxis("");
    final SimpleDateFormat sdf = new SimpleDateFormat(
            ((dateFormat != null) ? dateFormat : pref.getDefaultDateFormat()));
    sdf.setTimeZone(timeZone);
    DateAxis timeAxis = new DateAxis("Time (" + timeZone.getID() + ")", timeZone) {

        protected List xxxxxrefreshTicksHorizontal(Graphics2D g2, Rectangle2D dataArea, RectangleEdge edge) {

            List ticks = super.refreshTicksHorizontal(g2, dataArea, edge);

            List<Tick> result = new java.util.ArrayList<Tick>();

            Font tickLabelFont = getTickLabelFont();
            g2.setFont(tickLabelFont);

            if (isAutoTickUnitSelection()) {
                selectAutoTickUnit(g2, dataArea, edge);
            }

            DateTickUnit unit = getTickUnit();
            Date tickDate = calculateLowestVisibleTickValue(unit);
            Date upperDate = getMaximumDate();

            Date firstDate = null;
            while (tickDate.before(upperDate)) {

                if (!isHiddenValue(tickDate.getTime())) {
                    // work out the value, label and position
                    String tickLabel;
                    DateFormat formatter = getDateFormatOverride();
                    if (firstDate == null) {
                        if (formatter != null) {
                            tickLabel = formatter.format(tickDate);
                        } else {
                            tickLabel = getTickUnit().dateToString(tickDate);
                        }
                        firstDate = tickDate;
                    } else {
                        double msdiff = tickDate.getTime() - firstDate.getTime();
                        int hours = (int) (msdiff / 1000 / 60 / 60);
                        tickLabel = hours + "H";
                    }
                    //                tickLabel = tickLabel;
                    TextAnchor anchor = null;
                    TextAnchor rotationAnchor = null;
                    double angle = 0.0;
                    if (isVerticalTickLabels()) {
                        anchor = TextAnchor.CENTER_RIGHT;
                        rotationAnchor = TextAnchor.CENTER_RIGHT;
                        if (edge == RectangleEdge.TOP) {
                            angle = Math.PI / 2.0;
                        } else {
                            angle = -Math.PI / 2.0;
                        }
                    } else {
                        if (edge == RectangleEdge.TOP) {
                            anchor = TextAnchor.BOTTOM_CENTER;
                            rotationAnchor = TextAnchor.BOTTOM_CENTER;
                        } else {
                            anchor = TextAnchor.TOP_CENTER;
                            rotationAnchor = TextAnchor.TOP_CENTER;
                        }
                    }

                    Tick tick = new DateTick(tickDate, tickLabel, anchor, rotationAnchor, angle);
                    result.add(tick);
                    tickDate = unit.addToDate(tickDate, getTimeZone());
                } else {
                    tickDate = unit.rollDate(tickDate, getTimeZone());

                    continue;
                }

                // could add a flag to make the following correction optional...
                switch (unit.getUnit()) {

                case (DateTickUnit.MILLISECOND):
                case (DateTickUnit.SECOND):
                case (DateTickUnit.MINUTE):
                case (DateTickUnit.HOUR):
                case (DateTickUnit.DAY):
                    break;

                case (DateTickUnit.MONTH):
                    tickDate = calculateDateForPositionX(new Month(tickDate, getTimeZone()),
                            getTickMarkPosition());

                    break;

                case (DateTickUnit.YEAR):
                    tickDate = calculateDateForPositionX(new Year(tickDate, getTimeZone()),
                            getTickMarkPosition());

                    break;

                default:
                    break;

                }

            }

            return result;

        }

        private Date calculateDateForPositionX(RegularTimePeriod period, DateTickMarkPosition position) {

            if (position == null) {
                throw new IllegalArgumentException("Null 'position' argument.");
            }
            Date result = null;
            if (position == DateTickMarkPosition.START) {
                result = new Date(period.getFirstMillisecond());
            } else if (position == DateTickMarkPosition.MIDDLE) {
                result = new Date(period.getMiddleMillisecond());
            } else if (position == DateTickMarkPosition.END) {
                result = new Date(period.getLastMillisecond());
            }

            return result;

        }

    };
    timeAxis.setDateFormatOverride(sdf);

    final XYPlot[] xyPlotHolder = { null };

    xyPlotHolder[0] = new MyXYPlot(new TimeSeriesCollection(), timeAxis, valueAxis, null) {
        public void drawBackground(Graphics2D g2, Rectangle2D area) {
            super.drawBackground(g2, area);
            drawSunriseSunset(g2, xyPlotHolder[0], area);
        }
    };

    if (animationTimeAnnotation != null) {
        xyPlotHolder[0].addAnnotation(animationTimeAnnotation);
    }

    return xyPlotHolder[0];

}

From source file:org.projectforge.statistics.TimesheetDisciplineChartBuilder.java

private JFreeChart create(final TimeSeries series1, final TimeSeries series2, final Shape shape,
        final Stroke stroke, final boolean showAxisValues, final String valueAxisUnitKey) {
    final TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series1);//  ww w.j a v  a2s.co  m
    dataset.addSeries(series2);
    final JFreeChart chart = ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.VERTICAL,
            true, true, false);

    final XYDifferenceRenderer renderer = new XYDifferenceRenderer(new Color(238, 176, 176),
            new Color(135, 206, 112), true);
    renderer.setSeriesPaint(0, new Color(222, 23, 33));
    renderer.setSeriesPaint(1, new Color(64, 169, 59));
    if (shape != null) {
        renderer.setSeriesShape(0, shape);
        renderer.setSeriesShape(1, shape);
    } else {
        final Shape none = new Rectangle();
        renderer.setSeriesShape(0, none);
        renderer.setSeriesShape(1, none);
    }
    renderer.setSeriesStroke(0, stroke);
    renderer.setSeriesStroke(1, stroke);
    renderer.setSeriesVisibleInLegend(0, false);
    renderer.setSeriesVisibleInLegend(1, false);
    final XYPlot plot = chart.getXYPlot();
    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.lightGray);
    plot.setRangeGridlinePaint(Color.lightGray);
    final DateAxis xAxis = new DateAxis();
    xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    xAxis.setLowerMargin(0.0);
    xAxis.setUpperMargin(0.0);
    xAxis.setVisible(showAxisValues);
    plot.setDomainAxis(xAxis);
    final NumberAxis yAxis;
    if (showAxisValues == true) {
        yAxis = new NumberAxis(PFUserContext.getLocalizedString(valueAxisUnitKey));
    } else {
        yAxis = new NumberAxis();
    }
    yAxis.setVisible(showAxisValues);
    plot.setRangeAxis(yAxis);
    plot.setOutlineVisible(false);
    return chart;
}

From source file:de.tsystems.mms.apm.performancesignature.PerfSigBuildActionResultsDisplay.java

private JFreeChart createTimeSeriesChart(final StaplerRequest req, final XYDataset dataset)
        throws UnsupportedEncodingException {
    String chartDashlet = req.getParameter(Messages.PerfSigBuildActionResultsDisplay_ReqParamChartDashlet());
    String measure = req.getParameter(Messages.PerfSigBuildActionResultsDisplay_ReqParamMeasure());
    String unit = req.getParameter(Messages.PerfSigBuildActionResultsDisplay_ReqParamUnit());
    String color = req.getParameter(Messages.PerfSigBuildActionResultsDisplay_ReqParamColor());
    if (StringUtils.isBlank(color))
        color = Messages.PerfSigBuildActionResultsDisplay_DefaultColor();
    else/*from www. jav  a  2s .  com*/
        URLDecoder.decode(req.getParameter(Messages.PerfSigBuildActionResultsDisplay_ReqParamColor()), "UTF-8");

    String[] timeUnits = { "ns", "ms", "s", "min", "h" };
    JFreeChart chart;

    if (ArrayUtils.contains(timeUnits, unit)) {
        chart = ChartFactory.createTimeSeriesChart(PerfSigUtils.generateTitle(measure, chartDashlet), // title
                "time", // domain axis label
                unit, dataset, // data
                false, // include legend
                false, // tooltips
                false // urls
        );
    } else {
        chart = ChartFactory.createXYBarChart(PerfSigUtils.generateTitle(measure, chartDashlet), // title
                "time", // domain axis label
                true, unit, (IntervalXYDataset) dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                false, // tooltips
                false // urls
        );
    }

    XYPlot xyPlot = chart.getXYPlot();
    xyPlot.setForegroundAlpha(0.8f);
    xyPlot.setRangeGridlinesVisible(true);
    xyPlot.setRangeGridlinePaint(Color.black);
    xyPlot.setOutlinePaint(null);

    XYItemRenderer xyitemrenderer = xyPlot.getRenderer();
    if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer;
        xylineandshaperenderer.setBaseShapesVisible(true);
        xylineandshaperenderer.setBaseShapesFilled(true);
    }
    DateAxis dateAxis = (DateAxis) xyPlot.getDomainAxis();
    dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));
    xyitemrenderer.setSeriesPaint(0, Color.decode(color));
    xyitemrenderer.setSeriesStroke(0, new BasicStroke(2));

    chart.setBackgroundPaint(Color.white);
    return chart;
}

From source file:org.jivesoftware.openfire.reporting.graph.GraphEngine.java

/**
 * Generates a simple Time Axis.//ww  w .java 2s .  c o m
 *
 * @return the generated Time Axis.
 */
private DateAxis generateTimeAxis() {
    DateAxis xAxis = new DateAxis("");
    xAxis.setLowerMargin(0.05);
    xAxis.setUpperMargin(0.02);
    xAxis.setLabel(null);
    xAxis.setTickLabelsVisible(true);
    xAxis.setTickMarksVisible(true);

    xAxis.setAxisLineVisible(true);
    xAxis.setNegativeArrowVisible(false);
    xAxis.setPositiveArrowVisible(false);
    xAxis.setVisible(true);
    xAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    Locale locale = JiveGlobals.getLocale();
    // If the tick units have not yet been setup or the locale has changed
    if (tickUnits == null || !locale.equals(oldLocale)) {
        tickUnits = createTickUnits(locale, JiveGlobals.getTimeZone());
        oldLocale = locale;
    }
    xAxis.setStandardTickUnits(tickUnits);

    return xAxis;
}

From source file:ec.ui.view.MarginView.java

private JFreeChart createMarginViewChart() {
    JFreeChart result = ChartFactory.createXYLineChart("", "", "", Charts.emptyXYDataset(),
            PlotOrientation.VERTICAL, false, false, false);
    result.setPadding(TsCharts.CHART_PADDING);

    XYPlot plot = result.getXYPlot();//from ww  w  . j  a  v a  2  s . com
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    LinesThickness linesThickness = LinesThickness.Thin;

    XYLineAndShapeRenderer main = new LineRenderer();
    plot.setRenderer(MAIN_INDEX, main);

    XYDifferenceRenderer difference = new XYDifferenceRenderer();
    difference.setAutoPopulateSeriesPaint(false);
    difference.setAutoPopulateSeriesStroke(false);
    difference.setBaseStroke(TsCharts.getNormalStroke(linesThickness));
    plot.setRenderer(DIFFERENCE_INDEX, difference);

    DateAxis domainAxis = new DateAxis();
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    domainAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR);
    plot.setDomainAxis(domainAxis);

    NumberAxis rangeAxis = new NumberAxis();
    rangeAxis.setAutoRangeIncludesZero(false);
    rangeAxis.setTickLabelPaint(TsCharts.CHART_TICK_LABEL_COLOR);
    plot.setRangeAxis(rangeAxis);

    return result;
}

From source file:ec.ui.view.RevisionSaSeriesView.java

private void configureAxis(XYPlot plot) {
    SimpleDateFormat sdf = new SimpleDateFormat("MM-yyyy");
    DateAxis dateAxis = new DateAxis();
    dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    dateAxis.setDateFormatOverride(sdf);
    plot.setDomainAxis(dateAxis);//from  w  w w  .  j  av  a 2  s. c  o  m
    NumberAxis yaxis = new NumberAxis();
    if (range != null) {
        yaxis.setRange(range);
    }
    plot.setRangeAxis(yaxis);
}

From source file:ec.util.chart.swing.JTimeSeriesChart.java

private static JFreeChart createTsChart() {
    CombinedDomainXYPlot plot = new CombinedDomainXYPlot();

    plot.setAxisOffset(RectangleInsets.ZERO_INSETS);

    DateAxis domainAxis = new DateAxis();
    domainAxis.setTickLabelInsets(new RectangleInsets(2, 5, 2, 5));
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    domainAxis.setLowerMargin(0.02);//  w w  w .  jav a 2s.c  o  m
    domainAxis.setUpperMargin(0.02);
    plot.setDomainAxis(domainAxis);

    JFreeChart result = new JFreeChart("", null, plot, true);
    result.setPadding(CHART_PADDING);
    result.getLegend().setFrame(BlockBorder.NONE);
    result.getLegend().setBackgroundPaint(null);

    return result;
}

From source file:de.fau.amos.ChartRenderer.java

/**
 * Creates Chart with Bars (JFreeChart object) from TimeSeriesCollection. Is used when granularity is set to days, months or years.
 * Adjusts display of the chart, not the values itself.
 * /*from  w ww.ja  va2s .com*/
 * @param collection TimeSeriesCollection that should be used as basis of chart.
 * @param timeGranularity Selected granularity. Value is similar to granularity contained in TimeSeriesCollection "collection".
 * @param time first element of time that is displayed. Is used for caption text and axis text.
 * @param unit Unit of displayed values (kWh or kWh/TNF).
 * @return Returns finished JFreeChart object.
 */
private JFreeChart createTimeBarChart(TimeSeriesCollection collection, String timeGranularity, String time,
        String unit) {

    String xAxisLabel = null;

    // Modification of X-Axis Label (depending on the granularity)
    int month;

    String monthString = null;
    switch (timeGranularity) {
    //for Case "0" see method "createTimeLineChart"
    case "1":
        month = Integer.parseInt(time.substring(5, 7));
        monthString = new DateFormatSymbols(Locale.US).getMonths()[month - 1];
        xAxisLabel = "" + monthString + "  " + time.substring(0, 4);
        break;

    case "2":
        xAxisLabel = time.substring(0, 4);
        break;

    case "3":
        xAxisLabel = "Years";
        break;

    default:
        xAxisLabel = "Timespan";
    }

    JFreeChart barChart = ChartFactory.createXYBarChart("Bar Chart", // title
            xAxisLabel, // x-axis label
            true, // date axis?
            //            "Energy Consumption "+("1".equals(unit)?"[kWh]":("2".equals(unit)?"[kWh/TNF]":("3".equals(unit)?"[TNF]":""))),       // y-axis label
            ("1".equals(unit) ? "Energy Consumption [kWh]"
                    : ("2".equals(unit) ? "Energy Consumption [kWh/TNF]"
                            : ("3".equals(unit) ? "Produced Pieces [TNF]" : ""))),
            collection, // data
            PlotOrientation.VERTICAL, // orientation
            true, // create legend?
            true, // generate tooltips?
            false // generate URLs?
    );

    //graphical modifications for BarChart
    barChart.setBackgroundPaint(Color.white);
    XYPlot plot = barChart.getXYPlot();
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0));

    //Axis modification: Set Axis X-Value directly below the bars
    DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
    dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

    //Axis modification: Remove Values from x-Axis that belong to former/later time element (Month/Day)
    dateAxis.setLowerMargin(0.01);
    dateAxis.setUpperMargin(0.01);

    //Axis modification: Axis values (depending on timeGranularity)
    if (timeGranularity.equals("1")) {
        dateAxis.setTickUnit(
                new DateTickUnit(DateTickUnitType.DAY, 2, new SimpleDateFormat("  dd.  ", Locale.US)));
    }
    if (timeGranularity.equals("2")) {
        dateAxis.setTickUnit(
                new DateTickUnit(DateTickUnitType.MONTH, 1, new SimpleDateFormat(" MMM ", Locale.US)));
    }
    if (timeGranularity.equals("3")) {
        dateAxis.setTickUnit(
                new DateTickUnit(DateTickUnitType.YEAR, 1, new SimpleDateFormat(" yyyy ", Locale.US)));
    }

    ClusteredXYBarRenderer clusteredxybarrenderer = new ClusteredXYBarRenderer(0.25, false);
    clusteredxybarrenderer.setShadowVisible(false);
    clusteredxybarrenderer.setBarPainter(new StandardXYBarPainter());
    plot.setRenderer(clusteredxybarrenderer);
    return barChart;

}

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

/**
 * Creates a chart./* w w  w  . j a va2  s .  co m*/
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart chart = ChartFactory.createTimeSeriesChart("Daylight Hours - London, UK", "Date", "Time",
            dataset, true, // legend
            true, // tool tips
            false // URLs
    );
    chart.setBackgroundPaint(Color.white);

    final XYDifferenceRenderer renderer = new XYDifferenceRenderer(Color.blue, Color.blue, false);
    renderer.setStroke(new BasicStroke(2.0f));
    renderer.setSeriesPaint(0, Color.yellow);
    renderer.setSeriesPaint(1, Color.red);
    final XYPlot plot = chart.getXYPlot();
    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    //        plot.setAxisOffset(new Spacer(Spacer.ABSOLUTE, 5.0, 5.0, 5.0, 5.0));

    final DateAxis domainAxis = new DateAxis("Time");
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    plot.setDomainAxis(domainAxis);
    plot.setForegroundAlpha(0.5f);

    final Color c = new Color(255, 60, 24, 63);
    final Marker bst = new IntervalMarker(new Day(28, 3, 2004).getFirstMillisecond(),
            new Day(30, 10, 2004).getFirstMillisecond(), c, new BasicStroke(2.0f), null, null, 1.0f);
    bst.setLabel("British Summer Time");
    bst.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
    bst.setLabelFont(new Font("SansSerif", Font.ITALIC + Font.BOLD, 10));
    bst.setLabelTextAnchor(TextAnchor.BASELINE_RIGHT);
    plot.addDomainMarker(bst, Layer.BACKGROUND);

    final DateAxis rangeAxis = new DateAxis("Time");
    rangeAxis.setLowerMargin(0.15);
    rangeAxis.setUpperMargin(0.15);
    plot.setRangeAxis(rangeAxis);
    return chart;
}

From source file:edu.ucla.stat.SOCR.chart.ChartGenerator_JTable.java

/**
 *
 *//*from  w ww. j av a 2 s  . c o  m*/
private void setDateAxis(XYPlot plot) {
    XYItemRenderer renderer = plot.getRenderer();
    StandardXYToolTipGenerator generator;
    if (timeType.equalsIgnoreCase("Year"))
        generator = new StandardXYToolTipGenerator("{1} = {2}", new SimpleDateFormat("yyyy"),
                new DecimalFormat("0"));
    else if (timeType.equalsIgnoreCase("Day"))
        generator = new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yy"), new DecimalFormat("#,##0.00"));
    else if (timeType.equalsIgnoreCase("Month"))
        generator = new StandardXYToolTipGenerator(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("MM-yy"), new DecimalFormat("#,##0.00"));
    else
        generator = new StandardXYToolTipGenerator("", DateFormat.getDateInstance(),
                new DecimalFormat("#,##0.00"));

    renderer.setBaseToolTipGenerator(generator);

    DateAxis domainAxis = new DateAxis("Time");
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    plot.setDomainAxis(domainAxis);
    //plot.setForegroundAlpha(0.5f);
}