java - Dynamically change chart colors using colorpicker -


i want dynamically change colors of different type charts, using colorpicker. made sample code pie chart.

color picker action listener:

        colorpicker.setonaction(event -> {                     node node = chart.lookup(".default-color0.chart-pie");         string str = "-fx-pie-color:" + torgbcode(colorpicker.getvalue()) + ";";          node.setstyle(str);     }); 

css file:

.default-color0.chart-pie { -fx-pie-color: #ffd700; } .default-color1.chart-pie { -fx-pie-color: #ffa500; } .default-color2.chart-pie { -fx-pie-color: #860061; } .default-color3.chart-pie { -fx-pie-color: #adff2f; } .default-color4.chart-pie { -fx-pie-color: #ff5700; } 

it works fine partially. problem that, when i'm changing color, legend of chart doesn't follow it. how dynamically update legend?

screenshot

you can update legend obtaining after has been drawn via node lookup , modifying style similar have done in question

this answer built on have provided there assumption know item want update know style class in line:

node node = chart.lookup(".default-color0.chart-pie"); 

so in mind, can similar following:

public static void updatechartlegendcolorfromitemname(chart chart, string itemtoupdate, color legendcolor){         set<node> legenditems = chart.lookupall("label.chart-legend-item");         if(legenditems.isempty()){ return; }         string stylestring = "-fx-background-color:" + torgbcode(legendcolor) + ";";         for(node legenditem : legenditems){             label legendlabel = (label) legenditem;             node legend = legendlabel.getgraphic(); //the legend icon (circle default)              if(legend != null && legendlabel.gettext().equals(itemtoupdate)){                 legend.setstyle(stylestring);             }         }     } 

an example usage you've provided be:

colorpicker.setonaction(event -> {             node node = chart.lookup(".default-color0.chart-pie");             string stylestring = "-fx-background-color:" + torgbcode(colorpicker.getvalue()) + ";";             node.setstyle(stylestring);             updatechartlegendcolorfromitemname(chart, "sunday", colorpicker.getvalue());         }); 

hopefully helps


Popular posts from this blog

node.js - How do I prevent MongoDB replica set from querying the primary? -

Apache NiFi ExecuteScript: Groovy script to replace Json values via a mapping file -