Search in sources :

Example 26 with JFreeChart

use of org.jfree.chart.JFreeChart in project libresonic by Libresonic.

the class UserChartController method createChart.

private JFreeChart createChart(CategoryDataset dataset, HttpServletRequest request) {
    JFreeChart chart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.HORIZONTAL, false, false, false);
    CategoryPlot plot = chart.getCategoryPlot();
    Paint background = new GradientPaint(0, 0, Color.lightGray, 0, IMAGE_MIN_HEIGHT, Color.white);
    plot.setBackgroundPaint(background);
    plot.setDomainGridlinePaint(Color.white);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.white);
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    LogarithmicAxis rangeAxis = new LogarithmicAxis(null);
    rangeAxis.setStrictValuesFlag(false);
    rangeAxis.setAllowNegativesFlag(true);
    plot.setRangeAxis(rangeAxis);
    // Disable bar outlines.
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);
    // Set up gradient paint for series.
    GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64));
    renderer.setSeriesPaint(0, gp0);
    // Rotate labels.
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
    // Set theme-specific colors.
    Color bgColor = getBackground(request);
    Color fgColor = getForeground(request);
    chart.setBackgroundPaint(bgColor);
    domainAxis.setTickLabelPaint(fgColor);
    domainAxis.setTickMarkPaint(fgColor);
    domainAxis.setAxisLinePaint(fgColor);
    rangeAxis.setTickLabelPaint(fgColor);
    rangeAxis.setTickMarkPaint(fgColor);
    rangeAxis.setAxisLinePaint(fgColor);
    return chart;
}
Also used : CategoryAxis(org.jfree.chart.axis.CategoryAxis) BarRenderer(org.jfree.chart.renderer.category.BarRenderer) JFreeChart(org.jfree.chart.JFreeChart) CategoryPlot(org.jfree.chart.plot.CategoryPlot) LogarithmicAxis(org.jfree.chart.axis.LogarithmicAxis)

Example 27 with JFreeChart

use of org.jfree.chart.JFreeChart in project libresonic by Libresonic.

the class StatusChartController method handleRequest.

