Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1060
Dynamic label format on Y-Axis
posted

We are using DataChart in our view in which we display series of selected multiple (from dropdown above the chart). Few multiple series must be displayed in Percentage format (P2) and few in Number format (N2). We have chart Y-Axis definition like this:

<ig:NumericYAxis x:Name="yAxis1" MajorStroke="Gray" MinimumValue="{Binding ChartMinValue}" MajorStrokeThickness="1.5" MajorStrokeDashArray="1 2">
<ig:NumericYAxis.Label>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0">
<TextBlock Text="{Binding Item, StringFormat=N2}" Width="40" Foreground="Black" FontSize="11"></TextBlock>
<Border Background="Gray" Width="5" Height="3"/>
</StackPanel>
</DataTemplate>
</ig:NumericYAxis.Label>

Can anybody please help, how to dynamically set "StringFormat" in Textblock based on selected multiple?

  • 34690
    Verified Answer
    Offline posted

    Hello mirfan,

    I am a little unsure of your requirement in this case, but it sounds to me like you are looking to switch the formats of your NumericYAxis labels based on the series that are shown in your chart. If this is not the case, please let me know.

    If you are looking to continue with the DataTemplate for your NumericYAxis, I would recommend defining a separate TextBlock in your template that has the P2 StringFormat that you are looking for. You can then place a property on the data context of your XamDataChart that defines which format you wish to use in this case. The data context of your elements in your TextBlock has two properties, Item, which you are currently bound to, and Axis, which will return the owning NumericYAxis. The NumericYAxis has a Chart property that returns the XamDataChart, and so you can get to your "flag" property by binding to the following:

    "{Binding Axis.Chart.DataContext.<propertyName>}"

    Doing this will allow you to toggle the Visibility of the two formatted TextBlocks based on the format that you wish to show.

    I have attached a sample project to demonstrate the above. I hope this helps.

    Please let me know if you have any other questions or concerns on this matter.

    Sincerely,
    Andrew
    Associate Developer

    DataChartDynamicFormats.zip