Help with margins and spacing on horizontal graph

38 views
Skip to first unread message

agrikk

unread,
Jun 22, 2020, 5:11:37 PM6/22/20
to JpGraph
I have a scorekeeping application that I am attempting to add a horizontal graph to but I am having problems getting the spacing right so that the text doesn't overlap the horizontal bar:

Capture.PNG


Capture.PNG

As you can see, using a wide left marking to try to accommodate negative scores ends up with the names pushed off the chart for a game with positive numbers.

I'm sure there I can set it so that the margin is set based on the minimum score if it is negative, or a default value if all of the scores are positive, but I'm not quite sure how to go about it.


agrikk

unread,
Jun 22, 2020, 5:36:00 PM6/22/20
to JpGraph

Capture.PNG

Another version where there is a large delta between the top score and a negative score forces the player names out of the graph area.

agrikk

unread,
Jun 24, 2020, 12:38:18 PM6/24/20
to JpGraph
I solved this by catching various cases:


// I first set the margin when all scores are positive
$margin=10;

// I calculate the width between the top score and the bottom score to use in calulating the margins in the cases below
if ( abs(max($datay))+abs(min($datay)) == 0 ) {
$spreadRatio = 0;
} else {
$spreadRatio = abs(min($datay))/(abs(max($datay))+abs(min($datay)));
}
// if the maximum score is positive and the minimum score is negative then apply a combination of the chart width times the chart gap between top score and bottom score
if (min($datay)<0) {
$margin=$spreadRatio * ($width-$left-$right)+10;
}
// if all scores are negative then margin is just the space to the left of the chart

if (max($datay)<0) {
$margin=($width-$left-$right)+10;
}

Reply all
Reply to author
Forward
0 new messages