开发者

How to conditionally format axis values in Silverlight Toolkit LineSeries

I am trying to conditionally format the numbers that appear in a NumericAxis axis for a LineSeries (from Silverlight 4 Toolkit). To be more specific, I want numbers that are >=10000 and <=0.0001 to display in scientific notation, but I can't seem to make this work.

I can override the NumericAxisLabel template like this:

    <Style x:Key="NumericAxisLabelStyle" TargetType="chartingToolkit:NumericAxisLabel">
        <Setter Property="IsTabStop" Value="False"/>            
        <Setter Property="StringFormat" Value="{}{0:0.0E+00}" />                        
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="chartingToolkit:NumericAxisLabel">
                    <TextBlock Text="{TemplateBinding FormattedContent}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

But this will apply the scientific notation format to ALL labels in the axis. What I want is for the string format expression to "kick in" only when the condition I mentioned above occurs.

I was able to accomplish this in the LineDataPoint tooltip template fairly easy by using a binding with a custom value converter, like this:

 <ControlTemplate TargetType="chartingToolkit:LineDataPoint">
      <Grid x:Name="Root" Opacity="0">
           <ToolTipService.ToolTip>
                <StackPanel Margin="2,2,2,2">
                    <StackPanel Orientation="Horizontal">
                开发者_JAVA百科        <TextBlock Text="X:" />                                        
                        <ContentControl Content="{Binding objResultValueX, Converter={StaticResource ToCustomStringFormat}}"/>
                     </StackPanel>
                     <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Y:" />
                        <ContentControl Content="{Binding dblResultValueY, Converter={StaticResource ToCustomStringFormat}}"/>
                      </StackPanel>
                 </StackPanel>
            </ToolTipService.ToolTip>
            ...
    </Grid>
 </ControlTemplate>

If only I could specify a converter for the "FormattedContent" in the NumericAxisLabelStyle like I do for LineDataPoint template...surely there must be a way!

Any ideas?

Thanks in advance for the help!


Try setting the DataContext of the TextBlock to FormattedContent. Then apply the converter to the Text property as so:

<Style x:Key="NumericAxisLabelStyle" TargetType="chartingToolkit:NumericAxisLabel"> 
    <Setter Property="IsTabStop" Value="False"/> 
    <Setter Property="Template"> 
    <Setter.Value > 
        <ControlTemplate TargetType="chartingToolkit:NumericAxisLabel"> 
            <TextBlock DataContext="{TemplateBinding FormattedContent}" Text ="{Binding Converter={StaticResource ToCustomStringFormat}}"/> 
        </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 


It's also possible to override the PrepareAxisLabel() method from the Toolkit's DisplayAxis class.

The source code for the original method (found here) is:

protected virtual void PrepareAxisLabel(Control label, object dataContext)
    {
        label.DataContext = dataContext;
        label.SetStyle(AxisLabelStyle);
    }

So, you can override it with something like:

public class MyLinearAxis : LinearAxis
{     
    protected override void PrepareAxisLabel(Control label, object dataContext)
    {   
        (label as AxisLabel).StringFormat = "{0:c}";   // currency format, for example
        dataContext = 10.0;                            // your own custom numeric value

        base.PrepareAxisLabel(label, dataContext);
    }
}

In this way, you can get total control over the label as it's created.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