【问题标题】:javafx StackedBarChart cumulative valuejavafx StackedBarChart 累计值
【发布时间】:2017-01-05 22:24:38
【问题描述】:

有谁知道如何获取类别轴上每个 X 值的堆积条形图的累积值?有办法吗?

我无法手动完成,因为每个数据系列并未涵盖所有 X 值,并且大约有 400 个 X 值。

谢谢!

【问题讨论】:

    标签: javafx cumulative-sum stacked-chart


    【解决方案1】:

    假设你有一个

    StackedBarChart<String, Number> chart ;
    

    您可以将每个 x 值的累积总数计算为

    Map<String, Number> cumulativeTotal = new HashMap<>();
    for (Series<String, Number> series : chart.getData()) {
        for (XYChart.Data<String, Number> data : series.getData()) {
            cumulativeTotal.merge(data.getXValue(), data.getYValue(), (y1, y2) -> y1.doubleValue() + y2.doubleValue());
        }
    }
    

    或者,使用流:

    Map<String, Number> cumulativeTotal = chart.getData().stream()
            .flatMap(series -> series.getData().stream())
            .collect(Collectors.toMap(Data::getXValue, Data::getYValue, (y1, y2) -> y1.doubleValue() + y2.doubleValue()));
    

    【讨论】:

    • 非常感谢!直到现在我才看到你的答案。感谢您介绍新概念:地图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2019-06-06
    • 2015-12-07
    相关资源
    最近更新 更多