net.sf.profiler4j.console.AllocDiffPanel.java Source code

Java tutorial

Introduction

Here is the source code for net.sf.profiler4j.console.AllocDiffPanel.java

Source

/*
 * Copyright 2006 Antonio S. R. Gomes
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *      
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package net.sf.profiler4j.console;

import static javax.swing.BorderFactory.createCompoundBorder;
import static javax.swing.BorderFactory.createEmptyBorder;
import static javax.swing.BorderFactory.createLineBorder;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.text.SimpleDateFormat;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class AllocDiffPanel extends JPanel {

    private TimeSeries totalSeries;

    public AllocDiffPanel() {
        this(60000);
    }

    public AllocDiffPanel(int maxAgeMillis) {
        super(new BorderLayout());

        totalSeries = new TimeSeries("Allocs/Sec", Millisecond.class);
        totalSeries.setMaximumItemAge(maxAgeMillis);

        TimeSeriesCollection seriesCollection = new TimeSeriesCollection();
        seriesCollection.addSeries(totalSeries);

        NumberAxis numberAxis = new NumberAxis("Allocs/Sec");
        numberAxis.setLabelFont(new Font("SansSerif", 0, 14));
        numberAxis.setTickLabelFont(new Font("SansSerif", 0, 12));
        numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        DateAxis dateAxis = new DateAxis("Time");
        dateAxis.setTickLabelFont(new Font("SansSerif", 0, 12));
        dateAxis.setLabelFont(new Font("SansSerif", 0, 14));
        dateAxis.setAutoRange(true);
        dateAxis.setLowerMargin(0);
        dateAxis.setUpperMargin(0);
        dateAxis.setTickLabelsVisible(true);
        dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));

        XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, false);
        lineRenderer.setSeriesPaint(0, Color.RED);
        lineRenderer.setSeriesPaint(1, Color.GREEN.darker());
        lineRenderer.setStroke(new BasicStroke(2F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

        XYPlot xyplot = new XYPlot(seriesCollection, dateAxis, numberAxis, lineRenderer);
        xyplot.setBackgroundPaint(Color.WHITE);
        xyplot.setDomainGridlinePaint(Color.LIGHT_GRAY);
        xyplot.setRangeGridlinePaint(Color.LIGHT_GRAY);
        xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));

        JFreeChart chart = new JFreeChart("Object Allocactions in Remote JVM",
                new Font("SansSerif", Font.PLAIN, 18), xyplot, true);
        chart.setBackgroundPaint(Color.white);

        ChartPanel panel = new ChartPanel(chart);
        panel.setBorder(createCompoundBorder(createEmptyBorder(8, 8, 8, 8), createLineBorder(Color.LIGHT_GRAY)));
        add(panel);
        setBorder(createEmptyBorder(8, 8, 8, 8));
    }

    public void addSample(Millisecond m, double allocDiff) {
        totalSeries.add(m, allocDiff);
    }

    public void reset() {
        totalSeries.clear();
    }

}