Gostaria que o gráfico tivesse os valores sendo retirados de um jtable, porém me falta lógica pra eu conseguir fazer isso. Eu teria que converter data em String para adiciona-lo nas label’s do gráfico e posteriormente fazer uma soma de todas as vendas por mês, separando-as. Exemplo mês 01/2017 com R$ 3000, mês 02/2017 com R$ 4000 e por ai vai
Exemplo Minimo
import java.awt.Color;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.AbstractTableModel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class ExemploMinimo
{
public class Request implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Date registrationDate;
private String noteTotal;
public Date getRegistrationDate()
{
return registrationDate;
}
public void setRegistrationDate(Date registrationDate)
{
this.registrationDate = registrationDate;
}
public String getNoteTotal()
{
return noteTotal;
}
public void setNoteTotal(String noteTotal)
{
this.noteTotal = noteTotal;
}
}
// ----------------------------------------------------------------------------------------
public interface Dao<R>
{
public List<R> searchAll() throws Exception;
}
// ----------------------------------------------------------------------------------------
public class ModelTableRequestRoute extends AbstractTableModel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Request> request = new ArrayList<>();
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
public ModelTableRequestRoute(List<Request> list)
{
request = list;
}
@Override
public String getColumnName(int column)
{
switch (column)
{
case 0:
return "data";
case 1:
return "valor";
}
return super.getColumnName(column);
}
@Override
public int getColumnCount()
{
return 2;
}
@Override
public int getRowCount()
{
return request.size();
}
@Override
public Object getValueAt(int row, int column)
{
Request r = request.get(row);
switch (column)
{
case 0:
return dateFormat.format(r.getRegistrationDate());
case 1:
return r.getNoteTotal();
default:
return null;
}
}
}
// ----------------------------------------------------------------------------------------
public class FrameMinimo extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 4320341978109746731L;
private List<Request> requestList = new ArrayList<>();
private JPanel tablePanel;
private JPanel graphicPanel;
private Date currentDate = new Date();
private JTable table;
private JScrollPane scroll;
public FrameMinimo()
{
inicializarComponentes();
inicializarEventos();
Request r = new Request();
r.registrationDate = currentDate;
r.noteTotal = "3000.00";
requestList.add(r);
ContruirTabela();
construirGraficos();
}
private void inicializarComponentes()
{
setTitle("Exemplo Minimo");
setSize(1150, 637);
setLocationRelativeTo(null);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Table panel
tablePanel = new JPanel();
tablePanel.setSize(400, 402);
tablePanel.setLocation(3, 3);
tablePanel.setLayout(null);
tablePanel.setBackground(Color.WHITE);
tablePanel.setBorder(new LineBorder(Color.BLACK));
add(tablePanel);
// Table
table = new JTable();
scroll = new JScrollPane(table);
scroll.setFont(getFont());
scroll.setBorder(new LineBorder(Color.BLACK));
scroll.setLocation(3, 3);
scroll.setSize(394, 395);
scroll.getViewport().setBackground(Color.WHITE);
tablePanel.add(scroll);
// Graphic panel
graphicPanel = new JPanel();
graphicPanel.setSize(737, 603);
graphicPanel.setLocation(tablePanel.getX() + tablePanel.getWidth() + 1, 3);
graphicPanel.setLayout(null);
graphicPanel.setBackground(Color.WHITE);
graphicPanel.setBorder(new LineBorder(Color.BLACK));
add(graphicPanel);
}
private void ContruirTabela()
{
ModelTableRequestRoute modelTableRequestRoute = new ModelTableRequestRoute(requestList);
table.setModel(modelTableRequestRoute);
}
private void construirGraficos()
{
// FIXME Create data set with table values
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(40.5, "Venda", "01.2017"); // o valor de 40.5 deveria ser substituido pelo valor total calculado no mes
ds.addValue(20.5, "Venda", "02/2017"); // Onde esta "Venda" não precisa ser alterado
ds.addValue(40.5, "Venda", "03/2017"); // A data devera ser o mês e o ano em dois digitos
// Create chart
JFreeChart chart = ChartFactory.createLineChart("Vendas por Rota", "Mês", "Valor", ds, PlotOrientation.VERTICAL, true, true,
false);
// Chart properties
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinePaint(Color.GRAY);
ChartPanel cp = new ChartPanel(chart);
cp.setSize(730, 550);
cp.setLocation(3, 3);
cp.setVisible(true);
graphicPanel.add(cp);
graphicPanel.repaint();
}
private void inicializarEventos()
{
}
}
public static void main(String[] args)
{
ExemploMinimo e = new ExemploMinimo();
FrameMinimo fm = e.new FrameMinimo();
fm.setVisible(true);
}
}
