如何为单个系列的条形图添加3个图例? JAVAFX [英] how to add 3 legend for a single series barchart?? JAVAFX
问题描述
这是我的代码,用于生成10个不同颜色的条形图.我想分别添加图例,但它只显示黄色图例,我可以更改其颜色,但我想要3个图例.
Here is my code to generate 10 bars of different colors. I want to add legend respectively but it only shows yellow legend i can change its color but i want 3 legend.
我认为它只显示1种颜色,因为只有1个系列.单个系列是否可以添加多个图例?
I think it shows only 1 color because there is only 1 series. Is it possible to add more than 1 legend for a single series?
输出:
或者如果我可以将此图像作为图例添加到图表的左上方
or if i can add this image as legend to the middle left of my chart
我需要如何在条形图中显示图像或如何为单个系列条形图创建3个不同的标签
i need how to display image in bar chart or how to create 3 different labels for a single series bar chart
import javafx.application.Application;
import javafx.beans.value.*;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.stage.Stage;
public class DynamicallyColoredBarChart extends Application {
@Override
public void start(Stage stage) {
final CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Bars");
final NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Value");
final BarChart<String, Number> bc = new BarChart<>(xAxis, yAxis);
bc.setLegendVisible(false);
XYChart.Series series1 = new XYChart.Series();
for (int i = 0; i < 10; i++) {
// change color of bar if value of i is >5 than red if i>8 than blue
final XYChart.Data<String, Number> data = new XYChart.Data("Value " + i, i);
data.nodeProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> ov, Node oldNode, Node newNode) {
if (newNode != null) {
if (data.getYValue().intValue() > 8) {
newNode.setStyle("-fx-bar-fill: navy;");
} else if (data.getYValue().intValue() > 5) {
newNode.setStyle("-fx-bar-fill: red;");
}
}
}
});
series1.getData().add(data);
}
bc.getData().add(series1);
stage.setScene(new Scene(bc));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
推荐答案
要将图像放在左侧,您只需将图像和图表添加到HBox中即可:
To put an image to the left you can just add image and chart to HBox:
HBox root = new HBox(5);
root.getChildren().addAll(image, bc);
stage.setScene(new Scene(root));
这篇关于如何为单个系列的条形图添加3个图例? JAVAFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!