Home » Php » php – Jpgraph wont change colors on my barplot

php – Jpgraph wont change colors on my barplot

Posted by: admin July 12, 2020 Leave a comment


I’m just getting started with jpgraph and XAMPP I was looking to make a barplot.
As I was reading some articles on the web it said you could pass in an array of colors to setfillcolor so that each bar would be a different color. However whenever I pass it color the graph won’t change from its default color of light blue.

The graph respond s to changes in data but not changes to color.
I tried with an accumulated bar graph example and that seems to be color responsive.

I’m not sure how to diagnose a problem like this. Help please!

Code Below(Example from the Jpgraph documentation with color changed from orange to #B0C4DE [orange from the original example doesn’t show up]):

<?php // content="text/plain; charset=utf-8"


// Size of graph

// Set the basic parameters of the graph
$graph = new Graph($width,$height,'auto');

// Rotate graph 90 degrees and set margin

// Nice shadow

// Setup title
$graph->title->Set('Horizontal bar graph ex 1');

// Setup X-axis

// Some extra margin looks nicer

// Label align for X-axis

// Add some grace to y-axis so the bars doesn't go
// all the way to the end of the plot area

// We don't want to display Y-axis

// Now create a bar pot
$bplot = new BarPlot($datay);

//  ALSO tried:
//  $bplot->SetColor(array("red","green","blue","gray"));

//You can change the width of the bars if you like

// We want to display the value of each bar at the top
$bplot->value->SetFormat('%.1f mkr');

// Add the bar to the graph

// .. and stroke the graph
How to&Answers:

I had the same problem, the solution I found is to define the formatting after the BarPlot class initialization

$bplot = new BarPlot($datay);
$bplot->value->SetFormat('%.1f mkr');

Solution found here


I have the same problem, and this is my solution: just add “ClearTheme()”,like this:

     $graph = new Graph(310,200); 


This is due to the Add() using the Theme default settings. There are two solutions

  1. Add plot before applying settings
  2. Remove Theme $graph->graph_theme = null; after $graph->SetScale() or adjust the theme

Source: /docs/chunkhtml/ch29.html – Part VII. Theme Class

I recently dug into this as my legend was also not displaying correctly JpGraph: How to control x/y offset, margins and color in v3.5.0b1 when using AccBarPlot?