首页 > 解决方案 > 如何在折线图 javafx 中显示所需的 setTickLabelsVisible 数量

问题描述

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

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

例如:假设我在上午 10 点开始构建图表,并且我正在绘制 3 秒,所以在 4 小时后,即下午 1 点,我希望绘制每个刻度,但 Xaxis 上的标签应该是我的选择,比如说只有 4 个刻度标签(10AM、11AM、12AM、1PM)。

在此处输入图像描述

请查看所需的图表: 在此处输入图像描述

提前非常感谢。:)

代码:

    @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();
    }

标签: javafxlabellinechart

解决方案


推荐阅读