@RequestMapping(method = RequestMethod.GET)
public synchronized ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String type = request.getParameter("type");
    int index = Integer.parseInt(request.getParameter("index"));
    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", Millisecond.class);
    TransferStatus.SampleHistory history = status.getHistory();
    long to = System.currentTimeMillis();
    long from = to - status.getHistoryLengthMillis();
    Range range = new DateRange(from, to);
    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", 20000, 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);
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
    Paint background = new GradientPaint(0, 0, Color.lightGray, 0, IMAGE_HEIGHT, Color.white);
    plot.setBackgroundPaint(background);
    XYItemRenderer renderer = plot.getRendererForDataset(dataset);
    renderer.setSeriesPaint(0, Color.blue.darker());
    renderer.setSeriesStroke(0, new BasicStroke(2f));
    // Set theme-specific colors.
    Color bgColor = getBackground(request);
    Color fgColor = getForeground(request);
    chart.setBackgroundPaint(bgColor);
    ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setRange(range);
    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);
    ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, IMAGE_WIDTH, IMAGE_HEIGHT);
    return null;
}
Also used : Range(org.jfree.data.Range) Date(java.util.Date) JFreeChart(org.jfree.chart.JFreeChart) XYPlot(org.jfree.chart.plot.XYPlot) ValueAxis(org.jfree.chart.axis.ValueAxis) TransferStatus(org.libresonic.player.domain.TransferStatus) XYItemRenderer(org.jfree.chart.renderer.xy.XYItemRenderer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getChartAsBufferedImage.

/**
     * Helper method used to return a JFreeChart as a buffered Image.
     *
     * @param chartName a {@link java.lang.String} object.
     * @return a <code>BufferedImage</code>
     * @throws java.io.IOException if any.
     * @throws java.sql.SQLException if any.
     */
public static BufferedImage getChartAsBufferedImage(String chartName) throws IOException, SQLException {
    BarChart chartConfig = getBarChartConfigByName(chartName);
    JFreeChart chart = getBarChart(chartName);
    ImageSize imageSize = chartConfig.getImageSize();
    int hzPixels;
    int vtPixels;
    if (imageSize == null) {
        hzPixels = 400;
        vtPixels = 400;
    } else {
        hzPixels = imageSize.getHzSize().getPixels();
        vtPixels = imageSize.getVtSize().getPixels();
    }
    return chart.createBufferedImage(hzPixels, vtPixels);
}
Also used : ImageSize(org.opennms.netmgt.config.charts.ImageSize) BarChart(org.opennms.netmgt.config.charts.BarChart) JFreeChart(org.jfree.chart.JFreeChart) Paint(java.awt.Paint)

Example 29 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getBarChart.

/**
     * This method will returns a JFreeChart bar chart constructed based on XML configuration.
     *
     * @param chartName Name specified in chart-configuration.xml
     * @return <code>JFreeChart</code> constructed from the chartName
     * @throws java.io.IOException if any.
     * @throws java.sql.SQLException if any.
     */
public static JFreeChart getBarChart(String chartName) throws IOException, SQLException {
    //ChartConfigFactory.reload();
    BarChart chartConfig = null;
    chartConfig = getBarChartConfigByName(chartName);
    if (chartConfig == null) {
        throw new IllegalArgumentException("getBarChart: Invalid chart name.");
    }
    DefaultCategoryDataset baseDataSet = buildCategoryDataSet(chartConfig);
    JFreeChart barChart = createBarChart(chartConfig, baseDataSet);
    addSubTitles(chartConfig, barChart);
    if (chartConfig.getSubLabelClass().isPresent()) {
        addSubLabels(barChart, chartConfig.getSubLabelClass().get());
    }
    customizeSeries(barChart, chartConfig);
    return barChart;
}
Also used : BarChart(org.opennms.netmgt.config.charts.BarChart) DefaultCategoryDataset(org.jfree.data.category.DefaultCategoryDataset) JFreeChart(org.jfree.chart.JFreeChart)

Example 30 with JFreeChart

use of org.jfree.chart.JFreeChart in project opennms by OpenNMS.

the class ChartUtils method getBarChart.

/**
     * Helper method that returns the JFreeChart to an output stream written in JPEG format.
     *
     * @param chartName a {@link java.lang.String} object.
     * @param out a {@link java.io.OutputStream} object.
     * @throws java.io.IOException if any.
     * @throws java.sql.SQLException if any.
     */
public static void getBarChart(String chartName, OutputStream out) throws IOException, SQLException {
    BarChart chartConfig = getBarChartConfigByName(chartName);
    JFreeChart chart = getBarChart(chartName);
    ImageSize imageSize = chartConfig.getImageSize();
    int hzPixels;
    int vtPixels;
    if (imageSize == null) {
        hzPixels = 400;
        vtPixels = 400;
    } else {
        hzPixels = imageSize.getHzSize().getPixels();
        vtPixels = imageSize.getVtSize().getPixels();
    }
    ChartUtilities.writeChartAsJPEG(out, chart, hzPixels, vtPixels);
}
Also used : ImageSize(org.opennms.netmgt.config.charts.ImageSize) BarChart(org.opennms.netmgt.config.charts.BarChart) JFreeChart(org.jfree.chart.JFreeChart) Paint(java.awt.Paint)

Aggregations

JFreeChart (org.jfree.chart.JFreeChart)175 XYPlot (org.jfree.chart.plot.XYPlot)38 NumberAxis (org.jfree.chart.axis.NumberAxis)24 Color (java.awt.Color)22 DateAxis (org.jfree.chart.axis.DateAxis)21 DecimalFormat (java.text.DecimalFormat)18 ChartPanel (org.jfree.chart.ChartPanel)17 XYSeries (org.jfree.data.xy.XYSeries)17 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)17 RectangleInsets (org.jfree.ui.RectangleInsets)17 NumberFormat (java.text.NumberFormat)16 CategoryAxis (org.jfree.chart.axis.CategoryAxis)16 CategoryPlot (org.jfree.chart.plot.CategoryPlot)16 DefaultCategoryDataset (org.jfree.data.category.DefaultCategoryDataset)13 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)13 HashMap (java.util.HashMap)12 CategoryDataset (org.jfree.data.category.CategoryDataset)11 Paint (java.awt.Paint)10 ValueAxis (org.jfree.chart.axis.ValueAxis)10 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)10