如何在折线图javafx中显示所需数量的setTickLabelsVisible false

时间:2018-11-30 06:37:11

标签: javafx label linechart

我正在尝试为股票市场数据建立时间序列折线图。我正在动态获取输入,并将其绘制在折线图中。

我要做的是在图表上绘制每个刻度数据,但只希望显示所需数量的刻度标签。

例如:说我开始在上午10点建立图表,而我是在3秒内绘图,所以在4个小时后(即下午1点),我希望绘制每个刻度,但是Xaxis上的标签应该是我的选择,仅说4个刻度标签(上午10点,上午11点,下午12点,下午1点)。

enter image description here

请查看所需的图形: enter image description here

非常感谢。 :)

代码:

    @FXML
    private void buttonGraph(ActionEvent event) throws ParseException{  

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");  
        LocalTime initTime = LocalTime.now();
        Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
        Session session = cluster.connect("pass_master");
        lineChart.getData().clear();            
        ResultSet results2 = session.execute("select count(*) as ct from pass8 where tkn ="+basic.get(tkn.getText())+" and timestmp <= "+etDate+" and timestmp >= "+stDate);
        String total = null;
        for( Row row : results2){               
           total = row.toString();              
        }           
        System.out.println(total);
        System.out.println(total.substring(4, total.length()-1));
        int totalrows = Integer.valueOf(total.substring(4, total.length()-1));
        System.out.println("This is int value : "+totalrows);
        XYChart.Series<String,Double> series = new XYChart.Series<>();
        y.setAutoRanging(false);
        y.setSide(Side.RIGHT);          
        if (graphUpdater == null) {
            graphUpdater =new Thread(new Runnable() {
                double min = 10000000;
                double max = 0;
                String chartData ="";
                String invisible = "";
                int i = 0;
                XYChart.Data item;
                public void run() {
                    while(running) {
                        try {
                            Quote quote = MulticastReciever.getInstance().getQuote(tkn.getText());                              
                            if (quote!=null) {
                                LocalDateTime now = LocalDateTime.now();    
                                double value = quote.getLast();
                                if (value < min) {
                                    min = value;
                                }
                                if (value > max) {
                                    max = value;
                                }
                                i++;                                    
                                a1.add(value);                                                                          
                                 if (i % 5 == 0 ){
                                    chartData = dtf.format(now);
                                    x.setTickLabelFill(Color.RED);                                      
                                    item = new XYChart.Data<>(chartData, value);                                 
                                 }
                                    else {
                                        invisible += (char)32;
                                        chartData = dtf.format(now);
                                        x.setTickMarkVisible(false);                                            
                                        item = new XYChart.Data<>(chartData, value);                                               
                                 }
                                series.getData().add(item);                             
                                lineChart.setCreateSymbols(false);
                                lineChart.setLegendVisible(false);                                  
                                y.setLowerBound(min-(.001*min));
                                y.setUpperBound(max+(.001*max));
                                y.setTickLabelFill(Color.RED);
                                x.setTickMarkVisible(false);
                                y.setTickMarkVisible(false);
                            }                               
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            graphUpdater.start();
        }               
        System.out.println("previousToken:"+previousToken+", GC:"+this);
        int currentToken = Integer.valueOf(basic.get(tkn.getText()));

        if (previousToken!=null) {
            System.out.println("Going to unsubscribe token:"+previousToken);
            MulticastReciever.getInstance().unsubscribeMarketData(previousToken);
        }           

        MulticastReciever.getInstance().subscribeMarketData(tkn.getText(), currentToken);
        previousToken = currentToken;                               
        int factor=10;
        if(totalrows>300){
         factor =totalrows/300;
        }
        else
        {
            factor=1;
        }
        System.out.println(factor);
        int count = 0;          
        x.setTickLabelRotation(45.0);
        System.out.println("This is the property: "+x.isTickLabelsVisible());
        x.setTickLabelGap(5000.0);
        System.out.println("getTickLabelGap:"+x.getTickLabelGap());
        x.setTickLabelGap(10.0);
        System.out.println("getTickLabelGap:"+x.getTickLabelGap());
        lineChart.getData().addAll(series);
        a1.clear();                 
        for(final XYChart.Data<String,Double> data :series.getData()){              
            data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
                    lbl.setText("X :"+data.getXValue()+"\nY :"+ String.valueOf(data.getYValue()));
                    Tooltip.install(data.getNode(), new Tooltip("X :"+data.getXValue()+"\nY :"+ String.valueOf(data.getYValue())));                     
                }
            });
        }               
        cluster.close();
    }

0 个答案:

没有答案