Y-Axis rescaling on line charts [message #503948] |
Wed, 16 December 2009 15:24 |
Dariusz Filipski Messages: 10 Registered: July 2009 |
Junior Member |
|
|
Hi,
I need to add a dynamic marker to a simple line chart. I pass the value with persistent variable and the marker is nicely drawn. However, if the value is bigger than maximum value in data series or smaller than minimum one, marker line is drawn in wrong place comparing to the y-axis. I tried to get current min and max values of the axis and update them if necessary, but for some reason current values I get from Scale.getMax() and Scale.getMin() are always null. I know I can go through all data and count those min and max values manually, but it seems an overhead and dirty solution. What I'm doing wrong in retrieving actual values for y-axis?
Here's my script:
function beforeGeneration( chart, icsc )
{
importPackage(Packages.org.eclipse.birt.chart.model.impl);
importPackage(Packages.org.eclipse.birt.chart.model.type.imp l);
importPackage(Packages.org.eclipse.birt.chart.model.data.imp l);
importPackage(Packages.org.eclipse.birt.chart.model.attribut e);
importPackage(Packages.org.eclipse.birt.chart.model.attribut e.impl);
importPackage(Packages.org.eclipse.birt.chart.model.componen t.impl);
var target_val = icsc.getExternalContext().getScriptable().getPersistentGloba lVariable( "target");
if (target_val != null && target_val != 0)
{
var chart = icsc.getChartInstance();
var yAxis = chart.getAxes().get(0).getAssociatedAxes().get(0);
var yScale = yAxis.getScale();
//Draw the marker
target_ml = MarkerLineImpl.create(yAxis, NumberDataElementImpl.create(target_val));
target_ml.getLabel().getCaption().setValue("Target: " + target_val + "max: " + yScale.getMax() + "min: " + yScale.getMin() );
target_ml.getLineAttributes().getColor().set(255,0,0);
//Rescale Y-axis if needed
if (target_val > yScale.getMax())
yScale.setMax(NumberDataElementImpl.create(target_val));
if (target_val < yScale.getMin())
yScale.setMin(NumberDataElementImpl.create(target_val));
yAxis.setScale(yScale);
}
}
P.S. I tried to attach a sample report which shows strange behaviour especially if Scale.setMin() was used but somehow I failed so I put it at http://www.mediafire.com/?gomyggyxdzn
[Updated on: Wed, 16 December 2009 18:37] Report message to a moderator
|
|
|
Re: Y-Axis rescaling on line charts [message #504014 is a reply to message #503948] |
Wed, 16 December 2009 15:10 |
|
The problem is that in the beforeGeneration the min and max have not yet
been calculated. Can you try using an afterDataSetFilled Script like:
function afterDataSetFilled(series, dataSet, icsc)
{
importPackage( Packages.java.io );
importPackage( Packages.org.eclipse.birt.chart.model.type.impl );
importPackage( Packages.org.eclipse.birt.chart.util);
ps = PluginSettings.instance( );
if( series.getClass() == LineSeriesImpl ){
series.getLineAttributes().setThickness(5);
dsp = ps.getDataSetProcessor( series.getClass() );
//dataset max
var mmax = dsp.getMaximum( dataSet );
}
if( series.getClass() == BarSeriesImpl ){
}
}
Jason
Dariusz Filipski wrote:
> Hi,
>
> I need to add a dynamic marker to a simple line chart. I pass the value
> with persistent variable and the marker is nicely drawn. However, if the
> value is bigger than maximum value in data series or smaller than
> minimum one, marker line is drawn in wrong place comparing to the
> y-axis. I tried to get current min and max values of the axis and update
> them if necessary, but for some reason current values I get from
> Scale.getMax() and Scale.getMin() are always null. I know I can go
> through all data and count those min and max values manually, but it
> seems an overhead and dirty solution. What I'm doing wrong in retrieving
> actual values for y-axis?
>
> Here's my script:
>
>
>
> function beforeGeneration( chart, icsc )
> {
> importPackage(Packages.org.eclipse.birt.chart.model.impl);
> importPackage(Packages.org.eclipse.birt.chart.model.type.imp l);
> importPackage(Packages.org.eclipse.birt.chart.model.data.imp l);
> importPackage(Packages.org.eclipse.birt.chart.model.attribut e);
> importPackage(Packages.org.eclipse.birt.chart.model.attribut e.impl);
> importPackage(Packages.org.eclipse.birt.chart.model.componen t.impl);
>
> var target_val =
> icsc.getExternalContext().getScriptable().getPersistentGloba lVariable(
> "target");
>
> if (target_val != null && target_val != 0)
> {
> var chart = icsc.getChartInstance();
> var yAxis = chart.getAxes().get(0).getAssociatedAxes().get(0);
> var yScale = yAxis.getScale();
>
> //Draw the marker
> target_ml = MarkerLineImpl.create(yAxis,
> NumberDataElementImpl.create(target_val));
> target_ml.getLabel().getCaption().setValue("Target: " +
> target_val + "max: " + yScale.getMax() + "min: " + yScale.getMin() );
> target_ml.getLineAttributes().getColor().set(255,0,0);
>
> //Rescale Y-axis if needed
> if (target_val > yScale.getMax())
> yScale.setMax(NumberDataElementImpl.create(target_val));
>
> if (target_val < yScale.getMin())
> yScale.setMin(NumberDataElementImpl.create(target_val));
>
> yAxis.setScale(yScale);
> }
> }
>
>
|
|
|
Re: Y-Axis rescaling on line charts [message #504093 is a reply to message #504014] |
Thu, 17 December 2009 08:54 |
Dariusz Filipski Messages: 10 Registered: July 2009 |
Junior Member |
|
|
Jason Weathersby wrote on Wed, 16 December 2009 17:10 | The problem is that in the beforeGeneration the min and max have not yet
been calculated. Can you try using an afterDataSetFilled Script
|
Heh, I was suspecting that. Thanks, it works now.
However, I have additional problem now, as in my target report I have the same chart generated multiple times for different data in a table based on other data source (query->subquery binding) and first instance of this chart is scaled properly, next ones then inherit scales from previous ones (e.g. if first scales only max and next only min, the next one ends up with scaled both min and max). I can, of course, scale both values every time, but that means reimplementing the same scaling algorithm as the one inbuilt in Scale class.
So, when actually min and max values are calculated and when I can safely retrieve them with Scale.getMax() and Scale.getMin()?
|
|
|
Re: Y-Axis rescaling on line charts [message #504254 is a reply to message #504093] |
Thu, 17 December 2009 16:50 |
|
You could because you are using a plot with axes use the
afterComputation script like:
function afterComputations( chart, plotComputation )
{
var max =
plotComputation.getAxes().getPrimaryOrthogonal().getScale(). getMaximum();
}
Jason
Dariusz Filipski wrote:
> Jason Weathersby wrote on Wed, 16 December 2009 17:10
>> The problem is that in the beforeGeneration the min and max have not
>> yet been calculated. Can you try using an afterDataSetFilled Script
>
>
> Heh, I was suspecting that. Thanks, it works now.
> However, I have additional problem now, as in my target report I have
> the same chart generated multiple times for different data in a table
> based on other data source (query->subquery binding) and first instance
> of this chart is scaled properly, next ones then inherit scales from
> previous ones (e.g. if first scales only max and next only min, the next
> one ends up with scaled both min and max). I can, of course, scale both
> values every time, but that means reimplementing the same scaling
> algorithm as the one inbuilt in Scale class. So, when actually min and
> max values are calculated and when I can safely retrieve them with
> Scale.getMax() and Scale.getMin()?
>
|
|
|
|
Re: Y-Axis rescaling on line charts [message #504623 is a reply to message #504429] |
Mon, 21 December 2009 15:16 |
|
afterComputations was reinstated in the 2.5 branch and you can change
the markers anytime beforeDrawMarkerLine.
I have not tried auto scale class but this may work.
Jason
Dariusz Filipski wrote:
> Jason Weathersby wrote on Thu, 17 December 2009 18:50
>> You could because you are using a plot with axes use the
>> afterComputation script like:
>>
>> function afterComputations( chart, plotComputation )
>> {
>> var max =
>> plotComputation.getAxes().getPrimaryOrthogonal().getScale().
>> getMaximum();
>> }
>
>
> Isn't aftercomputations() deprecated? And isn't it too late to draw
> markers there? If I try I get "Cannot render text with an undefined
> color" errors.
>
> I've got another idea - any manual setting won't give me such a nice
> scaling as BIRT automated one (unless I implement the same algorithm :)
> ). So is it possible to use inbuilt auto scaling to compute new scale
> for me? E.g. to provide min and max values to AutoScale object, then ask
> it to compute new min, max and step for final scale?
|
|
|
Powered by
FUDForum. Page generated in 0.02043 seconds