如何使用jfreechart检查图表类型

How to check the type of chart using jfreechart

我正在使用 Jfreechart 来构建和显示图表。 我想知道,似乎没有标准的方法来检查创建图表的类型(例如条形图、散点图等)。 目前我正在检查底层图表以获取图表类型,但我相信在比较散点图和折线图时此选项是有限的。 有没有更好的方法在创建图表后区分图表类型?

编辑: 基本上我想在图表上应用数据。我正在使用不同的图表(比如 ScatterChart 和 XYLineChart)。我有一种方法,它应根据图表类型应用数据。两个图表具有相同的绘图类型,所以我无法区分它们。需要区分,因为我需要更改散点图的形状和线条样式。 我已经通过使用不同的方法应用数据解决了这个问题。但这实际上是一个肮脏的解决方法。我遇到了几个问题,区分图表类型对我来说非常有用。 这就是为什么我问,如果有标准的方法,获取 JFreeChart 对象的图表类型

Is there any better way to differentiate the chart type after it has been created?

API provides no such distinction. The combinations of dataset (model) and renderer (view) constructed by the static factory methods in ChartFactory 很常见,但绝不是详尽无遗的。这些名称只是描述性的。

I'm using different charts, lets say ScatterChart and XYLineChart…Both charts have the same plot type, so I can't distinguish them.

特别是,两家工厂都在 XYPlot 中构建了一个 XYDataset 的图表,并结合了 XYLineAndShapeRenderer。如果不希望进行转换,请避开工厂方法并手动构建图表,如 here.

所示

更一般地说,考虑使用 strategy pattern, cited and illustrated ,以 select 在运行时实现所需的行为。 enum 您选择的图表类型在这种情况下会很好地工作。