Search in sources :

Example 31 with DateAxis

use of org.jfree.chart.axis.DateAxis in project AndrOBD by fr3ts0n.

the class ObdDataPlotter method createChart.

/**
 * Creates a chart.
 *
 * @param dataset a dataset.
 * @return A chart.
 */
private JFreeChart createChart(XYDataset dataset) {
    chart = ChartFactory.createTimeSeriesChart(// title
    "OBD Data Graph", // x-axis label
    "Time", // y-axis label
    "Value", // data
    dataset, // create legend?
    true, // generate tooltips?
    true, // generate URLs?
    false);
    chart.setBackgroundPaint(Color.white);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setDomainCrosshairVisible(true);
    plot.setRangeCrosshairVisible(true);
    plot.getDomainAxis().setTickLabelFont(legendFont);
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));
    chart.getLegend().setItemFont(legendFont);
    return chart;
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) XYPlot(org.jfree.chart.plot.XYPlot) SimpleDateFormat(java.text.SimpleDateFormat)

Example 32 with DateAxis

use of org.jfree.chart.axis.DateAxis in project spf4j by zolyfarkas.

the class Charts method createJFreeChart.

private static JFreeChart createJFreeChart(final String chartName, final String uom, final XYDataset timeseriescollection) {
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(chartName, "Time", uom, timeseriescollection, true, true, false);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
    dateaxis.setVerticalTickLabels(true);
    XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
    xylineandshaperenderer.setBaseShapesVisible(true);
    xylineandshaperenderer.setUseFillPaint(true);
    xylineandshaperenderer.setLegendItemToolTipGenerator(new StandardXYSeriesLabelGenerator("Tooltip {0}"));
    return jfreechart;
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) XYPlot(org.jfree.chart.plot.XYPlot) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) StandardXYSeriesLabelGenerator(org.jfree.chart.labels.StandardXYSeriesLabelGenerator) JFreeChart(org.jfree.chart.JFreeChart)

Example 33 with DateAxis

use of org.jfree.chart.axis.DateAxis in project xwiki-platform by xwiki.

the class AxisConfigurator method setAxes.

/**
 * Set the axes in the chart model.
 *
 * @param plotType The target plot type.
 * @param chartModel The target chart model.
 * @throws MacroExecutionException if the axes are incorrectly configured.
 */
public void setAxes(PlotType plotType, SimpleChartModel chartModel) throws MacroExecutionException {
    AxisType[] defaultAxisTypes = plotType.getDefaultAxisTypes();
    for (int i = 0; i < axisTypes.length; i++) {
        AxisType type = axisTypes[i];
        if (i >= defaultAxisTypes.length) {
            if (type != null) {
                throw new MacroExecutionException("To many axes for plot type.");
            }
            continue;
        }
        if (type == null) {
            type = defaultAxisTypes[i];
        }
        Axis axis;
        switch(type) {
            case NUMBER:
                NumberAxis numberAxis = new NumberAxis();
                axis = numberAxis;
                setNumberLimits(numberAxis, i);
                break;
            case CATEGORY:
                axis = new CategoryAxis();
                break;
            case DATE:
                DateAxis dateAxis = new DateAxis();
                axis = dateAxis;
                dateAxis.setTickMarkPosition(DateTickMarkPosition.END);
                if (axisDateFormat[i] != null) {
                    try {
                        DateFormat dateFormat = new SimpleDateFormat(axisDateFormat[i], localeConfiguration.getLocale());
                        dateAxis.setDateFormatOverride(dateFormat);
                    } catch (IllegalArgumentException e) {
                        throw new MacroExecutionException(String.format("Invalid date format [%s].", axisDateFormat[i]));
                    }
                }
                setDateLimits(dateAxis, i);
                break;
            default:
                throw new MacroExecutionException(String.format("Unsupported axis type [%s]", type.getName()));
        }
        chartModel.setAxis(i, axis);
    }
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) CategoryAxis(org.jfree.chart.axis.CategoryAxis) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) AxisType(org.xwiki.chart.axis.AxisType) SimpleDateFormat(java.text.SimpleDateFormat) ValueAxis(org.jfree.chart.axis.ValueAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) DateAxis(org.jfree.chart.axis.DateAxis) Axis(org.jfree.chart.axis.Axis) CategoryAxis(org.jfree.chart.axis.CategoryAxis)

Example 34 with DateAxis

