gpt4 book ai didi

JavaFX 14,堆积条形图不显示负值

转载 作者:行者123 更新时间:2023-12-02 00:06:47 26 4
gpt4 key购买 nike

示例:

import javafx.application.Application;
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 Main extends Application {

public static void main(String[] args) {
launch(args);
}

// Start the javafx application
@Override
public void start(Stage stage) {

CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();

XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
series.getData().add(new XYChart.Data<String, Number>(0 + "", 5));
series.getData().add(new XYChart.Data<String, Number>(1 + "", -5));
series.getData().add(new XYChart.Data<String, Number>(2 + "", 3));
series.getData().add(new XYChart.Data<String, Number>(3 + "", -2));

StackedBarChart<String, Number> weightChangeChartBar = new StackedBarChart<String, Number>(xAxis, yAxis);
weightChangeChartBar.getData().addAll(series);

Scene scene = new Scene(weightChangeChartBar, 500, 500);
stage.setScene(scene);
stage.show();
}

}

结果:

Outcome

之前关于此问题的堆栈溢出问题在 JavaFX14 中都不起作用(无论如何对我来说),有人知道解决方案吗?

之前的问题:

  1. JavaFX:13 Stacked Bar Chart negative values not showing
  2. JavaFx StackedBar Chart Issue
  3. Javafx StackedBarChart bug

最佳答案

解决了:必须重写条形图行为才能解决该错误,必须将系列添加到条形图,然后才能将数据添加到该系列。

 package example;

import javafx.application.Application;
import javafx.scene.Node;
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 Main extends Application {

public static void main(String[] args) {
launch(args);
}

// Start the javafx application
@Override
public void start(Stage stage) {

CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();

XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();

// modify behavior to counter bug
StackedBarChart<String, Number> barChart = new StackedBarChart<String, Number>(xAxis, yAxis) {
@Override
protected void dataItemAdded(XYChart.Series<String,Number> series, int itemIndex, XYChart.Data<String,Number> item) {
super.dataItemAdded(series, itemIndex, item);

Node bar = item.getNode();
double barVal = item.getYValue().doubleValue();

if (barVal < 0) {
bar.getStyleClass().add("negative");
}
}
};

// add series
barChart.getData().addAll(series);

// THEN add data
series.getData().add(new XYChart.Data<String, Number>(0 + "", 5));
series.getData().add(new XYChart.Data<String, Number>(1 + "", -5));
series.getData().add(new XYChart.Data<String, Number>(2 + "", 3));
series.getData().add(new XYChart.Data<String, Number>(3 + "", -2));


Scene scene = new Scene(barChart, 500, 500);
stage.setScene(scene);
stage.show();
}

}

负节点还必须具有与正节点不同的 CSS。

/* ====== BAR CHART =========================================================== */
/* TODO flip gradient vertical for negative bars */
.chart-bar {
-fx-bar-fill: #22bad9;
-fx-background-color: linear-gradient(derive(-fx-bar-fill,-30%), derive(-fx-bar-fill,-40%)),
linear-gradient(derive(-fx-bar-fill,80%), derive(-fx-bar-fill, 0%)),
linear-gradient(derive(-fx-bar-fill,30%), derive(-fx-bar-fill,-10%));
-fx-background-insets: 0,1,2;
-fx-background-radius: 5 5 0 0, 4 4 0 0, 3 3 0 0;
}

.negative.chart-bar {
-fx-background-color: linear-gradient(to top, derive(-fx-bar-fill,-30%), derive(-fx-bar-fill,-40%)),
linear-gradient(to top, derive(-fx-bar-fill, 80%), derive(-fx-bar-fill,0%)),
linear-gradient(to top, derive(-fx-bar-fill,30%), derive(-fx-bar-fill,-10%));
-fx-background-radius: 0 0 5 5, 0 0 4 4, 0 0 3 3;
}
.bar-chart:horizontal .chart-bar, .stacked-bar-chart:horizontal .chart-bar {
-fx-background-color: linear-gradient(to left, derive(-fx-bar-fill,-30%), derive(-fx-bar-fill,-40%)),
linear-gradient(to left, derive(-fx-bar-fill,80%), derive(-fx-bar-fill, 0%)),
linear-gradient(to left, derive(-fx-bar-fill,30%), derive(-fx-bar-fill,-10%));
-fx-background-radius: 0 5 5 0, 0 4 4 0, 0 3 3 0;
}
.bar-chart:horizontal .negative.chart-bar, .stacked-bar-chart:horizontal .negative.chart-bar {
-fx-background-color: linear-gradient(to right, derive(-fx-bar-fill,-30%), derive(-fx-bar-fill,-40%)),
linear-gradient(to right, derive(-fx-bar-fill, 80%), derive(-fx-bar-fill, 0%)),
linear-gradient(to right, derive(-fx-bar-fill,30%), derive(-fx-bar-fill,-10%));
-fx-background-radius: 5 0 0 5, 4 0 0 4, 3 0 0 3;
}

关于JavaFX 14,堆积条形图不显示负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61014553/

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