- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试动态设置 BarChart
Series
颜色。
我希望相同的系列名称始终具有相同的颜色。它在图表中并不总是相同的系列名称,这就是为什么我不能依赖图表中的位置(默认情况下颜色在数据列表中的相同位置总是相同)。
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");
}
}
}
}
}
});
这没有按预期工作。 Series
的Node
始终为null
。这是为什么?
编辑:另一种无效的方法:
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..");
}
}
});
}
}
}
});
不幸的是,这个回调似乎从未被调用过..
最佳答案
试试这个:
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);
}
}
对于 Java 10 及更高版本,将 Used to change legend color
代码替换为以下代码。未在 Java 9 上测试
//Used to change legend color
for(Node node : bc.lookupAll("Label.chart-legend-item"))
{
Label tempLabel = (Label)node;
if(tempLabel.getText().equals("2003"))
{
tempLabel.getGraphic().setStyle("-fx-bar-fill: firebrick;");
}
}
此应用程序将 2003 系列的颜色更改为耐火砖色。 for 循环是您应该关注的部分。
更新我添加了一种更改图例颜色的方法。
2019 年 8 月 8 日更新原始代码适用于 Java 8。我正在为 Java 10 及更高版本添加代码。
关于JavaFX BarChart,按系列名称设置系列颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369495/
我为 BarChart 设置了 OnChartValueSelectedListener 但是当我点击任何条形图或 BarChart 中的任何地方时只有 onNothingSelected() 正在调
我正在尝试动态设置 BarChart Series 颜色。 我希望相同的系列名称始终具有相同的颜色。它在图表中并不总是相同的系列名称,这就是为什么我不能依赖图表中的位置(默认情况下颜色在数据列表中的相
我的窗口有以下 Controller : package window; import javafx.event.ActionEvent; import javafx.fxml.FXML; impor
如何更改 JavaFX BarChart 中条形的颜色? 我找不到通过 css setStyle 方法改变颜色的方法。 最佳答案 你可以使用css设置条形颜色 .default-color0.char
我有一个 flutter 应用程序,需要 Bar chart我可以在其中单击栏来打开包含相关信息的新页面。 我该怎么做? /// Bar chart example charts.BarChart(
我有一个 flutter 应用程序,需要 Bar chart我可以在其中单击栏来打开包含相关信息的新页面。 我该怎么做? /// Bar chart example charts.BarChart(
我在使用条形图时遇到问题。我为 Primefaces 编写了一个 barCharts 示例,但在页面上它们不可见。 这里是 Bean 类: import java.io.Serializable; i
我正在使用 Flot API 在我的应用程序中以图形方式绘制数据。我能够绘制数据并且能够实现工具提示。但我在使用工具提示时遇到问题。它显示 x 轴上的标签值“未定义”。 $(function () {
我想我已经尝试了所有方法,但我无法解决这个问题。 对于每家公司,我需要用该公司的颜色分组并显示 3 个条形图,并为每个条形图显示注释。 我只能得到这样的东西: 但是我真正需要的是这样的: 我已经花了超
我在后台的 iOS 应用程序中使用 NSTimer,它每 30 秒在数组中保存一些数据。该应用程序以折线图显示最后 10 个值(5 分钟的值)。 我的问题是当应用程序不在屏幕上时,在后台使用每 30
在我的项目中我使用 GWT 图表 - 我想从所有条开始的地方删除垂直线,并减少条之间的空间。是否可以?我已经尝试了几次,但没有成功。这是我的 Java 代码: BarChart chart
我的窗口有以下 Controller : package window; import javafx.event.ActionEvent; import javafx.fxml.FXML; impor
我的数据从 750 000 - 1 000 000+ 变化,所以我实际上不需要看到低于 750 000 的值。 如果我只是使 y[i] - 750 000 那么还有另一个问题(在 AxisY 上显示最
我正在创建谷歌条形图,它从数据库中获取数据。问题是数据包含低于“1”的值,如 0.6、0.7 等它不会出现在图表上。图表仅显示从 1 开始的值。 google.load('visualization
我正在使用 MPAndroidChart 在我的 Android 应用程序中绘制图表。 一个要求是我需要从非零 y 位置开始绘制条形图,如 this 中所建议的那样.所以我同样使用 CandleSti
是否可以将 UILabel 设置为条形图的水平条? (CorePlot) 谢谢! 最佳答案 如果您只需要标记其中一个条形,您可以使用 CPTPlotSpaceAnnotation。将 anchorPl
我正在使用 MPAndroidChart 库作为条形图,其中,我使用 chart.setDrawValueAboveBar(false) 来设置条形内部的条形值,现在我想要在条形内部垂直显示值。 请帮
我在 Google 中找不到它,所以我在这里询问。如何设置取决于 PrimeFaces BarChart 值的条形颜色。例如,如果我的值小于 50%,则条形图为红色。也许有人有类似的问题并且可以给我发
正如标题所说。基本上我有一个 ArrayList,其中 DiceRoll 是一个在 TableView 中成功使用的类,用于在值更改时更新值。 public class DiceRoll { priv
我需要从 BarChart 中删除右边的图例。 这是我的代码。 mChart = (BarChart) rootView.findViewById(R.id.chart1); mCha
我是一名优秀的程序员,十分优秀!