|
Customized colors for bar charts |
Posted by Jorge Eduardo Ardila on Sep-16-2014 06:03 |
|
Hi Peter,
I have some bar charts on PHP, running on linux. I want to customize each bar color,
based on the brand that it represents.
To do so, I query the brand and also the 0xRRGGBB color, in order to force the chart to
use tha specific color instead of an array of colors or aleatory colors.
Here is the code sent to the chart object:
<img class='center' src='chr_dec.php?
data=2,9,4&labels=CX6,CX3,Fortuner&title=ACELERACION&bc=0x0066CC,0x0066CC,0x
CC3300' >
Here I sent data, labels, chart title and bc which stands for brand color.
In the chart, I manage the strings this way:
$data= explode(",", $_GET["data"]);
$labels = explode(",", $_GET["labels"]);
$title = $_GET["title"];
$bc = explode(",", $_GET["bc"]); #brand color
No problem generating charts, labels, data and title work fine.
The problem is with the color, which i'm doing this way:
$layer = $c->addBarLayer($data, $bc);
As you may know, no result at all, instead I get this:
So how should I enforce especific color to each bar?
I have done with addBarLayer3, using an array of color, so I can force the order in
which colors are assigned. However, this time, I need to assign a specific color, which
comes from the databse to each bar.
Thanks in advance, Jorge
|
Re: Customized colors for bar charts |
Posted by Jorge Eduardo Ardila on Sep-16-2014 06:44 |
|
HI Peter, I indeed found something interesting in the forum. A loop like this:
sent to the browser:
<img class='center' src='chr_dec.php?
data=2,9,4&labels=CX6,CX3,Fortuner&title=ACELERACION&bc=0x0066CC,0x0066CC,0xC
C3300' >
$bc = explode(",", $_GET["bc"]); #brand color
$layer = $c->addBarLayer3($data);
for ($i = 0; $i < count($data); ++$i) {
$dataSet = $layer->getDataSet($i);
$dataSet->setDataColor($bc[$i]);
However, still I get black colums, not the color I need and also all bars colored the same.
Please keep in mind that the number of columns changes due to user selection of
attributes to visualize.
Thanks, Jorge |
Re: Customized colors for bar charts |
Posted by Peter Kwan on Sep-17-2014 00:15 |
|
Hi Jorge,
In ChartDirector, a color is represented as an integer, such as 0x0066CC in PHP syntax,
but not a text string "0x0066CC". For example:
$color = 0x0066CC;
#below is not a color
$not_color = "0x0066CC";
All query variables are text strings, not numbers. If you use the text string "0x0066CC" as
a number, the PHP interpreter treat the number as 0, so what ChartDirector receive is 0
(the black color).
For your case, you may use the following method:
Leave out the "0x" in the color. The color is 0066CC, and "0x" is just the syntax
requirement of PHP. The query parameter are data, not PHP code, so it does not need
the "0x". Just specify the color as text:
<img class='center' src='chr_dec.php?
data=2,9,4&labels=CX6,CX3,Fortuner&title=ACELERACION&bc=0066CC,0066CC,CC3300' >
Then please ask PHP to parse the text string as a hex number, like:
$bc = explode(",", $_GET["bc"]); #brand color
for ($i = 0; $i < count($bc); ++$i)
$bc[$i] = hexdec($bc[$i]);
Hope this can help.
Regards
Peter Kwan |
Re: Customized colors for bar charts |
Posted by Jorge Eduardo Ardila on Sep-17-2014 02:06 |
|
Yes indeed Peter it worked as expected, thanks again for such a great support.
Have a good day, week, month, year, life.
Jorge Eduardo. |
|