use of org.jfree.chart.axis.DateAxis in project ma-core-public by infiniteautomation.

the class ImageChartUtils method writeChart.

public static void writeChart(PointTimeSeriesCollection pointTimeSeriesCollection, boolean showLegend, OutputStream out, int width, int height, long from, long to) throws IOException {
    JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, null, showLegend, false, false);
    chart.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.CHART_BACKGROUND_COLOUR));
    XYPlot plot = chart.getXYPlot();
    ((DateAxis) plot.getDomainAxis()).setTimeZone(pointTimeSeriesCollection.getTimeZone());
    plot.setBackgroundPaint(SystemSettingsDao.getColour(SystemSettingsDao.PLOT_BACKGROUND_COLOUR));
    Color gridlines = SystemSettingsDao.getColour(SystemSettingsDao.PLOT_GRIDLINE_COLOUR);
    plot.setDomainGridlinePaint(gridlines);
    plot.setRangeGridlinePaint(gridlines);
    ((NumberAxis) plot.getRangeAxis()).setAutoRangeStickyZero(false);
    double numericMin = 0;
    double numericMax = 1;
    int numericSeriesCount = pointTimeSeriesCollection.getNumericSeriesCount();
    if (pointTimeSeriesCollection.hasNumericData()) {
        for (int i = 0; i < numericSeriesCount; i++) {
            NumericTimeSeries nts = pointTimeSeriesCollection.getNumericTimeSeries(i);
            AbstractXYItemRenderer renderer;
            if (nts.getPlotType() == DataPointVO.PlotTypes.STEP)
                renderer = new XYStepRenderer();
            else if (nts.getPlotType() == DataPointVO.PlotTypes.LINE)
                renderer = new XYLineAndShapeRenderer(true, false);
            else {
                // XYCardinalSplineRenderer spline = new XYCardinalSplineRenderer(.5d, 16);
                // XYSmoothLineAndShapeRenderer spline = new XYSmoothLineAndShapeRenderer();
                // XYSplineRenderer spline = new XYSplineRenderer();
                // spline.setBaseShapesVisible(false);
                // renderer = spline;
                renderer = new XYLineAndShapeRenderer(true, false);
            }
            if (nts.getPaint() != null)
                renderer.setSeriesPaint(0, nts.getPaint(), false);
            if (nts.getStroke() != null)
                renderer.setSeriesStroke(0, nts.getStroke(), false);
            plot.setDataset(i, new TimeSeriesCollection(nts.getTimeSeries()));
            plot.setRenderer(i, renderer);
        }
        numericMin = plot.getRangeAxis().getLowerBound();
        numericMax = plot.getRangeAxis().getUpperBound();
        if (!pointTimeSeriesCollection.hasMultiplePoints()) {
            // If this chart displays a single point, check if there should be a range description.
            TimeSeries timeSeries = pointTimeSeriesCollection.getNumericTimeSeries(0).getTimeSeries();
            String desc = timeSeries.getRangeDescription();
            if (!StringUtils.isBlank(desc)) {
                // Replace any HTML entities with Java equivalents
                desc = StripEntities.stripHTMLEntities(desc, ' ');
                plot.getRangeAxis().setLabel(desc);
            }
        }
    } else
        plot.getRangeAxis().setVisible(false);
    if (pointTimeSeriesCollection.getRangeMarkers() != null) {
        boolean rangeAdjusted = false;
        for (Marker marker : pointTimeSeriesCollection.getRangeMarkers()) {
            plot.addRangeMarker(marker);
            if (marker instanceof ValueMarker) {
                ValueMarker vm = (ValueMarker) marker;
                if (numericMin > vm.getValue()) {
                    numericMin = vm.getValue();
                    rangeAdjusted = true;
                }
                if (numericMax < vm.getValue()) {
                    numericMax = vm.getValue();
                    rangeAdjusted = true;
                }
            }
        }
        if (rangeAdjusted) {
            double adj = (numericMax - numericMin);
            plot.getRangeAxis().setLowerBound(numericMin - adj * plot.getRangeAxis().getLowerMargin());
            plot.getRangeAxis().setUpperBound(numericMax + adj * plot.getRangeAxis().getUpperMargin());
        }
    }
    int discreteValueCount = pointTimeSeriesCollection.getDiscreteValueCount();
    double interval = (numericMax - numericMin) / (discreteValueCount + 1);
    int intervalIndex = 1;
    if (pointTimeSeriesCollection.hasDiscreteData()) {
        for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
            DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
            XYStepRenderer renderer = new XYStepRenderer();
            TimeSeries ts = new TimeSeries(dts.getName(), null, null);
            for (IValueTime vt : dts.getValueTimes()) addMillisecond(ts, vt.getTime(), numericMin + (interval * (dts.getValueIndex(vt.getValue()) + intervalIndex)));
            if (dts.getPaint() != null)
                renderer.setSeriesPaint(0, dts.getPaint(), false);
            if (dts.getStroke() != null)
                renderer.setSeriesStroke(0, dts.getStroke(), false);
            plot.setDataset(numericSeriesCount + i, new TimeSeriesCollection(ts, pointTimeSeriesCollection.getTimeZone()));
            plot.setRenderer(numericSeriesCount + i, renderer);
            intervalIndex += dts.getDiscreteValueCount();
        }
    }
    if (from > 0)
        plot.getDomainAxis().setLowerBound(from);
    if (to > 0)
        plot.getDomainAxis().setUpperBound(to);
    if (pointTimeSeriesCollection.hasDiscreteData()) {
        // Add the value annotations.
        double annoX = plot.getDomainAxis().getLowerBound();
        intervalIndex = 1;
        for (int i = 0; i < pointTimeSeriesCollection.getDiscreteSeriesCount(); i++) {
            DiscreteTimeSeries dts = pointTimeSeriesCollection.getDiscreteTimeSeries(i);
            for (int j = 0; j < dts.getDiscreteValueCount(); j++) {
                XYTextAnnotation anno = new XYTextAnnotation(" " + dts.getValueText(j), annoX, numericMin + (interval * (j + intervalIndex)));
                if (!pointTimeSeriesCollection.hasNumericData() && intervalIndex + j == discreteValueCount)
                    // This prevents the top label from getting cut off
                    anno.setTextAnchor(TextAnchor.TOP_LEFT);
                else
                    anno.setTextAnchor(TextAnchor.BOTTOM_LEFT);
                anno.setPaint(((AbstractRenderer) plot.getRenderer(numericSeriesCount + i)).lookupSeriesPaint(0));
                plot.addAnnotation(anno);
            }
            intervalIndex += dts.getDiscreteValueCount();
        }
    }
    // Return the image.
    ChartUtilities.writeChartAsPNG(out, chart, width, height);
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) NumberAxis(org.jfree.chart.axis.NumberAxis) TimeSeries(org.jfree.data.time.TimeSeries) Color(java.awt.Color) AbstractXYItemRenderer(org.jfree.chart.renderer.xy.AbstractXYItemRenderer) XYLineAndShapeRenderer(org.jfree.chart.renderer.xy.XYLineAndShapeRenderer) IValueTime(com.serotonin.m2m2.view.stats.IValueTime) ValueMarker(org.jfree.chart.plot.ValueMarker) Marker(org.jfree.chart.plot.Marker) JFreeChart(org.jfree.chart.JFreeChart) XYTextAnnotation(org.jfree.chart.annotations.XYTextAnnotation) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) XYStepRenderer(org.jfree.chart.renderer.xy.XYStepRenderer) ValueMarker(org.jfree.chart.plot.ValueMarker)

