Stacking values from the same series

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Captain Chaos
Posts: 3
Joined: Wed Jun 11, 2008 12:41 pm
Location: Amersfoort, The Netherlands
Contact:

Stacking values from the same series

Post by Captain Chaos » Wed Jun 11, 2008 5:46 pm

I'm using JFreeChart 1.0.10 in an application I'm writing for keeping track of bank accounts. The chart I'm currently working on plots transactions in € (range axis) vs date (domain axis) using a StackedXYBarRenderer.

My challenge is to get the bars for transactions that happened on the same day to stack on top of each other. Is that possible? Note that I'm not talking about stacking items from different series, but items from the same series that have the same X value. Currently these are drawn over each other.

What I would like to achieve is that the total height of each stack of bars represents the total amount of money transferred on that day, but that each transaction is still clearly identifiable, with its own item label and tooltip, which seems the most intuitive way for a chart like this.

Kind regards,
Pepijn Schmitz[/i]

tharter
Posts: 3
Joined: Mon Jun 09, 2008 7:42 pm

Summarize

Post by tharter » Wed Jun 11, 2008 8:05 pm

I think you are going to have to do the math yourself to come up with a single series of data which is what you want. The charting code is really intended to chart data, not process it. So every value in the series is going to be plotted individually.

My solution to the somewhat similar situation is to do that sort of thing in SQL. In other words have your database do something like 'group by transactionDate' and then you have a series that is all ready to be plotted.

The other advantage to that is, you can get a pretty good reduction in the size of data you're pulling out of the database. Getting ALL the rows might be a lot of rows, but the summary could be MUCH smaller. The RDBMS is likely to be pretty fast at summarizing if the schema is fairly simple, so it shouldn't cost much CPU on the database side, but it will reduce network traffic a lot.

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

Re: Stacking values from the same series

Post by david.gilbert » Wed Jun 11, 2008 9:13 pm

Captain Chaos wrote:What I would like to achieve is that the total height of each stack of bars represents the total amount of money transferred on that day, but that each transaction is still clearly identifiable, with its own item label and tooltip, which seems the most intuitive way for a chart like this.
It seems like a sensible requirement, but there is no current renderer in JFreeChart that will do this. You could write a custom renderer that detects when there are multiple items for the same x-value (range). This would be easiest if you know that the dataset is ordered, since the renderer could just check the previous item(s) to know if stacking is required...but unfortunately the IntervalXYDataset interface does not guarantee any particular order (although you can at least query the order with the getDomainOrder() method).

If you get something working for this, it would be great if you were able to contribute it to the JFreeChart project...otherwise, I'll add this to my long to-do list and get to it eventually.
David Gilbert
JFreeChart Project Leader

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

Captain Chaos
Posts: 3
Joined: Wed Jun 11, 2008 12:41 pm
Location: Amersfoort, The Netherlands
Contact:

Re: Stacking values from the same series

Post by Captain Chaos » Wed Jun 11, 2008 9:17 pm

david.gilbert wrote:
Captain Chaos wrote:What I would like to achieve is that the total height of each stack of bars represents the total amount of money transferred on that day, but that each transaction is still clearly identifiable, with its own item label and tooltip, which seems the most intuitive way for a chart like this.
It seems like a sensible requirement, but there is no current renderer in JFreeChart that will do this. You could write a custom renderer that detects when there are multiple items for the same x-value (range). This would be easiest if you know that the dataset is ordered, since the renderer could just check the previous item(s) to know if stacking is required...but unfortunately the IntervalXYDataset interface does not guarantee any particular order (although you can at least query the order with the getDomainOrder() method).
I like to think I'm a half-decent Java programmer, so I'll take a crack at it. Can you give me some more pointers as to where best to start, which classes to extend, that sort of thing?

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

Post by david.gilbert » Wed Jun 11, 2008 9:29 pm

In theory, you should be able to copy XYBarRenderer (to a new file, say MyXYBarRenderer) and modify just the drawItem() method. The main thing you need to figure out is whether or not you need to adjust value0 and value1 (near the start of the method) for previous items with the same x-interval. It gets slightly more complicated if your data allows negative values (which it probably does).

You could also just subclass XYBarRenderer and override the drawItem() method.
David Gilbert
JFreeChart Project Leader

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

Captain Chaos
Posts: 3
Joined: Wed Jun 11, 2008 12:41 pm
Location: Amersfoort, The Netherlands
Contact:

Post by Captain Chaos » Wed Jun 11, 2008 10:45 pm

david.gilbert wrote:In theory, you should be able to copy XYBarRenderer (to a new file, say MyXYBarRenderer) and modify just the drawItem() method. The main thing you need to figure out is whether or not you need to adjust value0 and value1 (near the start of the method) for previous items with the same x-interval. It gets slightly more complicated if your data allows negative values (which it probably does).

You could also just subclass XYBarRenderer and override the drawItem() method.
I just realized it gets even more complicated. I also want to be able to display multiple accounts on one graph, also stacked on each other, so multiple series that won't have the same set of x-values, since each account will have transactions on different dates.

Is there some way to do that already, or would I have to build that too?

Locked