2012-12-19 17 views
2

저는 Java와 JavaFX를 처음 사용합니다. JavaFx LineChart을 사용하여 실시간 데이터를 표시하는 동적 그래프를 작성하려고합니다. 내 X 축 값을 설정하려면 NumberAxis을 사용하고 있습니다. NumberAxis의 생성자를 모두 사용하여 시도해 보았고 X 축 값을 1,2,3,4..etc로 자동 설정한다는 사실을 알았습니다.JavaFX LineChart 수동으로 X 축 값 설정

하지만 1,3,4,5,2 ...와 같은 다른 순서로 값을 표시하고 오름차순으로 표시하지 않습니다. 이 점에 대해 누군가 나를 안내 해줄 수 있습니까? 어떤 도움이라도 대단히 감사합니다. 감사 ! 여기

는 CategoryAxis의 사용에 볼

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; 
} 

} 

예외,

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를 사용했지만 null 포인터 예외로 인해 내 stage.show가 충돌했습니다. 또한 원래 게시물의 코드를 수정했으며 오류도 추가했습니다. 제발 제안 해주세요. – user1724114

+0

기본 질문에 대해 죄송합니다. 나는 초보자이다. 나는 빈 생성자 대신에'CategoryAxis (FXCollections.observableArrayList ("1", "3", "4", "2"))'생성자를 사용했다. Thumbs oracle docs 예제가 빈 생성자 [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'에 사용할 수있는 값을 명시 적으로 제공해야합니다. , 오라클 문서 샘플에서는이 작업을 수행하지 않습니다. – jewelsea