I'm using Jfreechart to build & display charts. I'm wondering, that it seems, there is no standard way, to check the type of created chart (e.g. bar, scatter etc). currently im checking the underlying plot to get the chart type, but i believe this option is limited, when comparing scatter chart with line chart. Is there any better way, to differ the chart types after it has been created?
Edit: Basically i want to apply data on charts. I'm using different charts (lets say ScatterChart and XYLineChart). I have one method, which shall apply the data based on the chart type. Both charts have the same plot type, so i cant distinguish them. The distinction is needed, as i need to change the shapes and line styles for the scatter chart. I already solved this, by using different methods to apply the data. But thats actually a dirty workaround. I came across on several issues, where distinguishing chart types would be very useful in my case. Thats why im asking, if there is a standard way, to obtain the chart type of an JFreeChart object
Is there any better way to differentiate the chart type after it has been created?
The API provides no such distinction. The combinations of dataset (model) and renderer (view) constructed by the static factory methods in ChartFactory
are common, but by no means exhaustive. The names are descriptive only.
I'm using different charts, lets say ScatterChart and XYLineChart…Both charts have the same plot type, so I can't distinguish them.
In particular, both factories construct a chart with an XYDataset
in an XYPlot
, combined with an XYLineAndShapeRenderer
. If casting is undesireable, eschew the factory method and construct the chart manually, as shown here.
More generally, consider using the strategy pattern, cited here and illustrated here, to select the desired behavior at runtime. An enum
of your chosen chart types would work well in this context.