← 返回所有博客文章

在 ComponentOne WPF&Silverlight Chart3D 的使用方法  文章中我了解了 3D 图表的基本使用方法。本篇文章中我们将叙述如何自定义坐标轴标签。

C1Chart3D 坐标轴标签由 AnnoTemplate 属性设置,我们可以通过自定义 DataTemlate 来实现标签的转换。实现起来也比较简单,使用 TextBlock 作为容器即可,XAML 代码如下:

 1: <c1:C1Chart3D Name="c1Chart3D1" Grid.Row="1">
 2:     <c1:C1Chart3D.Resources>
 3:         <local:AnnoLabelConverter x:Key="alc" />
 4:         <DataTemplate x:Key="AnnoLabelTemplate">
 5:             <TextBlock Text="{Binding Converter={StaticResource alc} }"/>
 6:         </DataTemplate>
 7:     </c1:C1Chart3D.Resources>
 8:     <c1:C1Chart3D.AxisY>
 9:         <c1:Axis3D  AnnoTemplate="{StaticResource AnnoLabelTemplate}"/>
 10:     </c1:C1Chart3D.AxisY>
 11:     <c1:C1Chart3D.Legend>
 12:         <c1:C1Chart3DLegend />
 13:     </c1:C1Chart3D.Legend>
 14: </c1:C1Chart3D>

接下来,我们创建 AnnoLabelConverter class 来转换标签值,需要实现 IValueConverter 接口,

 1: public class AnnoLabelConverter : IValueConverter
 2: {
 3:     public object Convert(object value, System.Type targetType, object parameter, 
System.Globalization.CultureInfo culture)
 4:     {
 5:         if (value != null)
 6:         {
 7:             int d;
 8:             if(int.TryParse(value.ToString(), out d))
 9:             {
 10:                 if (d == 0)
 11:                     return "Low";
 12:                 else if (d == 5)
 13:                     return "Medium";
 14:                 else if (d == 10)
 15:                     return "High";
 16:                 else
 17:                     return "";
 18:             }
 19:         }
 20:         return "";
 21:     }
 22:  
 23:     public object ConvertBack(object value, System.Type targetType, object parameter,
 System.Globalization.CultureInfo culture)
 24:     {
 25:         return null;
 26:     }
 27: }

 

最后,你仅需要在 XAML 页面引用当前命名空间即可:xmlns:local

效果图如下: