de.fub.maps.project.detector.model.statistics.BarChartPanel.java Source code

Java tutorial

Introduction

Here is the source code for de.fub.maps.project.detector.model.statistics.BarChartPanel.java

Source

/*
 * Copyright 2013 Serdar.
 *
 * 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 de.fub.maps.project.detector.model.statistics;

import de.fub.utilsmodule.text.CustomNumberFormat;
import java.awt.BorderLayout;
import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author Serdar
 */
public class BarChartPanel extends javax.swing.JPanel {

    private static final long serialVersionUID = 1L;
    private final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    private final JFreeChart barChart;
    private final CategoryPlot plot;
    private final ChartPanel chartPanel;

    /**
     * Creates new form StatisticSegmentLengthBarChart
     */
    public BarChartPanel() {
        initComponents();
        barChart = ChartFactory.createBarChart(null, null, null, dataset, PlotOrientation.VERTICAL, true, true,
                true);
        plot = barChart.getCategoryPlot();

        BarRenderer barRenderer = new BarRenderer();
        barRenderer.setMaximumBarWidth(.05);
        barRenderer.setBasePaint(Color.BLUE);
        barRenderer.setAutoPopulateSeriesPaint(false);
        barRenderer.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
        barRenderer.setBarPainter(new StandardBarPainter());
        barRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator(
                StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING, new CustomNumberFormat()));
        barRenderer.setBaseItemLabelsVisible(true);
        plot.setRenderer(barRenderer);
        plot.getDomainAxis().setMaximumCategoryLabelLines(3);
        plot.setBackgroundPaint(Color.white);
        barChart.setBackgroundPaint(Color.white);
        plot.setRangeGridlinesVisible(true);
        plot.getDomainAxis().setMaximumCategoryLabelLines(3);
        chartPanel = new ChartPanel(barChart, false);
        chartPanel.setVerticalAxisTrace(false);
        chartPanel.setDisplayToolTips(true);
        chartPanel.setBackground(Color.white);
        add(chartPanel, BorderLayout.CENTER);
    }

    public DefaultCategoryDataset getDataset() {
        return dataset;
    }

    public JFreeChart getBarChart() {
        return barChart;
    }

    public CategoryPlot getPlot() {
        return plot;
    }

    public ChartPanel getChartPanel() {
        return chartPanel;
    }

    public CategoryAxis getDomainAxis() {
        return plot.getDomainAxis();
    }

    public ValueAxis getRangeAxis() {
        return plot.getRangeAxis();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
     // Variables declaration - do not modify//GEN-BEGIN:variables
     // End of variables declaration//GEN-END:variables
}