I have a class that extends JPanel
for JFreeChart
. Inside of setMean()
, I tried updating values of dataset
or just the Function2D
, but nothing changes on the graph even with repaint()
.
public class JFreeChartPanel extends JPanel {
Function2D normal = new NormalDistributionFunction2D(0.0, 3.0);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, -5.0, 5.0, 100, "Normal");
double mean = 0.0, std = 1.0;
public double getMean() {
return mean;
}
public void setMean(double mean) {
this.mean = mean;
normal = new NormalDistributionFunction2D(mean,std);
dataset = DatasetUtilities.sampleFunction2D(normal, -5.0, 5.0, 100, "Normal");
repaint();
}
public double getStd() {
return std;
}
public void setStd(double std) {
this.std = std;
}
public JFreeChartPanel(){
JFreeChart chart = ChartFactory.createXYLineChart(
"Normal Distribution",
"X",
"Y",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
final ChartPanel chartPanel = new ChartPanel(chart);
setLayout(new BorderLayout());
add(chartPanel);
}
}
And this is executed everytime I change the value in my JTextField
.
public void updateMean()
{
String meanS = mean.getText();
double mean = 0.0;
try{
mean = Double.parseDouble(meanS);
System.out.println("Mean: "+mean);
jFreeChartPanel.setMean(mean);
}catch(Exception e){
System.out.println("Mean: incorrect input");
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…