1
I am with the following doubt, how can I make my dynamic graph ? I have a graph, which is formed by two values, I want that according to the result any calculation, it changes/alters its composition.
As, example I made a simple calculation adding up 2 values. As an example, add only 10 + 10, because the other half of the graph is already fixed as 80, thus completing the 100%.
However, it does not change according to this result, it should be divided into a 20% share (result of the calculation) and 80% already defined. I want to know how I can pass this value correctly to the chart.
What I did:
package jfreechart;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;
public class GraficoTeste extends JFrame {
private static final long serialVersionUID = 1L;
private PieDataset dataset;
private JFreeChart grafico;
private final MeuTxt entrada1 = new MeuTxt();
private final MeuTxt entrada2 = new MeuTxt();
private final JButton calcular = new JButton("Calcular");
class MeuTxt extends JTextField {
public void setValor(Object valor) {
setText("" + valor);
}
public Object getValor() {
return "" + getText();
}
}
public GraficoTeste() {
setSize(500, 400);
JPanel contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
add(colocaCampos());
add(colocaGrafico());
acao();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void acao() {
calcular.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
calculo();
}
});
}
private int total;
public void calculo() {
int valor1 = Integer.parseInt(entrada1.getText());
int valor2 = Integer.parseInt(entrada2.getText());
total = (valor1 + valor2);
System.out.println(total);//ver se ele esta calculando.
}
public JComponent colocaCampos() {
JPanel painelCampo = new JPanel();
JLabel labelAltura = new JLabel("Altura:");
painelCampo.add(labelAltura);
painelCampo.add(entrada1);
entrada1.setPreferredSize(new Dimension(100, 25));
JLabel labelPeso = new JLabel("Peso:");
painelCampo.add(labelPeso);
painelCampo.add(entrada2);
entrada2.setPreferredSize(new Dimension(100, 25));
painelCampo.add(calcular);
calcular.setPreferredSize(new Dimension(100, 23));
return painelCampo;
}
public JComponent colocaGrafico() {
JPanel painelG = new JPanel();
//cria o dataset
dataset = criaDataset();
//baseando no dataset criamos o gráfico
grafico = criaGrafico(dataset, "");
//insere o painel no gráfico
ChartPanel painelGrafico = new ChartPanel(grafico);
//tamanho padrão
painelGrafico.setPreferredSize(new Dimension(400, 200));
painelG.add(painelGrafico);
return painelG;
}
//cria um simples dataset
private DefaultPieDataset criaDataset() {
DefaultPieDataset resultado = new DefaultPieDataset();
resultado.setValue("Vermelho", total);
resultado.setValue("Azul", 80);//valor para teste
return resultado;
}
//cria o gráfico
private JFreeChart criaGrafico(PieDataset dataset, String titulo) {
JFreeChart graf = ChartFactory.createPieChart3D(
titulo, dataset, true, true, false);
PiePlot3D enredo = (PiePlot3D) graf.getPlot();
enredo.setStartAngle(290);
enredo.setDirection(Rotation.CLOCKWISE);
enredo.setForegroundAlpha(0.5f);
return graf;
}
public static void main(String[] args) {
GraficoTeste t = new GraficoTeste();
t.setVisible(true);
}
}
Wouldn’t that be duplicate? https://answall.com/q/202022/28595
– user28595