如果类别名称相同,则合并图表数据-JavaFX [英] Combine chart data if category name is the same - JavaFX

查看:78
本文介绍了如果类别名称相同,则合并图表数据-JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可观察到的图表数据列表,它是自动创建的.有什么方法可以自动将具有相同类别名称的数据合并到一个类别中?我之所以需要这样做,是因为可观察的数据列表是直接在SQL数据库中的表中创建的,因此我不知道用户是否真的想在图表中显示组合视图或每个类别和值.

I have an observable list of chart data which is created automatically. Is there any way to automatically combine the data which has the same category name in to one category? The reason I need this is since the observable list of data is created directly form a table in a SQL database, I can not know if the user actually wants to show a combined view or each category and value separately in the chart.

说我有以下数据:

我希望将所有值组合到一个名为Bordeau-1的饼中,而不是多次出现Bordeau-1,对于Boredeau-2和3也是一样.

Instead of Bordeau-1 appearing this many times, I want all the values combined in to one pie called Bordeau-1, the same goes for Boredeau-2 and 3.

这是我用来从ObservableList(代表一个表)自动创建数据的代码:

This is the code I use to automatically create data from the ObservableList which represents a table:

 ObservableList<PieChart.Data> pieChartData
                = 
FXCollections.observableArrayList(EasyBind.map(observableListData, rowData -> {
                    String name = (String) rowData.get(0);
                  Double value = Double.parseDouble(rowData.get(1));
                    return new PieChart.Data(name, value);
                }));

推荐答案

加载数据时,将其存储在 Map< String,Double> 中,并使用 Map.merge(...)方法来添加或合并新值.

When you load the data, store it in a Map<String, Double>, and use the Map.merge(...) method to add or combine new values.

因此,在从数据库加载数据的代码中,您将执行以下操作:

So in the code where you are loading the data from the database, you do this:

public Map<String, Double> loadDataFromDatabase() {
    Map<String, Double> data = new HashMap<>();
    // for each row in database:
        // get name and value from row
        data.merge(name, value, Double::sum);
    // end for...
    return data ;
}

将所有数据加载到地图中后,需要将其转换为 ObservableList< PieChart.Data> .我不认为在 EasyBind 中有一种可爱的方法来执行此操作,并且如果您始终在开始时加载所有数据,则不需要绑定列表.您可以执行以下操作:

Once you have loaded all the data into the map, you need to convert it to an ObservableList<PieChart.Data>. I don't think there's a cute way to do this in EasyBind, and if you're loading all the data at the start anyway, then you don't need the bound list. You can do something like:

ObservableList<PieChart.Data> pieChartData =
    data.entrySet().stream()
    .map(entry -> new PieChart.Data(entry.getKey(), entry.getValue()))
    .collect(Collectors.toCollection(() -> FXCollections.observableArrayList()));

如果在创建饼图之后原始地图可能会被修改,则您需要将其设置为 ObservableMap ,并向其注册一个侦听器,并在地图发生变化时相应地更新饼图数据.如果您需要这样做,可能值得在EasyBind框架中请求适当的功能.有点像方法

If the original map may get modified after the pie chart is created, you will need to make it an ObservableMap, register a listener with it, and update the pie chart data accordingly if the map changes. If you need this, it might be worth requesting the appropriate functionality in the EasyBind framework. Something like a method

public static <K, V, T> ObservableList<T> mapToList(ObservableMap<K, V> map, Function<Map.Entry<K, V>, T> mapping);

这篇关于如果类别名称相同,则合并图表数据-JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