JavaFX BarChart,按系列名称设置系列颜色 [英] JavaFX BarChart, Set Series Color by Series Name
问题描述
我正在尝试动态设置 BarChart
系列
颜色。
I am trying to dynamically set the BarChart
Series
color.
我希望同一系列名称的颜色始终相同。
图表中的系列名称并不总是相同,这就是为什么我不能依赖这个位置在图表中(默认颜色在数据列表中的相同位置始终相同)。
I want to have always the same color for the same Series name. Its not always the same Series names in the chart, which is why I cannot rely on the position in the chart (colors per default always the same at the same position in the data list).
chart.getData().addListener(new ListChangeListener<Series<String, Number>>() {
@Override
public void onChanged(
final javafx.collections.ListChangeListener.Change<? extends Series<String, Number>> c) {
while (c.next()) {
for (final Series s : c.getAddedSubList()) {
if ("booking".equalsIgnoreCase(s.getName())) {
if (s.getNode() != null) {
s.getNode().getStyleClass().add(".source-background-booking");
}
} else if ("airbnb".equalsIgnoreCase(s.getName())) {
if (s.getNode() != null) {
s.getNode().getStyleClass().add(".source-background-airbnb");
}
}
}
}
}
});
这不能按预期工作。 系列
的节点
始终为 null
。为什么会这样?
This does not work as expected. The Series
's Node
is always null
. Why is that?
编辑:另一种无效的方法:
Another approach that is not working:
chart.getData().addListener(new ListChangeListener<Series<String, Number>>() {
@Override
public void onChanged(
final javafx.collections.ListChangeListener.Change<? extends Series<String, Number>> c) {
while (c.next()) {
System.err.println("Series " + c.getAddedSubList() + " kommt rein..");
for (final Series<String, Number> s : c.getAddedSubList()) {
s.nodeProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(final ObservableValue<? extends Node> observable, final Node oldValue,
final Node newValue) {
System.err.println("Already good");
if ("booking".equalsIgnoreCase(s.getName())) {
newValue.getStyleClass().add(".source-background-booking");
System.err.println("Seems to work..");
} else if ("airbnb".equalsIgnoreCase(s.getName())) {
newValue.getStyleClass().add(".source-background-airbnb");
System.err.println("Seems to work..");
}
}
});
}
}
}
});
不幸的是,这个回调似乎永远不会被调用..
Unfortunately, this callback seems to be never called..
推荐答案
试试这个:
import com.sun.javafx.charts.Legend;
import com.sun.javafx.charts.Legend.LegendItem;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
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.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Label;
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(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();
//Used to change series color.
for (XYChart.Series<String, Number> series : bc.getData()) {
if(series.getName().equals("2003"))
{
System.out.println("Series name: " + series.getName());
for(Data data : series.getData())
{
data.getNode().setStyle("-fx-bar-fill: firebrick;");
}
}
}
//Used to change legend color
for(Node n : bc.getChildrenUnmodifiable())
{
if(n instanceof Legend)
{
System.out.println(((Legend)n).getItems());
for(LegendItem items : ((Legend)n).getItems())
{
System.out.println("Legend item text: " + items.getText());
if(items.getText().equals("2003"))
{
items.getSymbol().setStyle("-fx-bar-fill: firebrick;");
}
}
}
}
}
public static void main(String[] args) {
launch(args);
}
}
此应用程序更改2003系列颜色耐火砖。 for循环是你应该关注的部分。
This app changes the 2003 series color to firebrick. The for loop is the part you should focus on.
UPDATE
我添加了一种更改图例颜色的方法。
UPDATE I added a way to change the legend color.
这篇关于JavaFX BarChart,按系列名称设置系列颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!