|
Axis.setLabelFormat for dates... internationalization |
Posted by Rob MacFadyen on Jun-22-2011 05:27 |
|
Hi,
The X axis tick labels for a line chart of dates appear to use a default format of (.NET for
v4.1, from the developer docs):
By default, ChartDirector will try to guess if the axis represents numbers or dates. If the
axis represents numbers, it will use "{value}" as the default format. If the axis represents
dates, it will guess the format based on resolution of the dates (e.g. whether the dates
contain hourly data or monthly data, etc). It may use formats such as
{value|mm/dd/yy<*br*>hh:nn:ss}, {value|mm/dd/yy hh:nn:ss}, {value|mm/dd/yyyy},
{value|mm/yyyy} or {value|yyyy}.
The trouble I have is that formats like "mm/dd/yy" or "mm/dd/yyyy" don't seem to pay
attention to the current threads culture.
This is ok as I can work around by explicitly setting the format based on the current
culture (Axis.setLabelFormat).
However... I'm unclear as to how to automatically determine which format best suits the
data. How does the chart decide between "{value|mm/dd/yy<*br*>hh:nn:ss},
{value|mm/dd/yy hh:nn:ss}, {value|mm/dd/yyyy}, {value|mm/yyyy} or {value|yyyy}"
which one is best for the current data? I presume it uses the shorter label (eg
{value|yyyy}) when there only a few ticks and each tick represents a different year... or
some other method?
Can you share how this decision is being made? I'm hoping to duplicate the decision
making and feed setLabelFormat the appropriately sized format (customized for month,
day, year ordering based on culture).
Thanks!
Rob |
Re: Axis.setLabelFormat for dates... internationalization |
Posted by Peter Kwan on Jun-22-2011 16:44 |
|
Hi Rob,
If your date range is variable and the axis is auto-scaled by ChartDirector (so your code does not know what are the tick spacing), you may use Axis.setFormatCondition to determine the format to use in various cases.
The followings are part of the code from the "Zooming and Scrolling Demonstation" sample code. In that sample code, the chart can zoom. So the date range is variable. The following code formats the x-axis based on the tick spacing. (It uses a more complicated formatting than that of the default.)
// If all ticks are yearly aligned, then we use "yyyy" as the label format.
c.xAxis().setFormatCondition("align", 360 * 86400);
c.xAxis().setLabelFormat("{value|yyyy}");
// If all ticks are monthly aligned, then we use "mmm yyyy" in bold font as the first
// label of a year, and "mmm" for other labels.
c.xAxis().setFormatCondition("align", 30 * 86400);
c.xAxis().setMultiFormat(Chart.StartOfYearFilter(), "<*font=bold*>{value|mmm yyyy}",
Chart.AllPassFilter(), "{value|mmm}");
// If all ticks are daily algined, then we use "mmm dd<*br*>yyyy" in bold font as the
// first label of a year, and "mmm dd" in bold font as the first label of a month, and
// "dd" for other labels.
c.xAxis().setFormatCondition("align", 86400);
c.xAxis().setMultiFormat(
Chart.StartOfYearFilter(), "<*block,halign=left*><*font=bold*>{value|mmm dd<*br*>yyyy}",
Chart.StartOfMonthFilter(), "<*font=bold*>{value|mmm dd}");
c.xAxis().setMultiFormat2(Chart.AllPassFilter(), "{value|dd}");
// For all other cases (sub-daily ticks), use "hh:nn<*br*>mmm dd" for the first label of
// a day, and "hh:nn" for other labels.
c.xAxis().setFormatCondition("else");
c.xAxis().setMultiFormat(Chart.StartOfDayFilter(), "<*font=bold*>{value|hh:nn<*br*>mmm dd}",
Chart.AllPassFilter(), "{value|hh:nn}");
Hope this can help.
Regards
Peter Kwan |
|