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

Message ListMessage List     Post MessagePost Message

  bar chart scale
Posted by Leo on Jul-09-2013 11:36
Attachments:
Hello Peter,

I have a simple bar chart, gas price per gallon in the US, but eyeballing the bars do not
seem proportional.

Especially the last 2 bars, give people the illusion that the prices dropped 80% from peak.

Any recommendations?

perl code:
~~~
  1 #!/usr/bin/perl
  2 use perlchartdir;
  3 my $data =[3.603,3.587,3.602,3.665,3.729,3.704,3.705,3.715,3.689,3.645,3.567,3.563];
  4 my $c = new XYChart(290, 250);
  5 $linecolor = 0x0040FF;
  6 $c->getLegend()->setPos(-9999, -9999);
  7 $c->addLegend(-1, -1, 0, "arialbd.ttf", 20)->setBackground($perlchartdir::Transparent);
  8 $c->setPlotArea(-1, 20, 280, 230, 0xffffff, 0xffffff, 0xffffff, 0xffffff);
  9 $lastXCoor = scalar(@$data) - 1;
10 $layer=$c->addBarLayer($data, $linecolor);
11 $layer->setLineWidth(15);
12 $c->addTitle("1_TITLE", "bold", 25, 0x0B173B, 0xeeeeee);
13 $c->xAxis->setColors(0xFF0000,0xFF0000,0xFF0000,0xFF0000);
14 $c->yAxis->setColors(0xFF0000,0xFF0000,0xFF0000,0xFF0000);
15 $c->makeChart("gas.png");
peter03.jpg

  Re: bar chart scale
Posted by Peter Kwan on Jul-10-2013 00:17
Hi Leo,

The in your case is because the y-axis does not start from 0. If your code does not hide the y-axis, you should see that the chart is accurate.

For some application, it is more desirable for the y-axis to always start from 0, while for other applications, it may not be desirable to force the y-axis to start from 0. As ChartDirector does not know which type of y-axis should be used, so it just guesses using the following rule - if the data variation is small compared to the data value, it will not force the y-axis to start from 0, otherwise it will force the y-axis to start from 0.

For your case, the data variation (the max value minus the min value) is 0.126. The max value itself is 3.729. So the data variation is only 3.3% of the max data value, which is too small (the default in ChartDirector is 20%, configurable using Axis.setAutoScale). In this case, ChartDirector will choose not to start the y-axis from 0. If the y-axis is started from 0, all the bars will all be of the almost same length (there are only 3.3% variation), and the user may hardly see the difference among the bars. On the other hand, forcing the y-axis from 0 will accurately reflect that the values are in fact almost the same. If you prefer to start the y-axis from 0, you can use Axis.setAutoScale to set the "zero affinity" parameter to 0.

$c->yAxis()->setAutoScale(0.1, 0.1, 0);

Hope this can help.

Regards
Peter Kwan

  Re: bar chart scale
Posted by Leo on Jul-10-2013 02:13
Attachments:
So if starts from 0, differences barely noticeable.
If not starts from 0, differences become misleading.

Now, I added a data point at the beginning the array, the number 3.
Result is excellent.
But the question now becomes: how do I make the first bar invisible??

Thanks.

perl code:
~~~
1 #!/usr/bin/perl
  2 use perlchartdir;
  3 my $data =
[3,3.603,3.587,3.602,3.665,3.729,3.704,3.705,3.715,3.689,3.645,3.567,3.563];
  4 my $c = new XYChart(290, 250);
  5 $linecolor = 0x0040FF;
  6 $c->getLegend()->setPos(-9999, -9999);
  7 $c->addLegend(-1, -1, 0, "arialbd.ttf", 20)-
>setBackground($perlchartdir::Transparent);
  8 $c->setPlotArea(-1, 20, 280, 230, 0xffffff, 0xffffff, 0xffffff, 0xffffff);
  9 $lastXCoor = scalar(@$data) - 1;
10 $layer=$c->addBarLayer($data, $linecolor);
11 $layer->setLineWidth(15);
12 $c->addTitle("1_TITLE", "bold", 25, 0x0B173B, 0xeeeeee);
13 $c->xAxis->setColors(0xFF0000,0xFF0000,0xFF0000,0xFF0000);
14
15 #$c->yAxis()->setAutoScale(0.1, 0.1, 0.5);
16
17 $c->yAxis->setColors(0xFF0000,0xFF0000,0xFF0000,0xFF0000);
18 $c->makeChart("gas.png");
peter04.jpg

  Re: bar chart scale
Posted by Peter Kwan on Jul-10-2013 02:59
Hi Leo,

Instead of adding the number 3 to your data, please add another transparent layer using the value 3. For example:

my $dummyData = [3];
$c->addLineLayer($dummyData, $perlchartdir::Transparent);

Hope this can help.

Regards
Peter Kwan

  Re: bar chart scale
Posted by Leo on Jul-10-2013 04:18
Thanks, Peter.

Second thought, I think I am going to do it the formal way, this should take care of all
situations.

$c->yAxis()->setLogScale($low,$high);


$low and $high are calculated:
$low being the integer part of the lowest value. ( in my case, 3)
$high being the integer part of the highest value, then +1. (in my case, 4)

Do you see any holes in this??

  Re: bar chart scale
Posted by Peter Kwan on Jul-10-2013 05:31
Hi Leo,

Note that log scale must be positive. Zero and negative values are not allowed. Even if all your data are really positive (they cannot be zero), your $low can be set to 0 if you just take the "integer part of the lowest value".

If you do not show the y-axis labels, and you do not enforce the lower limit to be 0, then the height of the bars are just showing some "impression" to the user, and their values and accuracy are not important. (Their heights are not proportional to their values, and it is possible for completely different data values to look the same.) In this case, may be you can just set the "bottomExtension" parameter of setAutoScale to a larger value, like:

$c->yAxis()->setAutoScale(0.1, 0.5, 0);

Hope this can help.

Regards
Peter Kwan