Como fazer Graficos

4 respostas
edilsonon

Ola Galera,

Estou precisando fazer graficos de comparação de dados,

alguem poderia me ajudar, e como fazer um grafico no NetBeans

Abraços.

4 Respostas

rodrigo.bossini

Talvez o JFreeChart lhe sirva.

fabricioempresa

Segue um exemplo que eu fiz

package br.com.nautec.flowtracklite.util;

import br.com.nautec.flowtracklite.control.FileConfig;
import br.com.nautec.flowtracklite.model.dto.EventSensor;
import br.com.nautec.flowtracklite.model.exceptions.EventSensorDaoException;
import br.com.nautec.flowtracklite.model.factory.EventSensorDaoFactory;
import br.com.nautec.flowtracklite.view.ApplicationView;
import java.awt.Color;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class DualAxisDemo2 extends JFrame {

    public DualAxisDemo2(final String title) throws ParseException, EventSensorDaoException, Exception {

        super(title);
        final String chartTitle = "Volume por Sensor" + "\n" + ApplicationView.getInstance().getInicial().getText() + " a " + ApplicationView.getInstance().
                getFinal().getText();
        final XYDataset dataset = createDataset1();
        final JFreeChart chart = ChartFactory.createTimeSeriesChart(
                chartTitle,
                "Data",
                "Volume",
                dataset,
                true,
                true,
                false);
        final XYPlot plot = chart.getXYPlot();
        final XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        if (renderer instanceof StandardXYItemRenderer) {
            final StandardXYItemRenderer rr = (StandardXYItemRenderer) renderer;
            rr.setShapesFilled(true);
        }
        final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
        renderer2.setSeriesPaint(0, Color.BLACK);
        renderer.setToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance());
        plot.setRenderer(1, renderer2);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(650, 450));
        setContentPane(chartPanel);

    }

    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */
    private XYDataset createDataset1() throws ParseException, EventSensorDaoException, Exception {
        ApplicationView.getInstance().Pesqu();
        EventSensor[] es;
        float volume;
        String tmp = FileConfig.getInstance().getDateFormat();
        Date dt = new SimpleDateFormat(tmp).parse(ApplicationView.getInstance().getInicial().getText());
        Date dtf = new SimpleDateFormat(tmp).parse(ApplicationView.getInstance().getFinal().getText());
        long days = ((dtf.getTime() - dt.getTime()) + 3600000) / 86400000L; // 1 hora para compensar horário de verão
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        if (days > 30) {
            if (ApplicationView.getInstance().getSensors().getSelectedIndex() == 8) {
                for (int i = 101; i < 109; i++) {
                    es = EventSensorDaoFactory.create().findPulsebyDateMonth(i, dt, dtf);
                    final TimeSeries s1 = new TimeSeries(i, Month.class);
                    for (int j = 0; j < es.length; j++) {

                        String pulse = Integer.toString(es[j].getPulse());
                        if (es[j].getCorrection() > 0) {
                            volume = ((Float.parseFloat(pulse) / 6100) * es[j].getCorrection());
                            s1.add(new Month(es[j].getDateinc().getMonth() + 1, es[j].getDateinc().getYear() + 1900), volume);
                        } else {
                            volume = ((Float.parseFloat(pulse) / 6100));
                            s1.add(new Month(es[j].getDateinc().getMonth() + 1, es[j].getDateinc().getYear() + 1900), volume);
                        }
                    }
                    dataset.addSeries(s1);
                }
            } else {
               int[] x = ApplicationView.getInstance().getSensors().getSelectedIndices();
                for (int i = 0; i < x.length; i++) {
                    int sens = (x[i] + 101);
                    es = EventSensorDaoFactory.create().findPulsebyDateMonth(sens, dt, dtf);
                    final TimeSeries s1 = new TimeSeries(es[0].getDescription(), Month.class);
                    for (int j = 0; j < es.length; j++) {
                        String pulse = Integer.toString(es[j].getPulse());
                        if (es[j].getCorrection() > 0) {
                            volume = ((Float.parseFloat(pulse) / 6100) * es[j].getCorrection());
                            s1.add(new Month(es[j].getDateinc().getMonth() + 1, es[j].getDateinc().getYear() + 1900), volume);
                        } else {
                            volume = (Float.parseFloat(pulse) / 6100);
                            s1.add(new Month(es[j].getDateinc().getMonth() + 1, es[j].getDateinc().getYear() + 1900), volume);
                        }
                    }
                    dataset.addSeries(s1);
                }
            }
        } else if (days <= 30 && days > 1) {
           if (ApplicationView.getInstance().getSensors().getSelectedIndex() == 8) {
                for (int j = 101; j < 109; j++) {
                    es = EventSensorDaoFactory.create().findPulsebyDateDay(j, dt, dtf);
                    final TimeSeries s1 = new TimeSeries(j, Day.class);
                    for (int i = 0; i < es.length; i++) {
                        String pulse = Integer.toString(es[i].getPulse());
                        if (es[i].getCorrection() > 0) {
                            volume = ((Float.parseFloat(pulse) / 6100) * es[i].getCorrection());
                            s1.add(new Day(es[i].getDateinc().getDate(), es[i].getDateinc().getMonth() + 1, es[i].getDateinc().getYear() + 1900), volume);
                        } else {
                            volume = (Float.parseFloat(pulse) / 6100);
                            s1.add(new Day(es[i].getDateinc().getDate(), es[i].getDateinc().getMonth() + 1, es[i].getDateinc().getYear() + 1900), volume);
                        }
                    }
                    dataset.addSeries(s1);
                }
            } else {
              int[] x = ApplicationView.getInstance().getSensors().getSelectedIndices();
                for (int i = 0; i < x.length; i++) {
                    int sens = (x[i] + 101);
                    es = EventSensorDaoFactory.create().findPulsebyDateDay(sens, dt, dtf);
                    final TimeSeries s1 = new TimeSeries(sens, Day.class);
                    for (int j = 0; j < es.length; j++) {
                        String pulse = Integer.toString(es[j].getPulse());
                        if (es[j].getCorrection() > 0) {
                            volume = ((Float.parseFloat(pulse) / 6100) * es[j].getCorrection());
                            s1.add(new Day(es[j].getDateinc().getDate(), es[j].getDateinc().getMonth() + 1, es[j].getDateinc().getYear() + 1900), volume);
                        } else {
                            volume = (Float.parseFloat(pulse) / 6100);
                            s1.add(new Day(es[j].getDateinc().getDate(), es[j].getDateinc().getMonth() + 1, es[j].getDateinc().getYear() + 1900), volume);
                        }
                    }
                    dataset.addSeries(s1);
                }
            }
        }else {
           JOptionPane.showMessageDialog(null,"Não é possível fazer uma comparação entre as datas digitadas");
        }
        return dataset;
    }
}
edilsonon

Vlw, eu entendi o funcionamento, mais a minha dificuldade é em instalar o componente

alguem pode me ajudar a instalar ele no netbeans para que eu possa testalo,

já o baixei jFeeChart e ainda não consegui instalar e nem testar, porfavor alguem me ajude

pois estou precisando muito para adicionar para u meu cliente.

fabricioempresa

em biblioteca botao direito adicionar dependencia e ai em consulta escreve jfreechart vlw

Criado 7 de junho de 2010
Ultima resposta 8 de jun. de 2010
Respostas 4
Participantes 3