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

Message ListMessage List     Post MessagePost Message

  Hierarchical labels for xAxis
Posted by milo on Jun-01-2009 16:03
Hello,

How to add hierarchical labels (or in other word few rows of labels) to a xAxis? I want to achieve sth. like this:

10 |
    |
    |               X
    |               X           X
0  |     X         X           X
------------------------------
    |  Prod1 | Prod2 | Prod3     |
    |  ProdCat1       | ProdCat2 |
    |      ProductLine1              |

This type of chart comes from pivot tables. I'm using Perl version of ChartDirector.

  Re: Hierarchical labels for xAxis
Posted by Peter Kwan on Jun-02-2009 01:19
Hi Milo,

You may use custom labels (added with BaseChart.addText) for the second and third rows of labels. There is an example in the following link. May be you can use it as a reference.

http://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1193841656#N1193859158

Hope this can help.

Regards
Peter Kwan

  Re: Hierarchical labels for xAxis
Posted by milo on Jun-02-2009 14:33
This solves just "label part", but how about ticks? I mean third level of ticks (and 4-th, 5-th etc.). In the attached example "-" sign before base label is used to achieve minor and major ticks, but, when you have more than 2 levels of labels this doesn't work (or I don't know how to make it work).

  Re: Hierarchical labels for xAxis
Posted by Peter Kwan on Jun-03-2009 02:48
Attachments:
Hi milo,

You may use custom lines (BaseChart.addLine) as the ticks.

I have modified the code so that it can now use arbitrary number of x-axis labels, in which the labels can be unevenly spaced.

Hope this can help.

Regards
Peter Kwan
multixlayers.png
multixlayers.pl
#!/usr/bin/perl

# Include current script directory in the module path (needed on Microsoft IIS).
# This allows this script to work by copying ChartDirector to the same directory
# as the script (as an alternative to installation in Perl module directory)
use File::Basename;
use lib dirname($0) =~ /(.*)/;

use perlchartdir;

# The data for the bar chart
my $data0 = [100, 125, 156, 147, 87, 124, 178, 109, 140, 106, 192, 122];
my $data1 = [122, 156, 179, 211, 198, 177, 160, 220, 190, 188, 220, 270];
my $data2 = [167, 190, 213, 267, 250, 320, 212, 199, 245, 267, 240, 310];
my $labels = ["TJ", "AMK", "HZ", "TJ", "AMK", "HZ", "TJ", "AMK", "HZ", "TJ", "AMK", "HZ"];

# Create a XYChart object of size 540 x 375 pixels
my $c = new XYChart(540, 400);

# Add a title to the chart using 18 pts Times Bold Italic font
$c->addTitle("Average Weekly Network Load", "timesbi.ttf", 18);

# Set the plotarea at (50, 55) and of 440 x 280 pixels in size. Use a vertical
# gradient color from light blue (f9f9ff) to blue (6666ff) as background. Set border
# and grid lines to white (ffffff).
$c->setPlotArea(50, 55, 440, 280, $c->linearGradientColor(0, 55, 0, 335, 0xf9f9ff,
    0x6666ff), -1, 0xffffff, 0xffffff);

# Add a legend box at (50, 28) using horizontal layout. Use 10pts Arial Bold as font,
# with transparent background.
$c->addLegend(50, 28, 0, "arialbd.ttf", 10)->setBackground($perlchartdir::Transparent
    );

# Set the x axis labels
$c->xAxis()->setLabels($labels);

# Draw the ticks between label positions (instead of at label positions)
$c->xAxis()->setTickOffset(0.5);
#$c->xAxis()->setTickLength(50, 20);

# Set axis label style to 8pts Arial Bold
$c->xAxis()->setLabelStyle("arialbd.ttf", 8);
$c->yAxis()->setLabelStyle("arialbd.ttf", 8);

# Set axis line width to 2 pixels
$c->xAxis()->setWidth(2);
$c->yAxis()->setWidth(2);

# Add axis title
$c->yAxis()->setTitle("Throughput (MBytes Per Hour)");

# Add a multi-bar layer with 3 data sets
my $layer = $c->addBarLayer2($perlchartdir::Stack);
$layer->addDataSet($data0, 0xff0000, "Server #1");
$layer->addDataSet($data1, 0x00ff00, "Server #2");
$layer->addDataSet($data2, 0xff8800, "Server #3");

# Set bar border to transparent. Use glass lighting effect with light direction from
# left.
$layer->setBorderColor($perlchartdir::Transparent, perlchartdir::glassEffect(
    $perlchartdir::NormalGlare, $perlchartdir::Left));

$c->layout();

my $labels2 = ["Jun", "Jun", "Jun", "Jun", "Jul", "Jul", "Aug", "Aug", "Aug", "Aug", "Aug", "Sep"];
my $labels3 = ["XXX", "XXX", "XXX", "XXX", "XXX", "XXX", "QQQ", "QQQ", "QQQ", "QQQ", "QQQ", "QQQ"];
my @labelLayers = ($labels2, $labels3);

for (my $ii = 0; $ii <= $#labelLayers; ++$ii) {
	$labels = $labelLayers[$ii];

	my $lastXCoor = 0;
	my $noOfLabels = scalar(@$labels);
	
	for (my $i = 0; $i < $noOfLabels; ++$i) {
		if (($i == $noOfLabels - 1) || ($$labels[$i] ne $$labels[$i + 1])) {
			$c->addText($c->getXCoor(($lastXCoor + $i) / 2.0), 350 + (($ii + 0.5) * 25), $$labels[$i], "arialbi.ttf", 10, 0x000000, $perlchartdir::Center);
			$lastXCoor = $i + 1;
			if ($i != $noOfLabels - 1) {
				$c->addLine($c->getXCoor($lastXCoor - 0.5), 335, $c->getXCoor($lastXCoor - 0.5), 350 + ($ii * 25) + 25, 0x000000);
			}
		}
	}
}

$c->addLine(50, 335, 50, 350 + ($#labelLayers * 25) + 25, 0x000000);
$c->addLine(50 + 440, 335, 50 + 440, 350 + ($#labelLayers * 25) + 25, 0x000000);


# output the chart
binmode(STDOUT);
print "Content-type: image/png\\n\\n";
print $c->makeChart2($perlchartdir::PNG);


  Re: Hierarchical labels for xAxis
Posted by milo on Jun-03-2009 14:33
That's what I need! Thank you very much.

Regards