是否可以为条形图中的每个类别自定义标签? [英] Is it possible to customize label for each category in bar chart?
问题描述
最近我要求创建一个显示每个项目数据的条形图。这是一个例子:
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.
但是,由于系统剂量不能保证项目名称的唯一性,因此将其用作类别可能会导致问题,我将无法使用项目名称为不同的项目生成URL。另一方面,如果我使用唯一ID作为类别,我将无法显示项目名称。这让我陷入困境。
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.
所以我的问题是:
有没有办法生成自定义JFreeChart中的类别标签?
Is there a way to generate customize category label on the fly in JFreeChart ?
与 CategoryItemLabelGenerator
类似,但类别本身。所以我可以使用唯一ID作为类别,但在图表中显示项目名称。
Something similar to CategoryItemLabelGenerator
but for category itself. So I can use unique Id as category, but display project name in chart.
推荐答案
答案取决于您选择的 CategoryDataset
实现 KeyedValues2D
界面。接口期望密钥是唯一的,默认实现 DefaultKeyedValues2D
,要求密钥为 Comparable
且不可变。
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.
唯一字符串
实例是典型的具体参数类型,但 JFreeChart
强制执行唯一约束。一种方法是将 String
包装在一个实现 Comparable
的类中,并强制执行唯一性。 类值
是示例利用底层的 Double
实现。您的实现需要一个额外的属性来区分一个项目
形成另一个,可能使用源关系的主键。您可以覆盖 toString()
以获取名称的格式化表示。
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.
这篇关于是否可以为条形图中的每个类别自定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!