Is it possible to customize label for each category in bar chart?

java, jfreechart

Solution

The answer depends on how your chosen `CategoryDataset` implements the `KeyedValues2D` interface. The interface expects the keys to be unique, and the default implementation, `DefaultKeyedValues2D`, requires that the keys be `Comparable` and immutable.

Unique `String` instances are the typical concrete parameter type, but nothing in `JFreeChart` enforces the unique constraint. One approach wold be to wrap your `String` in a class that implements `Comparable` and also enforces uniqueness. The `class Value` is an example that leverages the underlying `Double` implementation. Your implementation would require an additional attribute to distinguish one `project` form another, perhaps using the primary key of the source relation. You could override `toString()` to get a formatted representation of the name.

Problem

Recently I get a requirement to create a bar chart that show data for each project. Here's an example : As you can see, the `Category` is the name of the project, and `Series` are different types of data in that project. However, since system dose not guarantee the uniqueness of project name, using it as categories can cause problem, and I will not able to use project name to generate URL for different projects. On the other hand, If I use unique id as category, I won't able to display the project name. This put me into a difficult situation. So my problem is : Is there a way to generate customize category label on the fly in JFreeChart ? Something similar to `CategoryItemLabelGenerator` but for category itself. So I can use unique Id as category, but display project name in chart.

Original source