这篇文章主要介绍定制 C1Chart3D 坐标轴标签的方法。关于 C1Chart3D 的基本使用方法,可以参考:点击。
我们主要通过 AnnoTemplate 来实现标签自定义。我们可以分配该属性到坐标轴的 DataTemplate 。下面为一段简单的代码片段:
<c1:C1Chart3D Name="c1Chart3D1" Grid.Row="1">
<c1:C1Chart3D.Resources>
<local:AnnoLabelConverter x:Key="alc" />
<DataTemplate x:Key="AnnoLabelTemplate">
<TextBlock Text="{Binding Converter={StaticResource alc} }"/>
</DataTemplate>
</c1:C1Chart3D.Resources>
<c1:C1Chart3D.AxisY>
<c1:Axis3D AnnoTemplate="{StaticResource AnnoLabelTemplate}"/>
</c1:C1Chart3D.AxisY>
<c1:C1Chart3D.Legend>
<c1:C1Chart3DLegend />
</c1:C1Chart3D.Legend>
</c1:C1Chart3D>
接下来,我们将定义 AnnoLabelConverter 类,该类将转换坐标轴上的数字为字符串。我们可以在类中编写逻辑去选择字符串的内容。添加以下类到你的工程中。确保添加 xmlns:local 命名空间到 XAML 页面。
public class AnnoLabelConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null)
{
int d;
if (int.TryParse(value.ToString(), out d))
{
if (d == 0)
return "Low";
else if (d == 5)
return "Medium";
else if (d == 10)
return "High";
else
return "";
}
}
return "";
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
这个数值转换类预定义 3 个数值对应的字符串。可以通过你的定义来更多丰富的样式: