Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Date format strings using locale
Date format strings using locale [message #484893] Wed, 09 September 2009 15:52 Go to next message
Eclipse UserFriend
Originally posted by: dfc.dcallaghan.co.uk

Hi,

I have a number of reports some of which display monthly data and some
daily data. Dates are displayed on the X axis of charts, in tables and in
data items (e.g. in a header to specifies the date range) and all works
fine, except....

...the charts. I generally use one of 2 format string for the dates. In
daily reports I use "dd MMM YYYY" and monthly I use "MMM YYYY" as the day
is irrelevant. When I change local the dates are formatted correctly and
the correct language is used everywhere else in the reports (e.g. Avril or
April) but the charts always show english dates.

Only when I change the chart to use a standard format like MEDIUM or SHORT
do they work correctly. Is this a bug? Do the charts not support
internationalization in dates with advanced format strings? Is there
another solution?

I can use a standard format string in the daily reports but it doesn't
make sense in the monthly reports - I really need to avoid displaying the
day component.

Any help would be appreciated. Thanks,

Darren.
Re: Date format strings using locale [message #484924 is a reply to message #484893] Wed, 09 September 2009 17:40 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Darren,

What version are you using? You could put a script on the chart like:

function beforeDrawAxisLabel( axis, label, context )
{
importPackage(Packages.org.eclipse.birt.chart.model.attribut e);
importPackage(Packages.java.text);

if (axis.getType() == AxisType.DATE_TIME_LITERAL)
{
value = label.getCaption().getValue();
importPackage( Packages.java.util );

var dtf = new SimpleDateFormat("yy",
context.getExternalContext().getScriptable().getLocale() );
var dtf2 = new SimpleDateFormat("MM/dd",
context.getExternalContext().getScriptable().getLocale());


var dt = new Date(value);
var fn1 = dtf2.format(dt);
}
}


Can you also log a bug for this?

Jason

Darren Callaghan wrote:
> Hi,
>
> I have a number of reports some of which display monthly data and some
> daily data. Dates are displayed on the X axis of charts, in tables and
> in data items (e.g. in a header to specifies the date range) and all
> works fine, except....
>
> ..the charts. I generally use one of 2 format string for the dates. In
> daily reports I use "dd MMM YYYY" and monthly I use "MMM YYYY" as the
> day is irrelevant. When I change local the dates are formatted correctly
> and the correct language is used everywhere else in the reports (e.g.
> Avril or April) but the charts always show english dates.
>
> Only when I change the chart to use a standard format like MEDIUM or
> SHORT do they work correctly. Is this a bug? Do the charts not support
> internationalization in dates with advanced format strings? Is there
> another solution?
>
> I can use a standard format string in the daily reports but it doesn't
> make sense in the monthly reports - I really need to avoid displaying
> the day component.
>
> Any help would be appreciated. Thanks,
>
> Darren.
>
>
Re: Date format strings using locale [message #485045 is a reply to message #484924] Thu, 10 September 2009 11:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dfc.dcallaghan.co.uk

Thanks Jason,

I've raised a bug for this (289050). I'm working with BIRT 2.5. Adding the
following javascript solves the problem:

function beforeDrawAxisLabel( axis, label, context )
{
importPackage(Packages.org.eclipse.birt.chart.model.attribut e);
importPackage(Packages.java.text);

if (axis.getType() == AxisType.DATE_TIME_LITERAL)
{
value = label.getCaption().getValue();
importPackage( Packages.java.util );

var df = new SimpleDateFormat("MMM yyyy",
context.getExternalContext().getScriptable().getLocale() );
var dt = new Date(value);
var newValue = df.format(dt);

label.getCaption().setValue(newValue);
}
}

The one thing I noticed was that I still have to format the chart date
using one of the standard formats otherwise the Date object created was
incorrect. Other than that this solution works fine - it's just a pain to
have to add this to every monthly chart.

Thanks again for the help, you're providing a lot of good support on this
news group.

Darren.
Re: Date format strings using locale [message #485068 is a reply to message #485045] Thu, 10 September 2009 13:15 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: dfc.dcallaghan.co.uk

Sorry - my last email needs a correction. I forgot how awkward date
strings can be and to properly support various client locales I need to
force the chart date format to somethings consistent like "dd-MMM-yyyy".
Then I can use the following script in the chart:

/**
* Called before rendering each label on a given Axis.
*
* @param axis
* Axis
* @param label
* Label
* @param context
* IChartScriptContext
*/

function beforeDrawAxisLabel( axis, label, context )
{
importPackage(Packages.org.eclipse.birt.chart.model.attribut e);
importPackage(Packages.java.text);

if (axis.getType() == AxisType.DATE_TIME_LITERAL)
{
var dfin = new SimpleDateFormat("dd-MM-yyyy");
var dfout = new SimpleDateFormat("MMM yyyy",
context.getExternalContext().getScriptable().getLocale() );

value = label.getCaption().getValue();
importPackage( Packages.java.util );

var dt = dfin.parse(value);
label.getCaption().setValue(dfout.format(dt));
}
}
Re: Date format strings using locale [message #485195 is a reply to message #485068] Thu, 10 September 2009 19:11 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Darren,

One other note. The beforeDrawAxisLabel is called for every axis label,
so you might want to set it once in the beforeGeneration like:

function beforeGeneration(chart, icsc)
{

importPackage( Packages.org.eclipse.birt.chart.model.attribute.impl );
importPackage( Packages.org.eclipse.birt.chart.model.attribute );

//currently the chart model only supports one base axis
xAxis = chart.getBaseAxes()[0];
yAxis = chart.getOrthogonalAxes( xAxis, true)[0];
yAxisNumber2 = chart.getOrthogonalAxes( xAxis, true)[1];
if ( xAxis.getType() == AxisType.DATE_TIME_LITERAL)
{
xAxis.setFormatSpecifier(
JavaDateFormatSpecifierImpl.create("MM-dd-yyyy") );
}
}

Jason


Darren Callaghan wrote:
> Sorry - my last email needs a correction. I forgot how awkward date
> strings can be and to properly support various client locales I need to
> force the chart date format to somethings consistent like "dd-MMM-yyyy".
> Then I can use the following script in the chart:
>
> /**
> * Called before rendering each label on a given Axis.
> * * @param axis
> * Axis
> * @param label
> * Label
> * @param context
> * IChartScriptContext
> */
>
> function beforeDrawAxisLabel( axis, label, context )
> {
> importPackage(Packages.org.eclipse.birt.chart.model.attribut e);
> importPackage(Packages.java.text);
>
> if (axis.getType() == AxisType.DATE_TIME_LITERAL)
> {
> var dfin = new SimpleDateFormat("dd-MM-yyyy");
> var dfout = new SimpleDateFormat("MMM yyyy",
> context.getExternalContext().getScriptable().getLocale() );
>
> value = label.getCaption().getValue();
> importPackage( Packages.java.util );
>
> var dt = dfin.parse(value);
> label.getCaption().setValue(dfout.format(dt));
> }
> }
>
>
Previous Topic:Stacked Bar-Chart, 2 Y-Axis, 2 Series
Next Topic:Nested tables + truncated images + Word emitter
Goto Forum:
  


Current Time: Thu Sep 19 13:46:59 GMT 2024

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

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

Back to the top