make any positive bar start at 0 in waterfall chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
fm64
Posts: 5
Joined: Thu Feb 25, 2016 3:43 pm
antibot: No, of course not.

make any positive bar start at 0 in waterfall chart

Post by fm64 » Thu Feb 25, 2016 4:07 pm

Hi I am using a waterfall chart with positive and negative values. the user may add new positive or negative bars dinamically. What I need is that when a positive bar (blue bars) is added, it has to start at 0 in the Y axis, something similar to the "subtotal" bars in excel. I need to avoid this behaviour (see image) that is that the "Tiempo final" bar starts at the beginning and not in the middle.
I am using a DefaultCategoryDataset with positive and negative valuses and a modified copy of the WaterfallChartDemo.java.

Is there any way to accomplish this? It's quite urgent.

Image

Thank you.

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

Re: make any positive bar start at 0 in waterfall chart

Post by david.gilbert » Thu Feb 25, 2016 10:03 pm

I didn't fully understand your description of the problem.
David Gilbert
JFreeChart Project Leader

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

fm64
Posts: 5
Joined: Thu Feb 25, 2016 3:43 pm
antibot: No, of course not.

Re: make any positive bar start at 0 in waterfall chart

Post by fm64 » Thu Feb 25, 2016 10:47 pm

Hi, what I need is that every blue bar (which will always contain a positive value) instead of starting where the values are being substracted or added, it must start from the 0 in the Y axis, just like the first and last bars. That is, I want every blue bar to be like the first and last bars, so when you add a blue bar, it has to start from Xaxis = 0, not Xaxis=lastbar.

see the picture for reference:

https://officeblogseast.blob.core.windo ... hart-8.png (sorry you have to download it)

I have tried overriding a method so when the value is positive, it moves the bar at the start of the axis, I don't know if that is possible.

I hope this made it more clear, Thank you

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

Re: make any positive bar start at 0 in waterfall chart

Post by david.gilbert » Thu Feb 25, 2016 11:04 pm

OK, I think I understand now. This would require a change to the renderer...I don't think it would be a hard change to make. In both drawItem() and findRangeBounds() you would need to adjust the running total to reset it to zero each time the data value switches from negative to positive. I don't think it would take much more than that, give it a try and see how it goes.
David Gilbert
JFreeChart Project Leader

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

fm64
Posts: 5
Joined: Thu Feb 25, 2016 3:43 pm
antibot: No, of course not.

Re: make any positive bar start at 0 in waterfall chart

Post by fm64 » Fri Feb 26, 2016 1:30 pm

Hi, thanks for your quick response. The thing is that I am completely new to Jfreechart (it's my second day working with it) and still don't know all the parameters very well. I am currently trying to override those methods by adding a condition in certain parts of the methods, but it seems that the setSertiesRunninTotal() method in drawItem() is private so I don't have access from the renderer.

EDIT: we may not need to override the findRangeBounds method since that affects just the plot, instead I tried using this after defining the orientation inside drawItem():

Code: Select all

                if (dataset.getValue(row, column).doubleValue() > 0) {
                    rectX = 0;
                }
with that modification, I succesfully put any positive bar at the start of the axis, however since state.setSeriesRunningTotal(current); is never called the negative bars are not shown

Image

this^ is the behaviour I was looking for the blue bars



EDIT2: managed to override setSeriesRunningTotal() using reflection, now the red bars are visible but they are a little away:

Image

I also checked the vertical orientation and it's a little messed up, I am trying yo modify the values of the coordinates in drawItem(), but i am a little confused about how j2dy0 and j2dy1 work.
Please help, I have been stucked for days

Locked