有没有办法在JavaFX图表中始终为特定的数据分配相同的颜色?

huangapple go评论88阅读模式
英文:

Is there a way to give a specific data always the same color in a javaFX chart?

问题

以下是您提供的内容的翻译结果:

.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: 绿色; }
.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;
}
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
        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)));
}

如果您需要更多帮助,请随时提问。

英文:

I am making a piechart in which you see attendances of students from a lecture.
Personally I thought it would be cool that the number of present students will always be green in the piechart. and the reasons of absence all a different shade of red. Is there any way to do this?

Here is my css:

<!-- language: lang-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;
}

And here is my Java:

<!-- language: lang-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 -&gt; item.getKey()
                .toString())).map(item -&gt; new PieChart.Data(String.format(&quot;%s: %.0f%%&quot;, item.getKey(),
                item.getValue() * 100f / lecture.getAttendaceSize()), item.getValue()))
                .collect(toCollection(FXCollections::observableArrayList)));
}

Thanks in advance 有没有办法在JavaFX图表中始终为特定的数据分配相同的颜色?

答案1

得分: 1

该系列的着色是根据添加系列的顺序进行的。现在您的第一个系列被设置为“绿色”,只要您调整算法,使您添加到饼图中的第一个系列是“在校学生人数”,那么该系列将始终显示为绿色。

英文:

Coloring of the series is done based on the order that the series is added. Right now your first series is set to be "green" so as long as you adjust your algorithm so that the first series you add to the PieChart is the "number of present students", that series will always be green.

huangapple
  • 本文由 发表于 2020年4月8日 07:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61091114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定