Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper way to iterate over datasets in XYPlot? #201

Open
lynnskii opened this issue Jan 5, 2021 · 7 comments
Open

Proper way to iterate over datasets in XYPlot? #201

lynnskii opened this issue Jan 5, 2021 · 7 comments

Comments

@lynnskii
Copy link

lynnskii commented Jan 5, 2021

I have noticed that datasets are stored in a map by index in XYPlot. However, there is no exposed way of iterating over the datasets other than getDataset(int index). The rub is that getDatasetCount() returns the number of datasets in the map, not the maximum index value.

I have plots that have optional datasets, so there may be gaps in the indices. For instance: datasets={0=BaseDataset, 2=Dataset2}. There is no dataset with index=1. When you try to iterate over the available datasets in a generic way, there's no way to know what dataset indices are actually available for the plot:

for (int i=0; i<plot.getDatasetCount(); i++) { // plot.getDatasetCount() = 2
XYDataset d = plot.getDataset(i); // i will be 0 and 1, so you never get dataset 2.
}

To properly obtain all a plot's datasets when you don't know what the indices may be, should getDatasetCount() return the maximum value of the datasets map's keySet()? Or other methods provided to return, say, an unmodifiable collection of the datasets map's values()? Internally to XYPlot, this is how iteration is done: for (XYDataset dataset: this.datasets.values()) { ... }

The same issue applies to the other maps by index in XYPlot: getRendererCount(), getDomainAxisCount(), getRangeAxisCount()

@permiakover
Copy link

Usually I wrap such kind of multiple dataset charts in a class with a dataset-fields. I suppose it's a bad practice to obtain data that already stored in dataset, in your way datasets are just rendered UI

@lynnskii
Copy link
Author

lynnskii commented Jan 8, 2021

Yeah, I have a set of tools for formatting and operating on XYPlots generically , so I have to rely on the XYPlot being able to report what datasets are available. It hadn't been a problem until I ran into the situation I described where I don't have "consecutive" dataset indices.

@jfree
Copy link
Owner

jfree commented Mar 22, 2021

Good point. I've added methods to XYPlot and CategoryPlot to return unmodifiable maps of the datasets and renderers.

@lynnskii
Copy link
Author

Thanks, David. We probably need similar methods for the domainAxes and rangeAxes maps (and any other index-based maps there may be).

@jfree
Copy link
Owner

jfree commented Mar 23, 2021

Methods for the axis maps are added now too.

@lynnskii
Copy link
Author

lynnskii commented Mar 23, 2021

Another thing I noticed was in StandardChartTheme.applyToXYPlot and applytoCategoryPlot, there is iteration over the domain and range axes as well as the renderers that should probably use the new map methods rather than getting a count and getXXX(index).

@lynnskii
Copy link
Author

Also in XYPlot.drawDomainMarkers and drawRangeMarkers, there's an
if (index >= getDatasetCount())
that should probably now be
if (!datasets.containsKey(index))

And also iteration over domainAxes in panDomainAxes and panRangeAxes in XYPlot, CombinedDomainXYPlot, and CombinedRangeXYPlot that should iterate over the axes maps.

There may be more of these types of changes, but these are the ones I had previously identified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants