Search in sources :

Example 36 with DateRange

use of org.jfree.data.time.DateRange in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class DateAxis method valueToJava2D.

/**
 * Translates the data value to the display coordinates (Java 2D User Space)
 * of the chart.
 *
 * @param value  the date to be plotted.
 * @param area  the rectangle (in Java2D space) where the data is to be
 *              plotted.
 * @param edge  the axis location.
 *
 * @return The coordinate corresponding to the supplied data value.
 */
@Override
public double valueToJava2D(double value, Rectangle2D area, RectangleEdge edge) {
    value = this.timeline.toTimelineValue((long) value);
    DateRange range = (DateRange) getRange();
    double axisMin = this.timeline.toTimelineValue(range.getLowerMillis());
    double axisMax = this.timeline.toTimelineValue(range.getUpperMillis());
    double result = 0.0;
    if (RectangleEdge.isTopOrBottom(edge)) {
        double minX = area.getX();
        double maxX = area.getMaxX();
        if (isInverted()) {
            result = maxX + ((value - axisMin) / (axisMax - axisMin)) * (minX - maxX);
        } else {
            result = minX + ((value - axisMin) / (axisMax - axisMin)) * (maxX - minX);
        }
    } else if (RectangleEdge.isLeftOrRight(edge)) {
        double minY = area.getMinY();
        double maxY = area.getMaxY();
        if (isInverted()) {
            result = minY + (((value - axisMin) / (axisMax - axisMin)) * (maxY - minY));
        } else {
            result = maxY - (((value - axisMin) / (axisMax - axisMin)) * (maxY - minY));
        }
    }
    return result;
}
Also used : DateRange(org.jfree.data.time.DateRange)

Example 37 with DateRange

use of org.jfree.data.time.DateRange in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class DateAxisTest method testSetRange.

/**
 * Test that the setRange() method works.
 */
@Test
public void testSetRange() {
    DateAxis axis = new DateAxis("Test Axis");
    Calendar calendar = Calendar.getInstance();
    calendar.set(1999, Calendar.JANUARY, 3);
    Date d1 = calendar.getTime();
    calendar.set(1999, Calendar.JANUARY, 31);
    Date d2 = calendar.getTime();
    axis.setRange(d1, d2);
    DateRange range = (DateRange) axis.getRange();
    assertEquals(d1, range.getLowerDate());
    assertEquals(d2, range.getUpperDate());
}
Also used : DateRange(org.jfree.data.time.DateRange) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 38 with DateRange

use of org.jfree.data.time.DateRange in project ES-LEI-2Sem-2022-Grupo-1 by tmrbo-iscte.

the class PeriodAxisTest method test1932146.

/**
 * A test for bug 1932146.
 */
@Test
public void test1932146() {
    PeriodAxis axis = new PeriodAxis("TestAxis");
    axis.addChangeListener(this);
    this.lastEvent = null;
    axis.setRange(new DateRange(0L, 1000L));
    assertNotNull(this.lastEvent);
}
Also used : DateRange(org.jfree.data.time.DateRange) Test(org.junit.jupiter.api.Test)

Example 39 with DateRange

use of org.jfree.data.time.DateRange in project jpsonic by tesshucom.

the class StatusChartController method handleRequest.

// (Millisecond, Date) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@Override
@GetMapping
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String type = request.getParameter(Attributes.Request.TYPE.value());
    int index = ServletRequestUtils.getIntParameter(request, Attributes.Request.INDEX.value(), 0);
    List<TransferStatus> statuses = Collections.emptyList();
    if ("stream".equals(type)) {
        statuses = statusService.getAllStreamStatuses();
    } else if ("download".equals(type)) {
        statuses = statusService.getAllDownloadStatuses();
    } else if ("upload".equals(type)) {
        statuses = statusService.getAllUploadStatuses();
    }
    if (index < 0 || index >= statuses.size()) {
        return null;
    }
    TransferStatus status = statuses.get(index);
    TimeSeries series = new TimeSeries("Kbps");
    TransferStatus.SampleHistory history = status.getHistory();
    long to = System.currentTimeMillis();
    long from = to - status.getHistoryLengthMillis();
    if (!history.isEmpty()) {
        TransferStatus.Sample previous = history.get(0);
        for (int i = 1; i < history.size(); i++) {
            TransferStatus.Sample sample = history.get(i);
            long elapsedTimeMilis = sample.getTimestamp() - previous.getTimestamp();
            long bytesStreamed = Math.max(0L, sample.getBytesTransfered() - previous.getBytesTransfered());
            double kbps = (8.0 * bytesStreamed / 1024.0) / (elapsedTimeMilis / 1000.0);
            series.addOrUpdate(new Millisecond(new Date(sample.getTimestamp())), kbps);
            previous = sample;
        }
    }
    // Compute moving average.
    series = MovingAverage.createMovingAverage(series, "Kbps", 20_000, 5000);
    // Find min and max values.
    double min = 100;
    double max = 250;
    for (Object obj : series.getItems()) {
        TimeSeriesDataItem item = (TimeSeriesDataItem) obj;
        double value = item.getValue().doubleValue();
        if (item.getPeriod().getFirstMillisecond() > from) {
            min = Math.min(min, value);
            max = Math.max(max, value);
        }
    }
    // Add 10% to max value.
    max *= 1.1D;
    // Subtract 10% from min value.
    min *= 0.9D;
    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(series);
    JFreeChart chart = ChartFactory.createTimeSeriesChart(null, null, null, dataset, false, false, false);
    StandardChartTheme theme = (StandardChartTheme) StandardChartTheme.createJFreeTheme();
    Font font = fontLoader.getFont(12F);
    theme.setExtraLargeFont(font);
    theme.setLargeFont(font);
    theme.setRegularFont(font);
    theme.setSmallFont(font);
    theme.apply(chart);
    Color bgColor = getBackground(request);
    chart.setBackgroundPaint(bgColor);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(bgColor);
    Color fgColor = getForeground(request);
    plot.setOutlinePaint(fgColor);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    plot.setRangeGridlinePaint(fgColor);
    plot.setRangeGridlineStroke(new BasicStroke(0.2f));
    plot.setDomainGridlinePaint(fgColor);
    plot.setDomainGridlineStroke(new BasicStroke(0.2f));
    XYItemRenderer renderer = plot.getRendererForDataset(dataset);
    Color stColor = getStroke(request);
    renderer.setSeriesPaint(0, stColor);
    renderer.setSeriesStroke(0, new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setRange(new DateRange(from, to));
    domainAxis.setTickLabelPaint(fgColor);
    domainAxis.setTickMarkPaint(fgColor);
    domainAxis.setAxisLinePaint(fgColor);
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setRange(new Range(min, max));
    rangeAxis.setTickLabelPaint(fgColor);
    rangeAxis.setTickMarkPaint(fgColor);
    rangeAxis.setAxisLinePaint(fgColor);
    synchronized (LOCK) {
        ChartUtils.writeChartAsPNG(response.getOutputStream(), chart, IMAGE_WIDTH, IMAGE_HEIGHT);
    }
    return null;
}
Also used : BasicStroke(java.awt.BasicStroke) TimeSeries(org.jfree.data.time.TimeSeries) StandardChartTheme(org.jfree.chart.StandardChartTheme) Color(java.awt.Color) TimeSeriesDataItem(org.jfree.data.time.TimeSeriesDataItem) DateRange(org.jfree.data.time.DateRange) Range(org.jfree.data.Range) Date(java.util.Date) JFreeChart(org.jfree.chart.JFreeChart) Font(java.awt.Font) Millisecond(org.jfree.data.time.Millisecond) DateRange(org.jfree.data.time.DateRange) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) ValueAxis(org.jfree.chart.axis.ValueAxis) TransferStatus(com.tesshu.jpsonic.domain.TransferStatus) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

DateRange (org.jfree.data.time.DateRange)39 Date (java.util.Date)24 Range (org.jfree.data.Range)13 Font (java.awt.Font)7 FontMetrics (java.awt.FontMetrics)6 FontRenderContext (java.awt.font.FontRenderContext)6 LineMetrics (java.awt.font.LineMetrics)6 DateFormat (java.text.DateFormat)6 SimpleDateFormat (java.text.SimpleDateFormat)6 RectangleInsets (org.jfree.ui.RectangleInsets)4 Plot (org.jfree.chart.plot.Plot)3 ValueAxisPlot (org.jfree.chart.plot.ValueAxisPlot)3 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 RectangleInsets (org.jfree.chart.api.RectangleInsets)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 TransferStatus (com.tesshu.jpsonic.domain.TransferStatus)1 BoundedDateAxis (fr.jmmc.oiexplorer.core.gui.chart.BoundedDateAxis)1 BasicStroke (java.awt.BasicStroke)1