在为C1Chart for WPF添加自定义标题、坐标轴单位标签以及旋转坐标轴注释这篇文章里,介绍了在WPF平台下添加自定义的标题。但有人提出希望标题剧中,坐标轴单位标签放到特定位置。本文在上一篇文章的基础上,改造Sample,达到需要的效果。
1.标题剧中
有两种方法可以让标题居中,第一种是使用WPF的绑定和Convert。将显示标题的TextBlockTranslateTransform.X向左平移,放置中间。为了达到这个效果,需要将C1Chart的宽度除以二,然后绑定给TextBlockTranslateTransform.X。
XMAL代码参考:
<textblock horizontalalignment="Center" dockpanel.dock="Top" text="2014年度报表"> <textblock.rendertransform> <translatetransform x="{Binding Path=ActualWidth,ElementName=c1Chart1,Converter={StaticResource widthConverter}}" /> </textblock.rendertransform> </textblock>
Converter代码:
[ValueConversion(typeof(double), typeof(double))] public class TitileAlignmentConvert : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double width = 0; if (double.TryParse(value.ToString(), out width)) { width = width / 2; } return width; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return value; } }
第二种方式,就是直接从C#代码,计算向左偏移的位置,然后赋值给TextBlockTranslateTransform.X.
代码参考:
private void Window_Loaded(object sender, RoutedEventArgs e) { TranslateTransform f = new TranslateTransform(); f.X = (this.c1Chart1.ActualWidth - this.chartTitle.ActualWidth) / 2; this.chartTitle.RenderTransform = f; }
2.坐标单位标签
将X轴的标签放在X轴的位置,将Y轴的单位标签放在Y轴的位置。需要将TextBlock放在Axis.Title中。
拿Y轴举例,代码参考:
<c1:C1Chart.View> <c1:ChartView> <c1:ChartView.AxisY> <c1:Axis> <c1:Axis.Title> <TextBlock Text="单位:万元" TextAlignment="Center" Foreground="Crimson"/> </c1:Axis.Title> </c1:Axis> </c1:ChartView.AxisY> </c1:ChartView> </c1:C1Chart.View>
运行起来效果如下图所示:
Sample如下: