CategoryPlot width

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

CategoryPlot width

Post by sarsipius » Wed Apr 22, 2009 4:52 pm

Hi,

I would like to know if I could add categories in a CategoryPlot without adujsting the space between categories

I would like to fix a space of 20 pixels and use a scrollbar

Is this possible ?

Thanks

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Thu Apr 23, 2009 7:49 am

Ok so I find the method setCategoryMargin to apply on a CategoryAxis but only the first dataset take account of the margin

Do I need to apply the margin for each dataset?

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

Re: CategoryPlot width

Post by david.gilbert » Thu Apr 23, 2009 9:57 am

JFreeChart doesn't work this way. It just draws the chart at the size it is asked to do (this size is determined externally - sometimes by the LayoutManager that controls the size of the ChartPanel containing the chart, sometimes by the caller of the createBufferedImage() or saveChartAsPNG() methods, sometimes by other code).
David Gilbert
JFreeChart Project Leader

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

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

Re: CategoryPlot width

Post by david.gilbert » Thu Apr 23, 2009 9:59 am

I should add that you could *try* to write some code that adjusts the preferred size of the ChartPanel (if that's what you are using) to take account of the number of categories in the chart.
David Gilbert
JFreeChart Project Leader

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

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Thu Apr 23, 2009 10:04 am

You tell that I could try to enlarge the side of my JPanel and the JFreeChart will adjust its width?

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Thu Apr 23, 2009 2:26 pm

This is my CategoryAxis:

Code: Select all

CategoryAxis xAxis = new CategoryAxis("");
xAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI/2.0));
xAxis.setLowerMargin(0.01);
xAxis.setUpperMargin(0.01);
xAxis.setCategoryMargin(0.1);
This is my ChartPanel:

Code: Select all

cp = new ChartPanel(chart);
cp.addChartMouseListener(this);
cp.setDomainZoomable(false);
cp.setRangeZoomable(false);
cp.setPreferredSize(new Dimension(40*30,1));
And this is my JScrollPane:

Code: Select all

JScrollPane scrollpane = new JScrollPane(panel);
scrollpane.setPreferredSize(new Dimension(1000,1));
Here you can see a screenshot of the chart:

Image

Whatever the CategoryMargin is, the space between the dots is the same

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Fri Apr 24, 2009 9:27 am

I tried to set a preferred size to the chart panel but it's not the solution

I need to fix the length of the domain axis but I don't find a method to do that
I don't even know if it's possible...

In next version coulid it be possible to not auto adjust the size of the plot? :oops:

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

Re: CategoryPlot width

Post by paradoxoff » Fri Apr 24, 2009 9:30 pm

The "category margin" is NOT an absolute size that defines the size between categories, but is the percentage of the total space that is used for the margin (hence the default value of 0.2 which is 20 %). The "distance between categories" is solely dependent on the lower and upper margin, the available width and the number of categories.
I am not really claer about what you want to achieve, but I think that you want to use a constant distance between categories that is independent of the number of categories. In order to achieve that, the width of the chart will have to grow if more categories are added to the dataset.
A while back, I made a patch that could address this issue (see here). First let me explain what the patch contains:
Additional flag useChartSize in the ChartPanel class. If this is set to false, the ChartPanel will behave as before and draw the chart in the rectangle given by the ChartPanel´s size which in turn might depend on the layout manager.
Additional flags useFixedChartAreaSize and useFixedPlotAreaSize, additional public method getPreferredChartAreaSize() in the JFreeChart class. By the flag useFixedChartAreaSize, it can be indicated that a size should be requested from the chart. If a preferredChartAreaSize is indeed requested by the ChartPanel, the flag useFixedPlotAreaSize comes into play. It indicates which size should be returned. If useFixedPlotAreaSize is false, then a user definable chart size („preferredChartAreaSizeOverride“) is returned. If useFixedPlotAreaSize is true, then the chart asks the Plot for its preferredPlotAreaSize.
If a CategoryPlot or an XYPlot are asked for their preferredPlotAreaSize, they will first retrieve a fixedDataAreaSize. They will then use this size a starting point and add the space for the axes, the axis offsets and the plot insets. This „enlarged size“ is then returned to the JFreeChart. The JFreeChart will then add the space required for the titles and its own inset to this size and return that to the ChartPanel. The ChartPanel then constructs a rectangle from the size and tells the JfreeChart to draw itself inside this rectangle. As a result, you get a chart that has a data area which was defined by the user and which will remain constant even if axes and/or titles are added.
How this might help you? The method that returns the fixedDataAreaSize is defined in the Plot class and simply returns a size that was explicitly set by the user. You could overwrite this method in the CategoryPlot class and return a width that is dynamically calculated based on the available categories of the dataset (good luck with multiple datasets :shock: ), and the margins of the CategoryAxis (upper, lower, and between categories).

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Mon Apr 27, 2009 9:02 am

You're right: I want a fixed width between categories and a scrollbar if there are more categories

Thanks for your patch :)
I'll try to use it

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Tue Apr 28, 2009 8:55 am

it's not that easy ^^;

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Tue Apr 28, 2009 4:23 pm

could someone tell me where the plot size in adjusting?
in the CategoryPlot class? in the JFreeChart class ?

I don't know where tweak the JFreeChart librairy in order to fix a size in pixel between category and adjust the width according to the category number

I hope that in the 1.0.14 version it will be possible to choose to adjust or not. Please :oops:

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

Re: CategoryPlot width

Post by paradoxoff » Tue Apr 28, 2009 10:52 pm

I have uploaded a new patch that also includes the changed sources for the JFreeChart, Plot, XYPlot and CategoryPlot classes. You just need to replace the old files with the patched ones (keep a backup, of course).
For some reason, there was no calculatePreferredPlotAreaSize-method in the CategoryPlot class :oops: .
But now it is there plus additional parameters fixedCategoryWidth and useFixedCategoryWidth. These can be used to make sure that the width of a category is constant, and that the data area grows if more categories are added (i.e. getFixedDataAreaSize in the Plot class has been overwritten).
The zip also includes two demos: an older one for XYPlots and a newer one for CategoryPlots that uses the new parameters mentioned above and creates a bar which is getting wider as more categories are added. The calculations are quite simple. You might want to look at them and adjust them to your needs, e. g. by taking the margins of the domain axis into account.

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: CategoryPlot width

Post by sarsipius » Wed Apr 29, 2009 7:52 am

thnaks again for your help

I just try your demo and I think now I will be able to do what I want

thanks thanks thanks

the patch link is here

Locked