How to display histogram in a Jpanel?

Asked

Viewed 49 times

1

The main program, whose code I didn’t put here, has a button that opens this frame, whose code is down here. I want to display a histogram within that frame.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;

public class CURVA extends JFrame {

    /**
     * Launch the application.
     */
    public static ChartPanel histograma()
    {
        int qt = 8;
        int vetor[] = new int[] {23,45,65,67,87,90,95,99};
        ChartPanel panel2;
        HistogramDataset dataset = new HistogramDataset();
        dataset.setType(HistogramType.FREQUENCY);
        double vetor3[]= new double[qt];

        for(int i = 0; i < qt; i++)
        {
            vetor3[i] = vetor[i];
        }

        dataset.addSeries("Fi",vetor3,3);      


        String plotTitle = "Histograma"; 
        String xaxis = "IC";
        String yaxis = "Fi"; 
        PlotOrientation orientation = PlotOrientation.VERTICAL; 
        boolean show = true; 
        boolean toolTips = true;
        boolean urls = false; 
        JFreeChart chart = ChartFactory.createHistogram( plotTitle, xaxis, yaxis, 
                dataset, orientation, show, toolTips, urls);

        XYPlot plot = (XYPlot) chart.getPlot();
        XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);

        panel2 = new ChartPanel(chart);
        panel2.setBounds(500, 10, 500, 500); 

        return panel2;

        /*panel.add(panel2);
        panel2.repaint();
   */
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CURVA frame = new CURVA();
                    frame.setVisible(true);

                    panel.add(histograma());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CURVA() {

        JPanel panel = new JPanel();
        panel.setBackground(new Color(0, 204, 51));
        panel.setBounds(0, 0, 552, 451);
        panel.getContentPane().add(panel);
        panel.setLayout(null);
    }

}

inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.