java - 有没有办法让 javaFX 图表中的特定数据始终具有相同的颜色?

标签 java css javafx charts pie-chart

我正在制作一个饼图,您可以在其中看到讲座中学生的出勤情况。 就我个人而言,我认为在场学生的数量在饼图中始终为绿色会很酷。缺席的原因都是不同深浅的红色。有什么办法可以做到这一点吗?

这是我的 CSS:

.chart {
    -fx-background-color: #484848;
}

.chart-legend {
    -fx-background-color: transparent;
    -fx-text-fill: #fff;
    -fx-border-color: #333;
}

.chart-legend-item {
    -fx-text-fill: #fff;
}

.default-color0.chart-pie { -fx-pie-color: green; }
.default-color1.chart-pie { -fx-pie-color: #ff0000; }
.default-color2.chart-pie { -fx-pie-color: #ce0000; }
.default-color3.chart-pie { -fx-pie-color: #a80000; }
.default-color4.chart-pie { -fx-pie-color: #870000; }
.default-color5.chart-pie { -fx-pie-color: #560000; }
.default-color6.chart-pie { -fx-pie-color: #380000; }
.default-color7.chart-pie { -fx-pie-color: #1c0000; }

.chart-pie-label-line {
    -fx-stroke: #fff;
    -fx-fill: #fff;
}

.chart-pie-label {
    -fx-fill: #fff;
}

这是我的 Java:

private void setUpPieChart() {
    this.pieChart.setLabelLineLength(8f);
    this.updatePieChart();
}

private void updatePieChart() {
    Lecture lecture = this.lectureTable.getSelectionModel().getSelectedItem();

    if (lecture == null)
        this.pieChart.getData().clear();
    else
        // Get all attendances of the lecture object which was obtained from the currently selected row,
        // create a temporary dictionary by grouping all attendances by attendance type and their respective count,
        // sort the dictionary by attendance name and generate a list of pie chart slices for each dictionary item
        this.pieChart.setData(lecture.getAttendances().stream().collect(Collectors.groupingBy(Attendance::getType,
                Collectors.counting())).entrySet().stream().sorted(Comparator.comparing(item -> item.getKey()
                .toString())).map(item -> new PieChart.Data(String.format("%s: %.0f%%", item.getKey(),
                item.getValue() * 100f / lecture.getAttendaceSize()), item.getValue()))
                .collect(toCollection(FXCollections::observableArrayList)));
}

提前致谢:)

最佳答案

系列的着色是根据系列添加的顺序完成的。现在,您的第一个系列设置为“绿色”,因此只要您调整算法,使添加到饼图的第一个系列是“当前学生的数量”,该系列将始终为绿色。

关于java - 有没有办法让 javaFX 图表中的特定数据始终具有相同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61091114/

相关文章:

java - JDK ClassLoader.getResourceAsStream 坏了吗? (未关闭的资源)

java - Spring Boot变量/嵌套请求体

JavaFX:TabPane 上下文菜单,取决于所选选项卡

java - 错误 : JavaFX runtime components are missing,,需要使用 JDK 11 运行此应用程序

java - 如何在使用 Axis 1.4 wsdl2java 生成的客户端时获取 SOAP 消息

java - 主Activiti中没有从内部Activiti接收到异常

html - 使用 CSS 动画加载图像 VS 使用 GIF 图像加载

javascript - 没有jquery的视差淡入淡出

html - 使用 bootstrap 3.3.6 将卡片与动态高度对齐?

未调用 JavaFX FXML Controller 初始化方法