图表未显示

Graph is not showing

我有一个程序可以将数据输入 table,计算其他数据,然后绘制图表。 table 和数据显示正确,但折线图不显示任何内容。我正在使用 JavaFXML 应用程序和场景生成器。

PS:我是 JAVA 的新人。

控制器:

package minpro1;

//import java.net.URL;
import java.text.DecimalFormat;
//import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
//import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class minpro1Controller {

double s1, s2, s3, s4, s5, s6, s7, s8, s9, w1, w2, w3, w4, w5, w6, w7, w8, pan, wp1, wp2, wp3, wp4, wp5, wp6, wp7, wp8,wp9, p80, totalweight, wptotal;
double os1, os2, os3, os4, os5, os6, os7, os8, os9, us1, us2, us3, us4, us5, us6, us7, us8, us9;

XYChart.Series graph = new XYChart.Series();

@FXML
private TextField P80;

@FXML
private TextField S3;

@FXML
private TextField W7;

@FXML
private TextField S4;

@FXML
private TextField W8;

@FXML
private TextField S5;

@FXML
private TextField S6;

@FXML
private TextField S7;

@FXML
private TextField S8;

@FXML
private TextField OS1;

@FXML
private NumberAxis yaxis;

@FXML
private TextField OS3;

@FXML
private TextField OS2;

@FXML
private TextField TOTALWEIGHT;

@FXML
private TextField OS5;

@FXML
private TextField OS4;

@FXML
private NumberAxis xaxis;

@FXML
private TextField OS7;

@FXML
private TextField US1;

@FXML
private TextField OS6;

@FXML
private TextField US3;

@FXML
private TextField OS8;

@FXML
private TextField OS9;

@FXML
private TextField US2;

@FXML
private TextField US5;

@FXML
private TextField US4;

@FXML
private TextField US7;

@FXML
private TextField US6;

@FXML
private TextField US8;

@FXML
private TextField US9;

@FXML
private TextField TOTALWP;

@FXML
private TextField WP2;

@FXML
private TextField WP1;

@FXML
private TextField W1;

@FXML
private TextField WP4;

@FXML
private TextField W2;

@FXML
private TextField WP3;

@FXML
private TextField W3;

@FXML
private TextField WP6;

@FXML
private TextField PAN;

@FXML
private TextField W4;

@FXML
private TextField WP5;

@FXML
private TextField S1;

@FXML
private TextField W5;

@FXML
private TextField WP8;

@FXML
private TextField WP9;

@FXML
private TextField S2;

@FXML
private TextField W6;

@FXML
private TextField WP7;

@FXML
private LineChart<Number,Number> GRAPH;


@FXML
private Button BUTTON;

@FXML
void handleButtonAction(ActionEvent event) 

