|
Date Time in x axis |
Posted by Manaswini Sugatoor on Feb-27-2012 19:29 |
|
Hi,
I am using the perl module of chart director.
I am trying to plot a bubble chart , with RF channels on y axis range (0-10), date/ time on x-axis and number of station on z axis.
So far I was successful in using chartTime2 and produce a nice xy xhart as required. But once I give the data (x,y,z) co ordinates data , the x axis labels all become one point and even data is not represented properly .
Please have a look at the attached chart and the code.
All date time attributes are in charttime2 format.
Please let me know , where it is actually going wrong.
The code snippet is as follows :
from master script :
my $bubble_chart = make_bubble_chart_encoded_cd(\\@data_x_interval_24,\\@data_x_interval_5,\\@data_y_channel_24,\\@data_y_channel_5,\\@data_z_stations__5,\\@x_lables,\\@y_labels,$x_title,$y_title,$title,$classA,$classB);
values are as follows:
data_x_interval_24 63465937200,63465922800,63465919200,63465930000,63465933600,63465926400
data_x_interval_5 63465937200,63465922800,63465919200,63465930000,63465933600,63465926400
data_y_channel_24 5,5,5,5,5,5
data_y_channel_5 5,5,5,5,5,5
data_z_stations_24 6,29,17,27,34,29
data_z_stations_5 2,12,3,11,12,8
x_lables 63465851248,63465854848,63465858448,63465862048,63465865648,63465869248,63465872848,63465876448,63465880048,63465883648,63465887248,63465890848,63465894448,63465898048,63465901648,63465905248,63465908848,63465912448,63465916048,63465919648,63465923248,63465926848,63465930448,63465934048,63465937648,63465941248
y_labels 0,1,2,3,4,5,6,7,8,9,10
-------------------------------------------------------------
sub make_bubble_chart_encoded_cd()
{
my ($dX0,$dX1,$dY0,$dY1,$dZ0,$dZ1,$x_lables,$y_labels,$x_title,$y_title,$title,$classA,$classB)=@_;
my $dataX0 = [@$dX0];
my $dataY0 = [@$dY0];
my $dataZ0 = [@$dZ0];
my $dataX1 = [@$dX1];
my $dataY1 = [@$dY1];
my $dataZ1 = [@$dZ1];
# Create a XYChart object of size 540 x 480 pixels
my $c = new XYChart(1100, 580);
# Set the plotarea at (70, 65) and of size 900 x 350 pixels. Turn on both horizontal
# and vertical grid lines with light grey color (0xc0c0c0)
$c->setPlotArea(70, 65, 1000, 300, -1, -1, $perlchartdir::Transparent, 0xc0c0c0, -1);
# Add a legend box at (70, 30) (top of the chart) with horizontal layout. Use 12 pts
# Times Bold Italic font. Set the background and border color to Transparent.
$c->addLegend(70, 30, 0,'', 8)->setBackground($perlchartdir::Transparent
);
# Add a title to the chart using 18 pts Times Bold Itatic font.
$c->addTitle($title,'', 10);
# Add titles to the axes using 12 pts Arial Bold Italic font
$c->yAxis()->setTitle($y_title,'', 8);
$c->xAxis()->setTitle($x_title,'', 8);
# Set the axes line width to 3 pixels
$c->xAxis()->setWidth(3);
$c->yAxis()->setWidth(3);
$c->xAxis()->setLabels2($x_lables)->setFontAngle(60);
$c->yAxis()->setLabels2($y_labels);
# Add (dataX0, dataY0) as a scatter layer with red (ff3333) glass spheres, where the
# sphere size is modulated by dataZ0. This creates a bubble effect.
$c->addScatterLayer($dataX0,$dataY0, $classA,
$perlchartdir::GlassSphere2Shape, 15, 0xff3333)->setSymbolScale($dataZ0);
# Add (dataX1, dataY1) as a scatter layer with blue (0000ff) glass spheres, where the
# sphere size is modulated by dataZ1. This creates a bubble effect.
$c->addScatterLayer($dataX1, $dataY1,$classB,
$perlchartdir::GlassSphere2Shape, 15, 0x0000ff)->setSymbolScale($dataZ1);
# Output the chart
my $tmp_file = "/tmp/bubble.png";
$c->makeChart($tmp_file);
my $buf;
my $encoded = "";
open(FILE,$tmp_file) or die "$!";
binmode FILE;
while (read(FILE, $buf, 60*57))
{
$encoded .= encode_base64($buf);
}
chomp($encoded);
my $outputsvg = '<object data="data:image/png;base64,'.$encoded.'" width="1100" height="580"></object>';
my $OUTHTML = $outputsvg;
`rm -rf $tmp_file`;
return $OUTHTML;
Timely response will be great help!
Regards,
Manaswini.
|
Re: Date Time in x axis |
Posted by Peter Kwan on Feb-28-2012 05:45 |
|
Hi Manaswini,
The cause of the problem is due to using Axis.setLabels2. Instead of using:
$c->xAxis()->setLabels2($x_lables)->setFontAngle(60);
$c->yAxis()->setLabels2($y_labels);
You may change them to:
$c->xAxis()->setDateScale(63465851248, 63465941248, 3600); #the 3600 is optional
$c->xAxis()->setLabelStyle("arial.ttf", 8, 0x000000, 60);
$c->yAxis()->setLinearScale(0, 10, 1);
In ChartDirector, "labels" (no matter set using setLabels or setLabels2) are treated to be "names", and their contents do not represent their x-coordinates. Instead, ChartDirector will always assume their x-coordinates to be 0, 1, 2, 3, .... irrespective of what are actually in the labels.
With setLabels2, ChartDirector knows that the labels are numbers or date/time, but their data type are just used for the purpose of formatted (eg, they can be formatted using {value|mmm dd, yyyy}). Their x-coordinates are still 0, 1, 2, 3, ....
So when addScatterLayer is used with x-coordinates of 63465937200, the labels are pushed to the left side (which has coordinates 0, 1, 2, ), and the bubbles are pushed to the right side (which has coordinates 63465937200, ...).
To solve the problem, instead of using Axis.setLabels2, the Axis.setDateScale or Axis.setLinearScale should be used. In fact, there is no need to provide every label on the axis. You just need to provide the axis end points and the tick increment, and ChartDirector can come up with the labels. I think in your case, it is even better if you omit the tick increment for the x-axis, as ChartDirector auto-scaling can come up with a reasonable increment. You can use Axis.setTickDensity to fine tune how "dense" you would like the ticks to be.
If you prefer to specify every label explicity, you may use setDateScale2 and setLinearScale2 instead.
Hope this can help.
Regards
Peter Kwan |
Re: Date Time in x axis |
Posted by Manaswini Sugatoor on Feb-28-2012 14:21 |
|
Thanks a ton!
It worked |
|