gpt4 book ai didi

java - 如何在Java FX中设置相对单位的类别差距?

转载 作者:行者123 更新时间:2023-11-30 02:47:16 34 4
gpt4 key购买 nike

假设我想让条之间的间隙与条的大小相同。如果单位是百分比,我会设置100。但这不起作用:

public class StackedBarChart_Horizontal_Try extends Application {

final static String itemA = "A";
final static String itemB = "B";
final static String itemC = "C";
@Override
public void start(Stage stage) {
final NumberAxis xAxis = new NumberAxis();
final CategoryAxis yAxis = new CategoryAxis();

final StackedBarChart<Number, String> bc = new StackedBarChart<Number, String>(xAxis, yAxis);

//bc.setBarGap(0);
bc.setCategoryGap(100);




bc.setTitle("Summary");
xAxis.setLabel("Value");
xAxis.setTickLabelRotation(90);
yAxis.setLabel("Item");

XYChart.Series series1 = new XYChart.Series();
series1.setName("2003");
series1.getData().add(new XYChart.Data(2, itemA));


XYChart.Series series2 = new XYChart.Series();
series2.setName("2004");
series2.getData().add(new XYChart.Data(41, itemB));

XYChart.Series series3 = new XYChart.Series();
series3.setName("2005");
series3.getData().add(new XYChart.Data(18, itemC));

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

给出

enter image description here

并且间隙的大小在窗口大小调整时保持不变:

enter image description here

这意味着间隙是以屏幕像素为单位设置的。

如何设置轴单位大小的间隙?

最佳答案

条形图的布局与您想要实现的效果相反:给定绘图区域的高度,布局机制首先为间隙分配空间,然后计算条形的大小基于剩余可用空间。

因此,执行此操作的方法是将 categoryGapProperty 绑定(bind)到给定条数和绘图区域高度(与 y 轴的高度相同)的正确大小)。要为间隙和条形分配相同的空间量,您需要将间隙等于总高度除以条形数量的两倍:

bc.categoryGapProperty().bind(Bindings.createDoubleBinding(() ->
yAxis.getHeight() / (2 * bc.getData().size()),
Bindings.size(bc.getData()), yAxis.heightProperty()));

完整示例:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class StackedBarChart_Horizontal_Try extends Application {

final static String itemA = "A";
final static String itemB = "B";
final static String itemC = "C";
@Override
public void start(Stage stage) {
final NumberAxis xAxis = new NumberAxis();
final CategoryAxis yAxis = new CategoryAxis();

final StackedBarChart<Number, String> bc = new StackedBarChart<Number, String>(xAxis, yAxis);

//bc.setBarGap(0);
// bc.setCategoryGap(100);

bc.categoryGapProperty().bind(Bindings.createDoubleBinding(() ->
yAxis.getHeight() / (2 * bc.getData().size()),
Bindings.size(bc.getData()), yAxis.heightProperty()));


bc.setTitle("Summary");
xAxis.setLabel("Value");
xAxis.setTickLabelRotation(90);
yAxis.setLabel("Item");

XYChart.Series series1 = new XYChart.Series();
series1.setName("2003");
series1.getData().add(new XYChart.Data(2, itemA));


XYChart.Series series2 = new XYChart.Series();
series2.setName("2004");
series2.getData().add(new XYChart.Data(41, itemB));

XYChart.Series series3 = new XYChart.Series();
series3.setName("2005");
series3.getData().add(new XYChart.Data(18, itemC));

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

在不同的窗口大小下呈现:

enter image description here

enter image description here

enter image description here

关于java - 如何在Java FX中设置相对单位的类别差距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39831843/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com