{
    s1 = Double.parseDouble(S1.getText());
    s2 = Double.parseDouble(S2.getText());
    s3 = Double.parseDouble(S3.getText());
    s4 = Double.parseDouble(S4.getText());
    s5 = Double.parseDouble(S5.getText());
    s6 = Double.parseDouble(S6.getText());
    s7 = Double.parseDouble(S7.getText());
    s8 = Double.parseDouble(S8.getText());
    s9 = -37;

    w1 = Double.parseDouble(W1.getText());
    w2 = Double.parseDouble(W2.getText());
    w3 = Double.parseDouble(W3.getText());
    w4 = Double.parseDouble(W4.getText());
    w5 = Double.parseDouble(W5.getText());
    w6 = Double.parseDouble(W6.getText());
    w7 = Double.parseDouble(W7.getText());
    w8 = Double.parseDouble(W8.getText());

    pan = Double.parseDouble(PAN.getText());

    double s[]={s9, s8, s7, s6, s5, s4, s3, s2, s1};
    double w[]={pan, w1, w2, w3, w4, w5, w6, w7, w8};


    totalweight=totalweightcalculations(w, pan);

    wp1=(w1/totalweight)*100;
    wp2=(w2/totalweight)*100;
    wp3=(w3/totalweight)*100;
    wp4=(w4/totalweight)*100;
    wp5=(w5/totalweight)*100;
    wp6=(w6/totalweight)*100;
    wp7=(w7/totalweight)*100;
    wp8=(w8/totalweight)*100;
    wp9=(pan/totalweight)*100;

    wptotal=wp1+wp2+wp3+wp4+wp5+wp6+wp7+wp8+wp9;

    os1=wp1;
    os2=wp2+os1;
    os3=wp3+os2;
    os4=wp4+os3;
    os5=wp5+os4;
    os6=wp6+os5;
    os7=wp7+os6;
    os8=wp8+os7;
    os9=wp9+os8;

    us1=100-os1;
    us2=100-os2;
    us3=100-os3;
    us4=100-os4;
    us5=100-os5;
    us6=100-os6;
    us7=100-os7;
    us8=100-os8;
    us9=100-os9;

    double us[]={us9, us8, us7, us6, us5, us4, us3, us2, us1};

    double a, b, c, d;
    for(int i=0; i<9; i++)
    {
        if (us[i]<80 && us[i+1]>80)
                {
                    a=Math.log10(us[i])-Math.log10(80);
                    b=Math.log10(us[i+1])-Math.log10(us[i]);
                    c=Math.log10(s[i+1])-Math.log10(s[i]);
                    d=Math.log10(s[i])-(c*(a/b));
                    p80=Math.pow(10, d);

                    break;
                }

    }



    DecimalFormat df = new DecimalFormat("#.##");
    String TW = df.format(totalweight);
    TOTALWEIGHT.setText(TW);
    String wpa = df.format(wp1);
    WP1.setText(wpa);
    String wpb = df.format(wp2);
    WP2.setText(wpb);
    String wpc = df.format(wp3);
    WP3.setText(wpc);
    String wpd = df.format(wp4);
    WP4.setText(wpd);
    String wpe = df.format(wp5);
    WP5.setText(wpe);
    String wpf = df.format(wp6);
    WP6.setText(wpf);
    String wpg = df.format(wp7);
    WP7.setText(wpg);
    String wph = df.format(wp8);
    WP8.setText(wph);
    String wpi = df.format(wp9);
    WP9.setText(wpi);
    String twp = df.format(wptotal);
    TOTALWP.setText(twp);

    String osa = df.format(os1);
    OS1.setText(osa);      
    String osb = df.format(os2);
    OS2.setText(osb);  
    String osc = df.format(os3);
    OS3.setText(osc);      
    String osd = df.format(os4);
    OS4.setText(osd);
    String ose = df.format(os5);
    OS5.setText(ose);   
    String osf = df.format(os6);
    OS6.setText(osf);      
    String osg = df.format(os7);
    OS7.setText(osg);     
    String osh = df.format(os8);
    OS8.setText(osh);    
    String osi = df.format(os9);
    OS9.setText(osi); 

    String usa = df.format(us1);
    US1.setText(usa); 
    String usb = df.format(us2);
    US2.setText(usb); 
    String usc = df.format(us3);
    US3.setText(usc); 
    String usd = df.format(us4);
    US4.setText(usd); 
    String use = df.format(us5);
    US5.setText(use); 
    String usf = df.format(us6);
    US6.setText(usf); 
    String usg = df.format(us7);
    US7.setText(usg); 
    String ush = df.format(us8);
    US8.setText(ush); 
    String usi = df.format(us9);
    US9.setText(usi); 

    String pp = df.format(p80);
    P80.setText(pp);


    for(int i=0; i<9; i++)
    {
        graph.getData().add(new XYChart.Data(Math.log10(s[i]), Math.log10(us[i])));

    }
    GRAPH.getData().add(graph);

}

private double totalweightcalculations(double[] s, double pan) 
{
    double a=0, tw=0;
    for(int i=0; i<9;i++)
    {
        tw=s[i]+a;
        a=tw;
    }

    return tw;
}



}

FXML:

 <?xml version="1.0" encoding="UTF-8"?>

 <?import javafx.scene.text.*?>
 <?import javafx.scene.chart.*?>
 <?import java.lang.*?>
 <?import java.util.*?>
 <?import javafx.scene.*?>
 <?import javafx.scene.control.*?>
 <?import javafx.scene.layout.*?>


 <AnchorPane id="AnchorPane" prefHeight="386.0" prefWidth="1162.0"         xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="minpro1.minpro1Controller">
