Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » BIRT » hiding labels for zero values using API
hiding labels for zero values using API [message #826044] Wed, 21 March 2012 15:31 Go to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 36
Registered: November 2011
Member
I am using java to build my entire pie chart, however, i am attempting to display labels within the chart showing the percentage of each slice, but when a slice has a value of 0 it displays the "0%" label which spills into the other slices and makes it look awful. I have found a million examples of how to do this using the script, however in every example they are passing values into the js function.. for example: "function afterDataSetFilled( dph, icsc )". The problem is that in my java class i dont instantiate these variable (ive looked all over and found that it is the DataPointHints and the IChartScriptContext, however there is literally 0 documentation anywhere showing how to use these in my situation).

So i guess my question is, how would i be able to instatiate these variables in my java class so that i can call the appropriate script function to grab my series values, or, is anyone familiar with a way that i can do this using only java.

thanks
Re: hiding labels for zero values using API [message #826191 is a reply to message #826044] Wed, 21 March 2012 19:32 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Those variables are passed automatically by the chart engine. In the
Java API you can set the scripts by using setScript eg


cwaBar
.setScript("function beforeGeneration( cm, icsc )"
+ "{importPackage(Packages.org.eclipse.birt.chart.model.attribute); "
+ "
importPackage(Packages.org.eclipse.birt.chart.model.attribute.impl); "
+ " cm.getLegend().getOutline( ).setStyle(
LineStyle.DASH_DOTTED_LITERAL );"
+ " cm.getLegend().getOutline( ).setColor(
ColorDefinitionImpl.GREEN() );"
+ " cm.getLegend().getOutline( ).setVisible( true );} ");


But if you just want to set the minimum slice look at the chart examples
view for this chart:


protected static final Chart createMinSliceChart( )
{
ChartWithoutAxes cwoaPie = ChartWithoutAxesImpl.create( );
cwoaPie.getBlock( ).setBackground( ColorDefinitionImpl.PINK( ) );

// Plot
Plot p = cwoaPie.getPlot( );
p.getClientArea( ).setBackground( ColorDefinitionImpl.PINK( ) );
p.getClientArea( ).getOutline( ).setVisible( false );
p.getOutline( ).setVisible( false );

// Legend
Legend lg = cwoaPie.getLegend( );
lg.setItemType( LegendItemType.CATEGORIES_LITERAL );
lg.getClientArea( ).getOutline( ).setVisible( true );
lg.getTitle( ).setVisible( false );

// Title
cwoaPie.getTitle( )
.getLabel( )
.getCaption( )
.setValue( "Explosion & Min Slice" ); //$NON-NLS-1$
cwoaPie.getTitle( ).getOutline( ).setVisible( false );

// Data Set
TextDataSet categoryValues = TextDataSetImpl.create( new String[]{
"New York", "Boston", "Chicago", "San Francisco", "Dallas",
"Miami"//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
//$NON-NLS-5$//$NON-NLS-6$
} );
NumberDataSet seriesOneValues = NumberDataSetImpl.create( new double[]{
24, 9, 30, 36, 8, 51
} );

// Base Series
SeriesDefinition sd = SeriesDefinitionImpl.create( );
cwoaPie.getSeriesDefinitions( ).add( sd );

Series seCategory = (Series) SeriesImpl.create( );

final Fill[] fiaBase = {
ColorDefinitionImpl.ORANGE( ),
GradientImpl.create( ColorDefinitionImpl.create( 225, 225, 255 ),
ColorDefinitionImpl.create( 255, 255, 225 ),
-35,
false ),
ColorDefinitionImpl.CREAM( ),
ColorDefinitionImpl.RED( ),
ColorDefinitionImpl.GREEN( ),
ColorDefinitionImpl.BLUE( ).brighter( ),
ColorDefinitionImpl.CYAN( ).darker( ),
};
sd.getSeriesPalette( ).getEntries( ).clear( );
for ( int i = 0; i < fiaBase.length; i++ )
{
sd.getSeriesPalette( ).getEntries( ).add( fiaBase[i] );
}

seCategory.setDataSet( categoryValues );
sd.getSeries( ).add( seCategory );

// Orthogonal Series
SeriesDefinition sdCity = SeriesDefinitionImpl.create( );
sd.getSeriesDefinitions( ).add( sdCity );

PieSeries sePie = (PieSeries) PieSeriesImpl.create( );
sePie.setDataSet( seriesOneValues );
sePie.setLabelPosition( Position.INSIDE_LITERAL );
sePie.setSeriesIdentifier( "Cities" ); //$NON-NLS-1$

// Explosion
sePie.setExplosion( 30 );
sePie.setExplosionExpression( "valueData<20 ||valueData>50"
);//$NON-NLS-1$

sdCity.getSeries( ).add( sePie );

// Min Slice
cwoaPie.setMinSlice( 10 );
cwoaPie.setMinSlicePercent( false );
cwoaPie.setMinSliceLabel( "Others" );//$NON-NLS-1$

return cwoaPie;
}


