Search in sources :

Example 1 with Minute

use of org.jfree.data.time.Minute in project dubbo by alibaba.

the class SimpleMonitorService method createChart.

private static void createChart(String key, String service, String method, String date, String[] types, Map<String, long[]> data, double[] summary, String path) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
    DecimalFormat numberFormat = new DecimalFormat("###,##0.##");
    TimeSeriesCollection xydataset = new TimeSeriesCollection();
    for (int i = 0; i < types.length; i++) {
        String type = types[i];
        TimeSeries timeseries = new TimeSeries(type);
        for (Map.Entry<String, long[]> entry : data.entrySet()) {
            try {
                timeseries.add(new Minute(dateFormat.parse(date + entry.getKey())), entry.getValue()[i]);
            } catch (ParseException e) {
                logger.error(e.getMessage(), e);
            }
        }
        xydataset.addSeries(timeseries);
    }
    JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("max: " + numberFormat.format(summary[0]) + (summary[1] >= 0 ? " min: " + numberFormat.format(summary[1]) : "") + " avg: " + numberFormat.format(summary[2]) + (summary[3] >= 0 ? " sum: " + numberFormat.format(summary[3]) : ""), toDisplayService(service) + "  " + method + "  " + toDisplayDate(date), key, xydataset, true, true, false);
    jfreechart.setBackgroundPaint(Color.WHITE);
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setBackgroundPaint(Color.WHITE);
    xyplot.setDomainGridlinePaint(Color.GRAY);
    xyplot.setRangeGridlinePaint(Color.GRAY);
    xyplot.setDomainGridlinesVisible(true);
    xyplot.setRangeGridlinesVisible(true);
    DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
    dateaxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
    BufferedImage image = jfreechart.createBufferedImage(600, 300);
    try {
        if (logger.isInfoEnabled()) {
            logger.info("write chart: " + path);
        }
        File methodChartFile = new File(path);
        File methodChartDir = methodChartFile.getParentFile();
        if (methodChartDir != null && !methodChartDir.exists()) {
            methodChartDir.mkdirs();
        }
        FileOutputStream output = new FileOutputStream(methodChartFile);
        try {
            ImageIO.write(image, "png", output);
            output.flush();
        } finally {
            output.close();
        }
    } catch (IOException e) {
        logger.warn(e.getMessage(), e);
    }
}
Also used : DateAxis(org.jfree.chart.axis.DateAxis) TimeSeries(org.jfree.data.time.TimeSeries) DecimalFormat(java.text.DecimalFormat) IOException(java.io.IOException) JFreeChart(org.jfree.chart.JFreeChart) BufferedImage(java.awt.image.BufferedImage) Minute(org.jfree.data.time.Minute) XYPlot(org.jfree.chart.plot.XYPlot) TimeSeriesCollection(org.jfree.data.time.TimeSeriesCollection) FileOutputStream(java.io.FileOutputStream) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 DecimalFormat (java.text.DecimalFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JFreeChart (org.jfree.chart.JFreeChart)1 DateAxis (org.jfree.chart.axis.DateAxis)1 XYPlot (org.jfree.chart.plot.XYPlot)1 Minute (org.jfree.data.time.Minute)1 TimeSeries (org.jfree.data.time.TimeSeries)1 TimeSeriesCollection (org.jfree.data.time.TimeSeriesCollection)1