javafx之使用鼠标滚轮缩放条形图

dudu 阅读:30 2024-02-27 23:08:18 评论:0

我找到了许多如何放大图表的示例,但我正在寻找用户可以使用鼠标滚轮滚动的基本示例。

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.chart.BarChart; 
import javafx.scene.chart.CategoryAxis; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
import javafx.stage.Stage; 
 
public class BarChartSample extends Application { 
    final static String austria = "Austria"; 
    final static String brazil = "Brazil"; 
    final static String france = "France"; 
    final static String italy = "Italy"; 
    final static String usa = "USA"; 
 
    @Override public void start(Stage stage) { 
        stage.setTitle("Bar Chart Sample"); 
        final CategoryAxis xAxis = new CategoryAxis(); 
        final NumberAxis yAxis = new NumberAxis(); 
        final BarChart<String,Number> bc =  
            new BarChart<String,Number>(xAxis,yAxis); 
        bc.setTitle("Country Summary"); 
        xAxis.setLabel("Country");        
        yAxis.setLabel("Value"); 
 
        XYChart.Series series1 = new XYChart.Series(); 
        series1.setName("2003");        
        series1.getData().add(new XYChart.Data(austria, 25601.34)); 
        series1.getData().add(new XYChart.Data(brazil, 20148.82)); 
        series1.getData().add(new XYChart.Data(france, 10000)); 
        series1.getData().add(new XYChart.Data(italy, 35407.15)); 
        series1.getData().add(new XYChart.Data(usa, 12000));       
 
        XYChart.Series series2 = new XYChart.Series(); 
        series2.setName("2004"); 
        series2.getData().add(new XYChart.Data(austria, 57401.85)); 
        series2.getData().add(new XYChart.Data(brazil, 41941.19)); 
        series2.getData().add(new XYChart.Data(france, 45263.37)); 
        series2.getData().add(new XYChart.Data(italy, 117320.16)); 
        series2.getData().add(new XYChart.Data(usa, 14845.27));   
 
        XYChart.Series series3 = new XYChart.Series(); 
        series3.setName("2005"); 
        series3.getData().add(new XYChart.Data(austria, 45000.65)); 
        series3.getData().add(new XYChart.Data(brazil, 44835.76)); 
        series3.getData().add(new XYChart.Data(france, 18722.18)); 
        series3.getData().add(new XYChart.Data(italy, 17557.31)); 
        series3.getData().add(new XYChart.Data(usa, 92633.68));   
 
        Scene scene  = new Scene(bc,800,600); 
        bc.getData().addAll(series1, series2, series3); 
        stage.setScene(scene); 
        stage.show(); 
    } 
 
    public static void main(String[] args) { 
        launch(args); 
    } 
} 

欢迎任何帮助。

P.S 我测试将代码插入到 ScrollPane 中,但我没有看到使用它的任何效果。
ScrollPane s1 = new ScrollPane(); 
        //s1.setPrefSize(620, 620); 
        s1.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Horizontal scroll bar 
        s1.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED);    // Vertical scroll bar 
        s1.setContent(bc); 

请您参考如下方法:

看看this回答。

我已经用这个代码工作了:

final double SCALE_DELTA = 1.1; 
bc.setOnScroll(new EventHandler<ScrollEvent>() { 
    public void handle(ScrollEvent event) { 
        event.consume(); 
 
        if (event.getDeltaY() == 0) { 
            return; 
        } 
 
        double scaleFactor = (event.getDeltaY() > 0) ? SCALE_DELTA : 1 / SCALE_DELTA; 
 
        bc.setScaleX(bc.getScaleX() * scaleFactor); 
        bc.setScaleY(bc.getScaleY() * scaleFactor); 
    } 
}); 
 
bc.setOnMousePressed(new EventHandler<MouseEvent>() { 
    public void handle(MouseEvent event) { 
        if (event.getClickCount() == 2) { 
            bc.setScaleX(1.0); 
            bc.setScaleY(1.0); 
        } 
    } 
}); 


标签:java
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号