Closing up blank space in JFreeChart bar chart
Asked Answered
M

1

6

I am using JFreeChart and would like to display a bar chart of player's scores, with the score on the y-axis and the player's games grouped on the x-axis.

e.g.

String[] {Player name, score, game number}
Player 1, 10 , 1
Player 1, 12 , 2
Player 1, 15 , 3
Player 2, 11 , 1
Player 3, 18 , 1

Because the players do not have to play the same number of games, this results in a lot of blank space when the dataSet is created, as it tries to plot a bar for Player 2 & 3 games 2 & 3.

data.addValue(score, game number, player name);

Output: (the numbers dont quite match, this was just a quick test I knocked up) Note the blank space in player 1 game 3 and player 3 games 1 and 2

Can anybody help me with how to close up this blank space? In theory player 1 could go on to play 100s of games with player 2 and 3 playing only a few, so it would look quite ridiculous! I am new to JFreeChart so there is probably an obvious solution!

Thank you in advance for your help.

Missie answered 23/5, 2013 at 23:30 Comment(5)
You should change it to a stacked graph java2s.com/Code/Java/Chart/JFreeChartStackedBarChartDemo1.htmLegendary
Thanks, but from looking at your link it seems that the stacked graph is more suited to displaying the total number of points scored across all games, whereas I would prefer to show each game side by side if possible.Missie
don't know if this will help, but in some of my code, to prevent display I set the values to null, rather than just not including them. If a line graph this allows two points not be joined if the data in between is missingLegendary
@user2310289, thanks for your comment. I tried setting the empty slots to null manually, but unfortunately the space was not closed up!Missie
@Missie Can you please check my question? #28690594Cunctation
T
16

First Look at the Picture carefully enter image description here

Here is some Explanation w.r.t Numbers.

  1. setLowerMargin(Double margin).
  2. setUpperMargin(Double margin).
  3. setCategoryMargin(Double margin).
  4. setItemMargin(Double margin).

Here is How you can use the methods in your chart

 CategoryPlot p = chart.getCategoryPlot(); 

 CategoryAxis axis = p.getDomainAxis();
 axis.setLowerMargin(0.1);
 axis.setUpperMargin(0.1);
 axis.setCategoryMargin(0.1);
 BarRenderer renderer = (BarRenderer) p.getRenderer();
 renderer.setItemMargin(0.1); 

You can set the value between 0.0 to 1.0 (example 0.1 means 10%)

Hope this Helps

(Update After your comment) Well in this Case you should use Layered Bar Chart

enter image description here

Tarazi answered 24/5, 2013 at 15:47 Comment(2)
Thanks, this is helpful for using JFreeChart in general. However it does not solve my specific problem. In your example, games player 0,1 and 2 have all played 3 games each. In my system, the players do not have to play the same amount of games, so it is not a case of the margins, rather that there is no data for some of the columnsMissie
Thanks again, I'l implement this for now. I would really like to use the grouped 3d bars though as I think they look a lot better and are easier to understand.Missie

© 2022 - 2024 — McMap. All rights reserved.