Jason

On 3/21/2012 11:31 AM, Missing name Mising name wrote:
> I am using java to build my entire pie chart, however, i am attempting
> to display labels within the chart showing the percentage of each slice,
> but when a slice has a value of 0 it displays the "0%" label which
> spills into the other slices and makes it look awful. I have found a
> million examples of how to do this using the script, however in every
> example they are passing values into the js function.. for example:
> "function afterDataSetFilled( dph, icsc )". The problem is that in my
> java class i dont instantiate these variable (ive looked all over and
> found that it is the DataPointHints and the IChartScriptContext, however
> there is literally 0 documentation anywhere showing how to use these in
> my situation).
> So i guess my question is, how would i be able to instatiate these
> variables in my java class so that i can call the appropriate script
> function to grab my series values, or, is anyone familiar with a way
> that i can do this using only java.
>
> thanks
>
Re: hiding labels for zero values using API [message #826207 is a reply to message #826191] Wed, 21 March 2012 20:08 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 36
Registered: November 2011
Member
Jason,
thanks for the help, i was overthinking my issue and complicating things way more than i needed to, thanks to you i have achieved part of my goal, however i still am having an issue.

my goal is to have labels display if their value is greater than 0, but to hide them if they are 0 or below. As of now i am using the following code and it is actually hiding all the labels, or showing all the labels, but it wont hide the zero-value labels indivudually. is this possible? here is the js i am using:

cwoaPie.setScript(
"function afterDataSetFilled( series, dataSet, icsc ){"+
"if( series.getSeriesIdentifier() == 'seriesPS' ){"+
"var list = dataSet.getValues();"+
"for(i=0;i<list.length;i++){"+
"if( list[i] == 0 ){"+
"series.getLabel().setVisible(false);"+
"}else{" +
"series.getLabel().setVisible(true);" +
"}}}}"+
Re: hiding labels for zero values using API [message #826850 is a reply to message #826207] Thu, 22 March 2012 15:24 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Why not just use a beforeDrawDataPointLabel script like:
function beforeDrawDataPointLabel( dph, label, icsc )
{
if( label.getCaption().getValue() == "0" ){
label.setVisible(false);
}
}

Jason


On 3/21/2012 4:08 PM, Missing name Mising name wrote:
> Jason,
> thanks for the help, i was overthinking my issue and complicating things
> way more than i needed to, thanks to you i have achieved part of my
> goal, however i still am having an issue.
>
> my goal is to have labels display if their value is greater than 0, but
> to hide them if they are 0 or below. As of now i am using the following
> code and it is actually hiding all the labels, or showing all the
> labels, but it wont hide the zero-value labels indivudually. is this
> possible? here is the js i am using:
>
> cwoaPie.setScript(
> "function afterDataSetFilled( series, dataSet, icsc ){"+
> "if( series.getSeriesIdentifier() == 'seriesPS' ){"+
> "var list = dataSet.getValues();"+
> "for(i=0;i<list.length;i++){"+
> "if( list[i] == 0 ){"+
> "series.getLabel().setVisible(false);"+
> "}else{" +
> "series.getLabel().setVisible(true);" +
> "}}}}"+
Re: hiding labels for zero values using API [message #826991 is a reply to message #826850] Thu, 22 March 2012 19:02 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 36
Registered: November 2011
Member
jason i had to add a '%' to that but other than that it worked perfect.

thanks
Re: hiding labels for zero values using API [message #830457 is a reply to message #826991] Tue, 27 March 2012 16:58 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 36
Registered: November 2011
Member
Jason,
I have one more issue that i am having a difficult time figuring out. Now that you clarified my issue with using the javascript events/functions I found a useful function online that displays a message in the event that all data is equal to 0. The functions works perfect, however, the title box will not resize and it only displays the first word of the message and then appends a "..." to the end. No matter what i do the title box will not enlarge enough to display all the text. Here is my js function:

cwoaPie.setScript(
"var allnull = true;"+
"function afterDataSetFilled( series, dataSet, icsc ){"+
"if( series.getSeriesIdentifier() == '' ){"+
"var list = dataSet.getValues();"+
"for(i=0;i<list.length;i++){"+
"if( list[i] > 0 && list[i] != null){"+
"allnull = false;"+
"}}}}"+

"function beforeRendering( gcs, icsc ){"+
"var chart = gcs.getChartModel();"+
"if( allnull ){" +
"chart.getPlot().setVisible(false);"+
"chart.getTitle().getLabel().getCaption().setValue('Data is empty');"+
"}}"
);


and as i said, the function works, but my display simply reads:

"Dat..."
Re: hiding labels for zero values using API [message #830753 is a reply to message #830457] Wed, 28 March 2012 02:48 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Why not use the api

Label EmptyMsg = LabelImpl.create();
EmptyMsg.getCaption().setValue("My Empty Message");
EmptyMsg.setVisible(true);
cwaBar.setEmptyMessage(EmptyMsg);

Jason


On 3/27/2012 12:58 PM, Missing name Mising name wrote:
> Jason,
> I have one more issue that i am having a difficult time figuring out.
> Now that you clarified my issue with using the javascript
> events/functions I found a useful function online that displays a
> message in the event that all data is equal to 0. The functions works
> perfect, however, the title box will not resize and it only displays the
> first word of the message and then appends a "..." to the end. No matter
> what i do the title box will not enlarge enough to display all the text.
> Here is my js function:
>
> cwoaPie.setScript(
> "var allnull = true;"+
> "function afterDataSetFilled( series, dataSet, icsc ){"+
> "if( series.getSeriesIdentifier() == '' ){"+
> "var list = dataSet.getValues();"+
> "for(i=0;i<list.length;i++){"+
> "if( list[i] > 0 && list[i] != null){"+
> "allnull = false;"+
> "}}}}"+
>
> "function beforeRendering( gcs, icsc ){"+
> "var chart = gcs.getChartModel();"+
> "if( allnull ){" +
> "chart.getPlot().setVisible(false);"+
> "chart.getTitle().getLabel().getCaption().setValue('Data is empty');"+
> "}}"
> );
>
>
> and as i said, the function works, but my display simply reads:
>
> "Dat..."
Re: hiding labels for zero values using API [message #831087 is a reply to message #830753] Wed, 28 March 2012 13:02 Go to previous messageGo to next message
Missing name Missing name is currently offline Missing name Missing nameFriend
Messages: 36
Registered: November 2011
Member
Jason,

I may be confused as to how the emptyMessage works... I have tried using that same code before, in the end even when all data is 0 my graph still doesnt apply it, so am i misunderstanding that a 0 value isnt the same as a null value? As of now i have been unable to get the emptyMessage to show up. The code i had put in the question above was able to recognize when to display the message i wanted, however, it didnt display it correctly, is there a way we can merge the two codes?

thanks
john
Re: hiding labels for zero values using API [message #832196 is a reply to message #831087] Thu, 29 March 2012 22:22 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

You are correct. Empty message happens when your chart has not data. A
0 is valid data. The issue you are having is that the title block is
already calculated by the time you change it. You have two choices to
fix the issue you are having. One is to move the title caption setting
to beforeGeneration and check to to see if all values are 0 in the
afterDataSetFilled script which is called before the beforeGeneration:


chartEmpty = true;
function afterDataSetFilled(series, dataSet, icsc)
{
importPackage( Packages.java.io );
importPackage( Packages.org.eclipse.birt.chart.model.type.impl );

if( series.getClass() == LineSeriesImpl ){
series.getLineAttributes().setThickness(5);
var list = dataSet.getValues();

for ( i=0; i<list.length; i=i+1)
{
if ( list[i] > 0 )
{
chartEmpty = false;
}
}

}

}

Or when you create the chart to begin with put a long title in.


cwaBar.getTitle().getLabel().getCaption().setValue("My VERY LONG TITLE
Makes Space");


Jason


On 3/28/2012 9:02 AM, Missing name Mising name wrote:
> Jason,
>
> I may be confused as to how the emptyMessage works... I have tried using
> that same code before, in the end even when all data is 0 my graph still
> doesnt apply it, so am i misunderstanding that a 0 value isnt the same
> as a null value? As of now i have been unable to get the emptyMessage to
> show up. The code i had put in the question above was able to recognize
> when to display the message i wanted, however, it didnt display it
> correctly, is there a way we can merge the two codes?
>
> thanks
> john
Previous Topic:Unexpected behavior difference between IReportContext.setGlobalVariable/setPageVariable
Next Topic:Script Functions: how are Java objects handled in JS?
Goto Forum:
  


Current Time: Tue Nov 25 00:52:16 GMT 2014

Powered by FUDForum. Page generated in 0.02771 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software