ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Changing bar colors depending on value
Posted by sam on Jan-20-2009 02:13
ive created a bar graph with two datasets, and an array for each set that has the rgb colors for chartdirector to fill. the arrays contaning the colors are filled, but they are not painted with the colors inside the arrays.
i have the following code in php
#add multicolor bar chart layer
$layer=$c->addbarlayer2(side,2);
$layer->addDataset($data0,$daycolor,"Rate1");
$layer->addDataset($data1,$monthcolor,"Rate2");

both sets are charted, but with darkgrey color (0x00000) as if the arrays where empty, when they are not. verified by doind a print_r($daycolor) and it shows rgbs corresponding to the colors i want

any help ?

  Re: Changing bar colors depending on value
Posted by Peter Kwan on Jan-20-2009 02:46
Hi sam,

Note that in ChartDirector, a color is a 32-bit integer. It is like 0x123456.

Note that "0x123456" (with quotes) is not a color, as it is a not a number according to PHP syntax standard. (It is a text string.) PHP will treat "0x123456" the same as 0 in numeric context, which is exactly equivalent to 0x000000 according to PHP syntax, so it is the black color.

If you print_r($daycolor) and it shows "0x123456", then it is certainly not a number, but a text string. In PHP, all numbers (no matter it is hex in the source code or not) will print out as decimal numbers. (Try print_r(0x123456); to see the output for printing numbers.)

If the above in fact is the cause of the problem, please ensure you use 32-bit integers as colors, not text strings.

Also, please note that $daycolor and $monthcolor should be scalar values. They should not be themselves arrays. Only addBarLayer3 accept an array of colors (that is, an array of 32-bit integers) as input. If you need to create a bar chart with every bar having a different color, please use addBarLayer3.

If the above still cannot solve the problem, would you mind to inform me what is the output of print_r($daycolor), and also please kindly attach a sample chart so I may see the exact color of your bars.

Regards
Peter Kwan

  Re: Changing bar colors depending on value
Posted by sam on Jan-20-2009 03:18
dear peter:

thanx for the fast reply.

i changed the contents of the arrays, insted of using " i used ' and the result whas the color name in 32 bit e.g. array[0]=>0x00bb00 etc. if i print_r(0x123456) it gives me 1193046, a decimal number a i guess.

anyways placing:
$layer=$c-> addbarlayer3(side,2);
$layer->adddataset($data0,$daycolor,"day");
$layer->adddataset($data1,$monthcolor, "monthly");

gives me a barchart with stack bars instead of 2 different sets, like addbarlayer2 does. the color problem persists, as all bargraphs have a dark grey color.

any sugestions?

i have 2 data sets, each set such be graphed separetely and with color criteria inside daycolor and monthcolor, (arrays).

thanx

sam

  Re: Changing bar colors depending on value
Posted by Peter Kwan on Jan-20-2009 04:43
Hi sam,

I mean only the API addBarLayer3 accepts an array of colors as the argument. It is for creating a bar chart in which every bar has a different color. The addDataSet API does not accept an array of colors. (You may look up addBarLayer3 and addDataSet in the ChartDirector documentation index for details on how to use these API.)

In your case, I assume you want to categorize you data. That is, for certain category of data (eg. data less than certain values), you want to use one color, and for another category, you want to use another color.

For this type of charts, in ChartDirector, the data structure should be to use a different data series for different category of data. In the simplest example, suppose $data0 is either red or green. Instead of creating a color array, you may use:

#$data0 should really be two categories of bars
$redBars = array_pad(array(), count($data0), NoValue);
$greenBars = array_pad(array(), count($data0), NoValue);

for ($i = 0; $i < count($data0); ++i) {
    if ($data0[$i] > 50)
         $redBars[$i] = $data0[$i];
    else
         $greenBars[$i] = $data0[$i];
}

If your data comes from a database, you may create the redBars and greenBars data series directly when you read the data from your query result.

You may use the same method to categorize $data1.

Then you may design a way to graphically visualize the data. If you want to use a bar chart with Side layout, you may use something like:

$layer = $c->addBarLayer2(Stack);
//bars on left
$layer->addDataSet($redBars, 0xff0000);
$layer->addDataSet($greenBars, 0x00ff00);

//bars on right
$layer->addDataSet($yellowBars, 0xffff00);
$layer->addDataSet($blueBars, 0x0000ff);

Hope this can help.

Regards
Peter Kwan

  Re: Changing bar colors depending on value
Posted by sam on Jan-20-2009 05:37
Attachments:
dear peter:
here is what im trying to do. i have 2 sets of data graphed in horizontal bars. if the value of the bar is between 0-2 it should be green, 2-6 yellow and greater than 6 should be red.
ive managed to graph both sets, but with the following code:

$layer =$c->addBarLayer2(Side,2)
$layer->addDataSet($data0,0xff8080);
$layer->addDataSet($data1,0xff8080);

like you said before, this option does not accept an array for color definition, so this piece of code has to be changed but i dont know how

the graphs show just like i want but the colors dont match the daycolor or month color array.
how can we make this happen?

sam

ps attached referenced excel image for illustration purposes
sets.JPG

  Re: Changing bar colors depending on value
Posted by Peter Kwan on Jan-20-2009 19:51
Hi sam,

If your data are come a database, you can split the each data set into three data series for red, yellow and green bars using an SQL statement.

If not (or you do not bother to re-write the SQL), you may try the following code.

function colorBars($layer, $data)
{
    $redBars = array_pad(array(), count($data), NoValue);
    $yellowBars = array_pad(array(), count($data), NoValue);
    $greenBars = array_pad(array(), count($data), NoValue);

    for ($i = 0; $i < count($data); ++$i) {
        if ($data[$i] > 6)
            $redBars[$i] = $data[$i];
        else if ($data[$i] <= 2)
            $greenBars[$i] = $data[$i];
        else
            $yellowBars[$i] = $data[$i];
    }

    $layer->addDataGroup();
    $layer->addDataSet($redBars, 0xff0000);
    $layer->addDataSet($yellowBars, 0xffff00);
    $layer->addDataSet($greenBars, 0x00ff00);
}

$layer = $c->addBarLayer2(Stack);
colorBars($layer, $data0);
colorBars($layer, $data1);

Hope this can help.

Regards
Peter Kwan