|
issue with bar layer |
Posted by devael on Jul-07-2008 11:04 |
|
Hi,
I tried to replace a SVG chart (see attached fig_1) with ChartDirs.
I set the xAxis to linear scale (it was value range but not date time value)
and I have successfully added two vertical lines using 'addMark'. (see attached fig_2)
The problem occurred when I added bar layer which caused the xAxis to shrink (see attached fig_3)
following was the code I used.
Can you please help me to spot what mistakes I did here... or perhaps this is not the correct way?
thanks,
devael
bar_data_Y
[1,13,44,111,104,73,49,29,22,5,3,1,2,1]
bar_data_X
[25.005,25.010,25.015,25.020,25.025,25.030,25.035,25.040,25.045,25.050,25.055,25.060,25.065,25.095]
###Create a XYChart object#################################
#note: the width and height of the graph was supplied by the CGI parameters
my $width = 500;
my $height= 220;
my $c = new XYChart($width, $height,$G_PINK,0xFF000000);
# Set the plotarea
my $top_x_pad = 50 ;
my $top_y_pad = 35 ;
my $bottom_x_pad = 30;
my $bottom_y_pad = 40;
my $chart_width = $width - $top_x_pad - $bottom_x_pad ;
my $chart_height = $height - $top_y_pad - $bottom_y_pad;
### Create Plot Area and set the border colour #########
$c->setPlotArea($top_x_pad, $top_y_pad, $chart_width, $chart_height,$G_WHITE,-1, -1, 0xc0c0c0,$perlchartdir::Transparent);
$c->yAxis()->setLabelGap(0);
$c->yAxis()->setTickLength(-1);
$c->yAxis()->setTickDensity(15);
$c->yAxis()->setLabelStyle("",7);
$c->xAxis()->setLabelStyle("",7);
$c->yAxis()->setMargin(0,2);
$c->setClipping(0);
debug_msg("\\nxAxis_min: $xAxis_min",1);
debug_msg("\\nxAxis_max: $xAxis_max",1);
### Add a bar layer #######################################
$c->xAxis()->setLinearScale($xAxis_min,$xAxis_max,0.005);
$c->xAxis()->setLabelStep(4,1);
$c->xAxis()->setLabelFormat("{value|3}");
$c->xAxis()->setLabelStyle("arial.ttf", 7);
#create green and red mark lines
my $proper_avg_weight = sprintf('%.3f',$avg_weight);
my $Avg_mark = $c->xAxis()->addMark($proper_avg_weight,$G_RED);
$Avg_mark->setDrawOnTop(0);
$Avg_mark->setLineWidth(3);
my $proper_pack_label_weight = sprintf('%.3f',$$data{'pack_label_weight'});
my $Pack_mark = $c->xAxis()->addMark($proper_pack_label_weight,$G_GREEN);
$Pack_mark->setDrawOnTop(0);
$Pack_mark->setLineWidth(3);
# Add a bar chart layer using the given data
debug_msg("\\nBarData_Y : \\n" . Dumper(\\@BarData_Y),1);
debug_msg("\\nBarData_X : \\n" . Dumper(\\@BarData_X),1);
my $bar_layer = $c->addBarLayer3(\\@BarData_Y,\\@BarColors);
$bar_layer->setXData(\\@BarData_X);
$bar_layer->setBarWidth(7);
$bar_layer->setBorderColor($perlchartdir::Transparent);
|
Re: issue with bar layer |
Posted by Peter Kwan on Jul-08-2008 01:16 |
|
Hi Devael,
If a bar layer (or any layer with a object with a finite width such as a floating box, candlestick, OHLC symbol, etc) is added to the chart, ChartDirector will automatically "indent" the axis. The effect is like adding 0.5 to either side of the axis.
The reason for the indentation is as follows. If you add bars centered at x = 0, x = 1 and x = 2, the x-axis actually has to be from x = -0.5 to 2.5. It is because half of the bar at x = 0 will be to the left of x = 0, and half of the bar at x = 2.0 will be to the right of x = 2.0.
For bar charts, the most common type of x-axis is a "label based" x-axis (set up using Axis.setLabels), which is equivalent to the x = 0, 1, 2, .... axis above, except that the labels are text strings supplied externally. So if a bar layer exists on the chart, ChartDirector will "indent" the x-axis by default.
In your case, as your axis range itself is very short, the +/- 0.5 indentation seriously affect the scale of your axis,
To disable the indentation, you may use:
$c->xAxis()->setIndent(0);
This should solve the issue you currently encountered.
Hope this can help.
Regards
Peter Kwan |
Re: issue with bar layer |
Posted by devael on Jul-08-2008 05:12 |
|
Thank you very much Peter!
It indeed works!
devael |
|