android - MPAndroidChart conditional dataset color -
hello folks: wondering if there better way conditional coloring bars using mpandroidchart. solution create conditional array of colors follows:
private void setdatacolored(list<string> labels, list<float> values) { final int greencolor = color.parsecolor("#66bb6a"); final int redcolor = color.parsecolor("#ef5350"); arraylist<barentry> entries = new arraylist<>(); list<integer> colors = new arraylist<>(); (int pos = 0; pos < values.size(); pos++) { float value = values.get(pos); entries.add(new barentry(math.abs(value), pos)); colors.add(value >= 0 ? greencolor : redcolor); } bardataset dataset = new bardataset(entries, "values"); dataset.setcolors(colors); arraylist<bardataset> datasets = new arraylist<bardataset>(); datasets.add(dataset); bardata data = new bardata(labels, datasets); data.setdrawvalues(true); mchart.setdata(data); }
i worry performance, since need create colors
array. derived problem legend: cant visualize specifying red , green color meaning.
i see documentation here
we can sure apply different colors bar based on our dynamic condition. let's see problem have array of float values need match color array. if value in negative should represent red , positive should display green.
instead of colors array can have linedataset example see below :
final int greencolor = r.color.green; final int redcolor = r.color.red; arraylist<barentry> entries = new arraylist<>(); list<integer> colors = new arraylist<>(); (int pos = 0; pos < values.size(); pos++) { float value = values.get(pos); entries.add(new barentry(math.abs(value), pos)); colors.add(value >= 0 ? greencolor : redcolor); } linedataset setcomp1 = new linedataset(entries, "company 1"); // sets colors dataset, resolution of resource name "real" color done internally setcomp1.setcolors(colors, context);
Comments
Post a Comment