BarChart with horizontal Bars - manipulate TickLabel's width

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
azrael
Posts: 9
Joined: Fri Jan 20, 2012 1:02 pm
antibot: No, of course not.
Location: Germany

BarChart with horizontal Bars - manipulate TickLabel's width

Post by azrael » Fri Jan 20, 2012 1:39 pm

Hi,

I'm new into jfreecharts and I'm trying to setup some diagrams - exported as buffered Image - on a webpage. Each diagramm is a single graphic but I want them to be diaplayed in some kind of accordence zu each other. I'm using version 1.0.13

To illustrate what's the goal an what I actually get, please consider the folowing (in real there are more diagrams than two...):

Actual:

Code: Select all

Starting with some lines of text to introduce the diagrams...

   Apples  +XXXXXX
           |
  Oranges  +XXXXXXXXXXXXXX
           |
  Bananas  +XXXXXXXXX
           *-----+-----+-----+-----+

Some more lines of text...

  Pineapples  +XXXXXX
              |
  Tangarines  +XXXXXXXXXXXXXX
              |
    Coconuts  +XXXXXXXXX
              *----+----+----+----+
For each diagram, the category labels - resprectively their width - are calculated by the longest name. In fact of this, the bar area starts at a different point from the left for each diagram. But the goal I'm trying to achive should be nearest to the following:

Goal:

Code: Select all

Starting with some lines of text to introduce the diagrams...

      Apples  +XXXXXX
              |
     Oranges  +XXXXXXXXXXXXXX
              |
     Bananas  +XXXXXXXXX
              *----+----+----+----+

Some more lines of text...

  Pineapples  +XXXXXX
              |
  Tangarines  +XXXXXXXXXXXXXX
              |
    Coconuts  +XXXXXXXXX
              *----+----+----+----+
I want to manipulate the calculation for the width of the tick labels to grant the label area something like a minimum width or a fixed width. I have searched the documentation and the examples, but sadly haven't found anything. Is the any chance, to get my plans working? Momentary I'm using ChartFactory.createBarChart with PlotOrientation.HORIZONTAL to create the chart.

In the context of that, choping too long tick labels if adding a fixed width - and adding some nice linebreaks in that case - would be a kind of heaven on earth^^

Thanks for any help and advice in advance...

azrael
Posts: 9
Joined: Fri Jan 20, 2012 1:02 pm
antibot: No, of course not.
Location: Germany

Re: BarChart with horizontal Bars - manipulate TickLabel's w

Post by azrael » Wed Jan 25, 2012 5:01 pm

Nobody any idea??

At the moment I'm trying to find out, if its possible to handle the issue by manipulating CategoryAxis.reserveSpace() and how. Unfortunately without success so far.

Has noone tried to do what I want to do? Or do I need to provide more information on my goals?

Any help appreciated - thanks in advance...
Last edited by azrael on Thu Jan 26, 2012 9:38 am, edited 1 time in total.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: BarChart with horizontal Bars - manipulate TickLabel's w

Post by matinh » Thu Jan 26, 2012 8:41 am

Hi!

I'm afraid, there is no such feature in JFreeChart providing what you want out of the box. Similar requests have been asked before in this forum. Maybe you could find some helpful information if you dig through the posts.

If you know the categories in each chart in advance, you could eventually try playing around with adding some insets on some of the charts. This is similar to HTML/CSS paddings. Not sure, if this would suite your needs though.

Regarding line-breaks: this is also not supported at the moment. But IIRC there were already patches or workarounds for this. Try searching this forum and the patch- resp. feature-tracker.

hth,
- martin

azrael
Posts: 9
Joined: Fri Jan 20, 2012 1:02 pm
antibot: No, of course not.
Location: Germany

Re: BarChart with horizontal Bars - manipulate TickLabel's w

Post by azrael » Thu Jan 26, 2012 9:32 am

Hi Martin,

thanks for your reply! I am aware, there's no such feature implementet yet - so I'm trying (and hoping) to write some piece of code on my own... :)
matinh wrote:If you know the categories in each chart in advance, you could eventually try playing around with adding some insets on some of the charts. This is similar to HTML/CSS paddings. Not sure, if this would suite your needs though.
The charts, I'm displaying are for financial concerns, so one of them for example lists country names for categories and another one lists types of securities. Sadly I just can't determine the category names in advance - it's from one up to eight countries out of hundreds possibilities (similar case for each chart). So Ineed some kind of automatism for the calculation...

But maybe I can get the text meassures für the different names and possibly calculate the margins direct at that point, where I build up the Dataset..?! Or I could modify the name by inserting manually line-breaks an white-spaces... sounds like a huge bunch of calculations...
matinh wrote:Regarding line-breaks: this is also not supported at the moment. But IIRC there were already patches or workarounds for this. Try searching this forum and the patch- resp. feature-tracker.
Thanks for these hints... searching the trackers didn's cross my mind. Hopefully I'm able to find a suitable piece of code there - line-breaking the longest category names would be half the battle! :)

What about my idea of manipulating the function CategoryAxis.reserveSpace()? Any experiences in this regard? I'm afraid, if I override an axis class, I need to override much more.. beginning with the plot class an ending up with the ChartFactory..?!
Last edited by azrael on Thu Jan 26, 2012 9:58 am, edited 2 times in total.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: BarChart with horizontal Bars - manipulate TickLabel's w

Post by matinh » Thu Jan 26, 2012 9:51 am

azrael wrote:What about my idea of manipulating the function CategoryAxis.reserveSpace()? Any experiences in this regard?
I don't know this method but it sounds reasonable to modify it for your needs.
azrael wrote: I'm afraid, if I override an axis class, I need to override much more.. beginning with the plot class an ending up with the ChartFactory..?!
Creating your own axis class shouldn't require any changes in any other class.

- martin

azrael
Posts: 9
Joined: Fri Jan 20, 2012 1:02 pm
antibot: No, of course not.
Location: Germany

Re: BarChart with horizontal Bars - manipulate TickLabel's w

Post by azrael » Thu Jan 26, 2012 3:41 pm

ok... the issue with the min-width seems to be fixed...

I made a derivation of CategoryAxis and inserted a new member (with setter and getter) for minWidth. Afterwards I overrode the function reserveSpace():

Code: Select all

	
	private double minWidth = 150.0;

	public double getMinWidth() {
		return minWidth;
	}

	public void setMinWidth(double value) {
		this.minWidth = value;
	}

	@Override
	public AxisSpace reserveSpace(Graphics2D g2, Plot plot, Rectangle2D plotArea, RectangleEdge edge, AxisSpace space) {

		space = super.reserveSpace(g2, plot, plotArea, edge, space);

		if (RectangleEdge.isLeftOrRight(edge)) {
			double left = space.getLeft();
			if (left < minWidth) {
				space.setLeft(minWidth);
			}
		}

		return space;
	}
Come to think of it, the solution was quite easy... needed just to find the spot to lever it up! Probably I could do little more tuning (don't know, if it works fine with the axis on the right...) but it suits for me. Well... from this point on, I'm going to try inserting some line-breaks if the label ist too long for the minWidth (making it a desiredWidth)... seems like the function drawCategoryLabels() suits to this...

thanks again to matinh - I've fond a snippet to regard "\n" within the label's caption as line-break... although it's for the ValueAxis and without automatism pending on font metriks, I think it'll be a usable base for my concerns.

keeping you current...

Locked