Why clone XYDataItem when adding it to XYSeries?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
stefan.vahlgren
Posts: 2
Joined: Wed Feb 08, 2017 10:43 pm
antibot: No, of course not.

Why clone XYDataItem when adding it to XYSeries?

Post by stefan.vahlgren » Sun Feb 12, 2017 3:28 pm

When adding a XYDataIem to a XYSeries, the dataItem is cloned into a new instance, and then this clone is added to the data List instead of the provided object in the argument. Why is this cloning done? The user of the add method of course assumes that the provided object shall be added, and not a clone.

As I use the add method, I need to have the reference to that exact object as I use it to update the coordinates at a later stage.
There are two ways around this though if there should be a reason for the clone.

1: fetch the added XYDataItem from the series data List. This can be done by first fetching the index using the dataItems x value, and then perform a data.get with that index. This is probably only possible if the allowDuplicateXValues is false

2. Override the XYDataItem clone method so that it isn't cloning and instead sending back itself (return this;).

Both methods above seems to work for how I am using jfreechart without having any exceptions.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Why clone XYDataItem when adding it to XYSeries?

Post by david.gilbert » Mon Feb 13, 2017 7:49 am

I think my original thinking was to prevent outside code from changing the state of the XYSeries without the series instance knowing about it. For example, the minY and maxY values won't be updated if you change the y-value directly in an XYDataItem, nor will a SeriesChangeEvent be sent to the XYSeriesCollection instance that the series belongs to (if any) which would mean no automatic chart update.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

stefan.vahlgren
Posts: 2
Joined: Wed Feb 08, 2017 10:43 pm
antibot: No, of course not.

Re: Why clone XYDataItem when adding it to XYSeries?

Post by stefan.vahlgren » Mon Feb 13, 2017 9:10 pm

Thanks for the reply David.

What I want to achieve is to frequently and as efficient as possible update already added XYDataItem's. So lets say I have a large amount of already added XYDataItems in a series and I want to update half of them with a new y value. If using the addOrUpdate for each of the updated dataitem, this lead to equal amount of rerendering the chart (and the rest of the addOrUpdate method) , when in my case one rerendering whould be enough.

Giving my scenario, can you consider removing the cloning of the XYDataItem and another method where the SeriesChangeEvent are called and the minY and maxY values updated? Or have I possibly missunderstood how it is supposed to work.

I think it would be a good idea to support both ways of doing this (the current and the way I am suggesting)

Thanks in advance and for a great chart tool!

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Why clone XYDataItem when adding it to XYSeries?

Post by paradoxoff » Mon Feb 13, 2017 9:56 pm

If I want to frequently and efficiently manipulate data, I would probably write my own dataset implementation.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Why clone XYDataItem when adding it to XYSeries?

Post by david.gilbert » Tue Feb 14, 2017 8:41 am

If you want to make bulk changes to a dataset without repainting the chart for each update, you can call chart.setNotify(false) first, make all the dataset changes, then call chart.setNotify(true) at the end (which will trigger one repaint).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked