i have a little programm which connects with a Database for getting data and shows this in a StackedBarChart. Before i insert the data, i sort them for every day and customer. I have a ComboBox too, which lets the user choose the day. So every time the day is changed, i clear the data of the chart and add new data. Then i want to change the color dynamically, because i don't know how many customers will be displayed. I know i have to do this after the Stage is shown. At the start of the programm the first chart displayed thems to look right. The colors of the stacked bars and legend changed and don't repeat. But if i select another day the colors of the legend doesn't change and show alyways the same eight colors, which repeat of there are more then eight customers. Here is a piece of my code:
public class BarChartSample extends Application
{
StackPane chartPane;
StackPane comboboxPane;
BorderPane layout;
Scene scene;
StackedBarChart<String, Number> bc;
ComboBox<String> combobox;
ArrayList<Date> listDates;
ArrayList<Customer> listCustomers;
HashMap<String, ArrayList<XYChart.Series<String, Number>>> listData;
ArrayList<Day> listDays;
ObservableList<String> xCategories =
FXCollections.<String> observableArrayList("6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19");
StackedBarChart<String, Number> chart;
@Override
public void start(Stage stage) throws Exception
{
try
{
boolean dataExists = getDataSortedByDay();
if (dataExists)
{
stage.setTitle("Title");
ArrayList<String> weekdays = dateToWeekday(listDates);
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Lieferstunde");
xAxis.setCategories(xCategories);
yAxis.setLabel("Anzahl Dokumente");
chart = new StackedBarChart<String, Number>(xAxis, yAxis);
chart.setAnimated(false);
chart.applyCss();
listData = new HashMap<String, ArrayList<XYChart.Series<String, Number>>>();
// create for every day a new chart and save to hashmap
for (Day day : listDays)
{
// create for every customer of a day a new series
ArrayList<XYChart.Series<String, Number>> listSeries = new ArrayList<XYChart.Series<String, Number>>();
for (Customer customer : day.getListCustomers())
{
XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
series.setName(customer.getName());
series.getData().add(new XYChart.Data<String, Number>(String.valueOf(customer.getHour()), customer.getNumDocs()));
listSeries.add(series);
}
listData.put(dateToWeekday(day.getDate()), listSeries);
}
chartPane = new StackPane();
chart.getData().addAll(listData.get(weekdays.get(0)));
chartPane.getChildren().add(chart);
comboboxPane = new StackPane();
comboboxPane.getChildren().add(createComboBoxMenu(weekdays));
layout = new BorderPane();
layout.setTop(comboboxPane);
layout.setCenter(chartPane);
scene = new Scene(layout, 1000, 800);
stage.setScene(scene);
stage.centerOnScreen();
stage.show();
chart.setTitle(weekdays.get(0));
changeColorsOfChart(chart);
}
else
{
// no data found
}
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ComboBox<String> createComboBoxMenu(ArrayList<String> weekdays)
{
ContextMenu contextMenu = new ContextMenu();
for (String date : weekdays)
{
MenuItem menuItem = new MenuItem(date);
contextMenu.getItems().add(menuItem);
}
combobox = new ComboBox<String>(FXCollections.<String> observableArrayList(weekdays));
combobox.setContextMenu(contextMenu);
combobox.valueProperty().addListener(new ChangeListener<String>()
{
@Override
public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue)
{
chart.getData().clear();
chart.getData().addAll(listData.get(newValue));
changeColorsOfChart(chart);
}
});
return combobox;
}
public void changeColorsOfChart(StackedBarChart<String, Number> chart)
{
ArrayList<Color> listColors = new ArrayList<Color>();
for (int i = 0; i < chart.getData().size(); i++)
{
Set<Node> seriesNodes = chart.lookupAll(".series" + i);
String rgb = randomColorRgbFormat();
while (listColors.contains(rgb))
{
rgb = randomColorRgbFormat();
}
for (Node n : seriesNodes)
{
n.setStyle("-fx-background-color: rgb(" + rgb + "); ");
}
}
}
public String randomColorRgbFormat()
{
Color color = new Color(Math.random(), Math.random(), Math.random(), 0);
return String.format("%d, %d, %d, 1.0", (int) (color.getRed() * 255), (int) (color.getGreen() * 255),
(int) (color.getBlue() * 255));
}
public static void main(String[] args)
{
launch(args);
}
I already looked up a lot of on stackoverflow and other websites and found jewelseas example. But i can't solve the problem when the data is always changing. If you need further information to help me, please let me know!