Example 35 with DateAxis

use of org.jfree.chart.axis.DateAxis in project nimbus by nimbus-org.

the class DateAxisTickUnitAdjusterService method adjust.

// AbstractTickUnitAdjusterServiceのJavaDoc
protected void adjust(ValueAxis axis) {
    if (!(axis instanceof DateAxis)) {
        throw new IllegalArgumentException("axis is not DateAxis.");
    }
    DateAxis dateAxis = (DateAxis) axis;
    DateTickUnit orgTickUnit = dateAxis.getTickUnit();
    int unit = orgTickUnit.getUnit();
    if (autoRangeMinimumSizeEnabled) {
        double autoRangeMinimumSize = Double.NaN;
        switch(unit) {
            case DateTickUnit.MILLISECOND:
                autoRangeMinimumSize = 1d;
                break;
            case DateTickUnit.SECOND:
                autoRangeMinimumSize = 1000d;
                break;
            case DateTickUnit.MINUTE:
                autoRangeMinimumSize = 60d * 1000d;
                break;
            case DateTickUnit.HOUR:
                autoRangeMinimumSize = 60d * 60d * 1000d;
                break;
            case DateTickUnit.DAY:
                autoRangeMinimumSize = 24d * 60d * 60d * 1000d;
                break;
            case DateTickUnit.MONTH:
                autoRangeMinimumSize = 31d * 24d * 60d * 60d * 1000d;
                break;
            case DateTickUnit.YEAR:
                autoRangeMinimumSize = 365d * 24d * 60d * 60d * 1000d;
                break;
            default:
                break;
        }
        if (!Double.isNaN(autoRangeMinimumSize)) {
            Plot plot = dateAxis.getPlot();
            if (plot instanceof ValueAxisPlot) {
                ValueAxisPlot vap = (ValueAxisPlot) plot;
                Range r = vap.getDataRange(dateAxis);
                if (r == null) {
                    if (zeroLength > 0.0d) {
                        dateAxis.setAutoRangeMinimumSize(zeroLength);
                    }
                } else {
                    dateAxis.setAutoRangeMinimumSize(autoRangeMinimumSize * (Double.isNaN(unitCountCommonDivisor) ? 1.0d : unitCountCommonDivisor) * 2);
                }
            } else {
                dateAxis.setAutoRangeMinimumSize(autoRangeMinimumSize * (Double.isNaN(unitCountCommonDivisor) ? 1.0d : unitCountCommonDivisor) * 2);
            }
        }
    }
    double length = dateAxis.getRange().getLength();
    // ミリ秒以外は計算
    switch(unit) {
        case DateTickUnit.SECOND:
            // 秒
            length = length / 1000d;
            break;
        case DateTickUnit.MINUTE:
            // 分
            length = length / (60d * 1000d);
            break;
        case DateTickUnit.HOUR:
            // 時
            length = length / (60d * 60d * 1000d);
            break;
        case DateTickUnit.DAY:
            // 日
            length = length / (24d * 60d * 60d * 1000d);
            break;
        case DateTickUnit.MONTH:
            // 月
            length = length / (28d * 24d * 60d * 60d * 1000d);
            break;
        case DateTickUnit.YEAR:
            // 年
            length = length / (365d * 24d * 60d * 60d * 1000d);
            break;
        default:
            break;
    }
    double unitCount = length / displayGraduationCount;
    if (unitCount <= 0d) {
        // 1ずつ表示
        unitCount = 1d;
    } else {
        unitCount = adjustUnitCountByCommonDivisor(axis, unitCount);
    }
    DateFormat newFormat = null;
    if (format != null) {
        newFormat = format;
    } else {
        newFormat = new SimpleDateFormat();
    }
    int newUnitCount = (int) Math.ceil(unitCount);
    // 新しいTickUnitを設定
    dateAxis.setTickUnit(new DateTickUnit(unit, newUnitCount, newFormat));
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) DateTickUnit(org.jfree.chart.axis.DateTickUnit) Plot(org.jfree.chart.plot.Plot) ValueAxisPlot(org.jfree.chart.plot.ValueAxisPlot) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ValueAxisPlot(org.jfree.chart.plot.ValueAxisPlot) Range(org.jfree.data.Range) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

DateAxis (org.jfree.chart.axis.DateAxis)39 XYPlot (org.jfree.chart.plot.XYPlot)33 JFreeChart (org.jfree.chart.JFreeChart)30 SimpleDateFormat (java.text.SimpleDateFormat)21 NumberAxis (org.jfree.chart.axis.NumberAxis)18 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)13 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)13 Color (java.awt.Color)10 DateFormat (java.text.DateFormat)8 Date (java.util.Date)7 Map (java.util.Map)7 ValueAxis (org.jfree.chart.axis.ValueAxis)7 StandardXYToolTipGenerator (org.jfree.chart.labels.StandardXYToolTipGenerator)7 BasicStroke (java.awt.BasicStroke)5 TreeMap (java.util.TreeMap)5 DateTickUnit (org.jfree.chart.axis.DateTickUnit)5 TimeSeries (org.jfree.data.time.TimeSeries)5 ChartCompositePart (com.cubrid.cubridmanager.ui.monitoring.editor.internal.ChartCompositePart)4 HistoryComposite (com.cubrid.cubridmanager.ui.monitoring.editor.internal.HistoryComposite)4 ShowSetting (com.cubrid.cubridmanager.ui.monitoring.editor.internal.ShowSetting)4