<children>
  <Label alignment="CENTER" layoutX="25.0" layoutY="21.0" text="Size (microns)" textAlignment="CENTER" />
  <Label alignment="CENTER" layoutX="144.0" layoutY="21.0" text="Weight" />
  <Label alignment="CENTER" contentDisplay="CENTER" layoutX="481.0" layoutY="21.0" text="Percent Undersize" />
  <Label alignment="CENTER" layoutX="357.0" layoutY="21.0" text="Percent Oversize" />
  <Label alignment="CENTER" contentDisplay="CENTER" layoutX="234.0" layoutY="21.0" text="Weight Percent" />
  <TextField fx:id="S1" alignment="CENTER" layoutX="25.0" layoutY="38.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S2" alignment="CENTER" layoutX="25.0" layoutY="63.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S3" alignment="CENTER" layoutX="25.0" layoutY="88.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S4" alignment="CENTER" layoutX="25.0" layoutY="113.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S5" alignment="CENTER" layoutX="25.0" layoutY="138.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S6" alignment="CENTER" layoutX="25.0" layoutY="163.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S7" alignment="CENTER" layoutX="25.0" layoutY="189.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="S8" alignment="CENTER" layoutX="25.0" layoutY="214.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W8" alignment="CENTER" layoutX="127.0" layoutY="214.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W7" alignment="CENTER" layoutX="127.0" layoutY="189.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W6" alignment="CENTER" layoutX="127.0" layoutY="163.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W5" alignment="CENTER" layoutX="127.0" layoutY="138.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W4" alignment="CENTER" layoutX="127.0" layoutY="113.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W3" alignment="CENTER" layoutX="127.0" layoutY="88.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W2" alignment="CENTER" layoutX="127.0" layoutY="63.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="W1" alignment="CENTER" layoutX="127.0" layoutY="38.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="WP1" alignment="CENTER" editable="false" layoutX="224.0" layoutY="38.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP2" alignment="CENTER" editable="false" layoutX="224.0" layoutY="63.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP3" alignment="CENTER" editable="false" layoutX="224.0" layoutY="88.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP4" alignment="CENTER" editable="false" layoutX="224.0" layoutY="113.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP5" alignment="CENTER" editable="false" layoutX="224.0" layoutY="138.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP6" alignment="CENTER" editable="false" layoutX="224.0" layoutY="163.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP7" alignment="CENTER" editable="false" layoutX="224.0" layoutY="189.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP8" alignment="CENTER" editable="false" layoutX="224.0" layoutY="214.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS8" alignment="CENTER" editable="false" layoutX="350.0" layoutY="214.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS7" alignment="CENTER" editable="false" layoutX="350.0" layoutY="189.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS6" alignment="CENTER" editable="false" layoutX="350.0" layoutY="163.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS5" alignment="CENTER" editable="false" layoutX="350.0" layoutY="138.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS4" alignment="CENTER" editable="false" layoutX="350.0" layoutY="113.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS3" alignment="CENTER" editable="false" layoutX="350.0" layoutY="88.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS2" alignment="CENTER" editable="false" layoutX="350.0" layoutY="63.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS1" alignment="CENTER" editable="false" layoutX="350.0" layoutY="38.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US1" alignment="CENTER" editable="false" layoutX="478.0" layoutY="38.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US2" alignment="CENTER" editable="false" layoutX="478.0" layoutY="63.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US3" alignment="CENTER" editable="false" layoutX="478.0" layoutY="88.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US4" alignment="CENTER" editable="false" layoutX="478.0" layoutY="113.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US5" alignment="CENTER" editable="false" layoutX="478.0" layoutY="138.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US6" alignment="CENTER" editable="false" layoutX="478.0" layoutY="163.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US7" alignment="CENTER" editable="false" layoutX="478.0" layoutY="189.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US8" alignment="CENTER" editable="false" layoutX="478.0" layoutY="214.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="PAN" alignment="CENTER" layoutX="127.0" layoutY="239.0" prefHeight="25.0" prefWidth="74.0" />
  <TextField fx:id="TOTALWEIGHT" alignment="CENTER" editable="false" layoutX="127.0" layoutY="264.0" prefHeight="25.0" prefWidth="74.0" />
  <Label alignment="CENTER" layoutX="48.0" layoutY="243.0" text="PAN" />
  <Label alignment="CENTER" layoutX="42.0" layoutY="268.0" text="TOTAL" />
  <TextField fx:id="TOTALWP" alignment="CENTER" editable="false" layoutX="224.0" layoutY="264.0" prefHeight="25.0" prefWidth="102.0" />
  <LineChart fx:id="GRAPH" layoutX="678.0" layoutY="28.0" prefHeight="331.0" prefWidth="416.0" title="GATES-GAUDIN-SCHUHMANN PLOT">
    <xAxis>
      <NumberAxis label="log Particle Size" side="BOTTOM" fx:id="xaxis" />
    </xAxis>
    <yAxis>
      <NumberAxis fx:id="yaxis" label="log Cumulative Perent Passing" side="LEFT" />
    </yAxis>
  </LineChart>
  <Button fx:id="BUTTON" alignment="CENTER" layoutX="581.0" layoutY="273.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Show Answers" />
  <Label layoutX="452.0" layoutY="338.0" text="P80">
     <font>
        <Font name="System Bold" size="14.0" />
     </font>
  </Label>
  <TextField fx:id="P80" alignment="CENTER" editable="false" layoutX="487.0" layoutY="335.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="OS9" alignment="CENTER" editable="false" layoutX="350.0" layoutY="239.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="US9" alignment="CENTER" editable="false" layoutX="478.0" layoutY="239.0" prefHeight="25.0" prefWidth="102.0" />
  <TextField fx:id="WP9" alignment="CENTER" editable="false" layoutX="224.0" layoutY="239.0" prefHeight="25.0" prefWidth="102.0" />
</children>
</AnchorPane>

主应用程序:

package minpro1;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class MinPro1 extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("minpro1.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

您的一个数据点给出了 NaN x 值,这导致数字轴的自动范围失败。如果您在按钮处理方法中提供一些日志记录,您可以看到这一点:

for(int i=0; i<9; i++)
{

    XYChart.Data<Number, Number> dataPoint = new XYChart.Data<>(Math.log10(s[i]), Math.log10(us[i]));
    System.out.println("("+dataPoint.getXValue()+", "+dataPoint.getYValue()+")");
    graph.getData().add(dataPoint);
}

您可以通过在添加到图表之前检查值来解决问题:

for(int i=0; i<9; i++)
{

    XYChart.Data<Number, Number> dataPoint = new XYChart.Data<>(Math.log10(s[i]), Math.log10(us[i]));
    System.out.println("("+dataPoint.getXValue()+", "+dataPoint.getYValue()+")");
    if (Double.isFinite(dataPoint.getXValue().doubleValue())) {
        graph.getData().add(dataPoint);
    }
}