codebreaker
codebreaker

Reputation: 813

Constant colors on a PieChart

I have a JavaFX PieChart, and I want to have a color associated with specific regions. However, it seems like I can only have colors associated with the order that the Data is added to the chart. For example, if I want to plot the colors of cars in a parking lot, I could do this:

.default-color0.chart-pie { -fx-pie-color: #FF0000; }
.default-color1.chart-pie { -fx-pie-color: #00FF00; }
.default-color2.chart-pie { -fx-pie-color: #0000FF; }
.default-color3.chart-pie { -fx-pie-color: #FFFF00; }
.default-color4.chart-pie { -fx-pie-color: #00FFFF; }

As long as I add my "red car" data first, and then the "green car" data, etc, everything is fine. But If there are no red cars, and I don't add that Data, then the green cars become red, as they are the first data point. I could add a Data("Red", 0), but then that shows up in my PieChart as a slice with zero area, but it still has a label, and it could be confusing. Is there any way to avoid this? Either to mark Data objects with zero data as invisible, or assign constant colors to categories?

Upvotes: 1

Views: 1888

Answers (2)

brian
brian

Reputation: 10969

Your solution still just has you adding the items in order of the colors. It's not hard to get a reference to the legend. There's no way to add a color to the PieChart.Data but you could make your own class.

import com.sun.javafx.charts.Legend;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class CustomPie extends Application {
    public static void main(String[] args) {launch(args);}

    @Override
    public void start(Stage primaryStage) {
        PieData pieData = new PieData();
        pieData.add(new PieChart.Data("Grapefruit", 13d), Color.AQUA);
        pieData.add(new PieChart.Data("Oranges", 25d), Color.ALICEBLUE);
        pieData.add(new PieChart.Data("Plums", 10d), Color.AQUAMARINE);
        pieData.add(new PieChart.Data("Pears", 22d), Color.BLUE);
        pieData.add(new PieChart.Data("Apples", 30d), Color.BLUEVIOLET);

        final MyPie pieChart = new MyPie(pieData.pieChartData);
        Scene scene = new Scene(new VBox(pieChart));
        primaryStage.setScene(scene);
        primaryStage.show();

        for (PieChart.Data data : pieData.pieChartData) {
            int idx = pieData.pieChartData.indexOf(data);
            Color color = pieData.pieChartColors.get(idx);
            data.getNode().setStyle("-fx-pie-color: " + color.toString().replace("0x", "#") + ";");
            pieChart.legend.getItems().get(idx).setSymbol(new Rectangle(8, 8, color));
        }
    }
}

class MyPie extends PieChart {
    public Legend legend;

    public MyPie(ObservableList<PieChart.Data> pieChartData) {
        super(pieChartData);
        legend = (Legend) getLegend();
    }
}

class PieData {
    ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
    ObservableList<Color> pieChartColors = FXCollections.observableArrayList();

    public void add(PieChart.Data data, Color color){
        pieChartData.add(data);
        pieChartColors.add(color);
    }
}

I haven't checked if adding/removing data confuses it but the data structure could be improved.

Upvotes: 0

codebreaker
codebreaker

Reputation: 813

Okay, here's what I ended up doing (and it works):

Platform.runLater(new Runnable() {
  @Override
  public void run() {
    for (int i = 0; i < usedColors.size(); i++) {
      for (Node node : chart.lookupAll(String.format(".default-color%d.chart-pie", i))) {
        node.setStyle(String.format("-fx-pie-color: #%06x;", usedColors.get(i)));
      }
    }
  }
});

Where usedColors is a List containing the correct colors in order. This will affect the legend as well. Using runLater is necessary.

And thanks to everyone in the comments for your help.

Upvotes: 1

Related Questions