Tuesday 15 May 2012

Combine chart data if category name is the same - JavaFX -



Combine chart data if category name is the same - JavaFX -

i have observable list of chart info created automatically. there way automatically combine info has same category name in 1 category? reason need since observable list of info created straight form table in sql database, can not know if user wants show combined view or each category , value separately in chart.

say have next data:

instead of bordeau-1 appearing many times, want values combined in 1 pie called bordeau-1, same goes boredeau-2 , 3.

this code utilize automatically create info observablelist represents table:

observablelist<piechart.data> piechartdata = fxcollections.observablearraylist(easybind.map(observablelistdata, rowdata -> { string name = (string) rowdata.get(0); double value = double.parsedouble(rowdata.get(1)); homecoming new piechart.data(name, value); }));

when load data, store in map<string, double>, , utilize map.merge(...) method add together or combine new values.

so in code loading info database, this:

public map<string, double> loaddatafromdatabase() { map<string, double> info = new hashmap<>(); // each row in database: // name , value row data.merge(name, value, double::sum); // end for... homecoming info ; }

once have loaded info map, need convert observablelist<piechart.data>. don't think there's cute way in easybind, , if you're loading info @ start anyway, don't need bound list. can like:

observablelist<piechart.data> piechartdata = data.entryset().stream() .map(entry -> new piechart.data(entry.getkey(), entry.getvalue())) .collect(collectors.tocollection(() -> fxcollections.observablearraylist()));

if original map may modified after pie chart created, need create observablemap, register listener it, , update pie chart info accordingly if map changes. if need this, might worth requesting appropriate functionality in easybind framework. method

public static <k, v, t> observablelist<t> maptolist(observablemap<k, v> map, function<map.entry<k, v>, t> mapping);

charts javafx

No comments:

Post a Comment