2012-12-19 60 views
2

我是Java新手,对JavaFX也是如此。我正尝试构建一个使用JavaFx LineChart显示实时数据的动态图形。我正在使用NumberAxis来为我的X轴设置数值。我尝试使用两个NumberAxis的构造函数,并注意到它会自动将X轴值设置为1,2,3,4..etc。JavaFX LineChart手动设置X轴值

但我想显示不同的顺序,如1,3,4,5,2 ...,而不是按升序排列的值。有人可以在这方面指导我吗?很感谢任何形式的帮助。谢谢 !

这里是我的代码看起来,

public class UI extends Application{ 

@SuppressWarnings("unchecked") 
public static XYChart.Series series = new XYChart.Series(); 
public static XYChart.Series series1 = new XYChart.Series(); 


/** 
* @param args 
*/ 


public static void main(String[] args) {   
    launch(args); 
} 

@Override 
public void start(Stage primaryStage) throws Exception { 
    BorderPane border = new BorderPane(); 
    //HBox hbox = addHBox(); 
    border.setTop(addHBox()); 
    border.setCenter(addLineChart()); 

    Scene scene = new Scene(border); 

    primaryStage.setScene(scene); 
    primaryStage.setHeight(570); 
    primaryStage.setWidth(700); 
    //primaryStage.setResizable(false); 
    primaryStage.centerOnScreen(); 


    //primaryStage.setFullScreen(true); 


    primaryStage.show(); 
} 

public HBox addHBox() { 
    HBox hbox = new HBox(); 
    hbox.setStyle("-fx-background-color: #e6e6fa;");//d3d3d3 gray, f5f5f5 smoke white 


    hbox.setPadding(new Insets(0, 0, 0, 560)); 
    hbox.setSpacing(10); 

    hbox.setMaxWidth(10); 
    hbox.setStyle("-fx-background-color: #e6e6fa;");//d3d3d3 gray, f5f5f5 smoke white 

    //Label logoLabel = new Label(); 
    Image logoImage = new Image(getClass().getResourceAsStream("Logo.jpeg"));  
    ImageView logoImageViewer = new ImageView();   
    //Rectangle2D viewportRect = new Rectangle2D(40, 35, 110, 110); 
    //logoImageViewer.setViewport(viewportRect); 

    logoImageViewer.setImage(logoImage);   
    logoImageViewer.setPreserveRatio(true); 
    logoImageViewer.setSmooth(true); 
    logoImageViewer.setCache(true); 

    hbox.getChildren().add(logoImageViewer); 
    return hbox; 
} 



public Node addLineChart(){ 

    //final NumberAxis xAxis = new NumberAxis(1,10,1);  
    final CategoryAxis xAxis = new CategoryAxis(); 

    xAxis.setAutoRanging(false); 


    final NumberAxis yAxis = new NumberAxis(-80,60,20);   
    yAxis.setMinorTickVisible(false); 




    final LineChart<String,Number> lineChart = new LineChart<String,Number>(xAxis,yAxis); 




    //populating the series with data 
    series.getData().add(new XYChart.Data("1", 20.222222222));//Math.random())); 
    series.getData().add(new XYChart.Data("3", 0));//Math.random())); 
    series.getData().add(new XYChart.Data("4", 1));//Math.random())); 
    series.getData().add(new XYChart.Data("2", -60.11111));//Math.random())); 

    lineChart.getData().add(series); 
    lineChart.setLegendVisible(false); 

    return lineChart; 
} 

} 

例外看到使用的CategoryAxis,

Exception in Application start method 
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403) 
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47) 
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NullPointerException 
    at javafx.scene.chart.Axis.layoutChildren(Axis.java:634) 
    at javafx.scene.Parent.layout(Parent.java:1018) 
    at javafx.scene.chart.XYChart.layoutChartChildren(XYChart.java:684) 
    at javafx.scene.chart.Chart$1.layoutChildren(Chart.java:84) 
    at javafx.scene.Parent.layout(Parent.java:1018) 
    at javafx.scene.Scene.layoutDirtyRoots(Scene.java:513) 
    at javafx.scene.Scene.doLayoutPass(Scene.java:484) 
    at javafx.scene.Scene.preferredSize(Scene.java:1485) 
    at javafx.scene.Scene.impl_preferredSize(Scene.java:1512) 
    at javafx.stage.Window$10.invalidated(Window.java:719) 
    at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:127) 
    at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:161) 
    at javafx.stage.Window.setShowing(Window.java:782) 
    at javafx.stage.Window.show(Window.java:797) 
    at javafx.stage.Stage.show(Stage.java:229) 
    at com.bosch.mvci.ford.powerbalance.ui.UI.start(UI.java:184) 
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319) 
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206) 
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29) 
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73) 
... 1 more` 

回答

1

使用您的x轴的CategoryAxis

+0

嗨jewelsea,谢谢你的回应。我其实尝试使用CategoryAxis,但我的stage.show与空指针异常崩溃。我也修改了原始文章中的代码,并添加了错误。请建议。 – user1724114

+0

很抱歉提出基本问题。我是一个新手,我使用'CategoryAxis(FXCollections.observableArrayList(“1”,“3”,“4”,“2”))'构造函数而不是空的构造函数,它工作。这有点奇怪,因为oracle文档示例使用了空的构造函数[link](http://docs.oracle.com/javafx/2/charts/line-chart.htm#CIHGBCFI) – user1724114

+0

在您的示例中,您[setAutoRanging] (http://docs.oracle.com/javafx/2/api/javafx/scene/chart/Axis.html#setAutoRanging%28boolean%29)为false,这要求您明确提供'CategoryAxis'的可用值,Oracle文档样本不会这样做。 – jewelsea