我有以下XAML
定义:
<Charting:Chart x:Name="ColumnChart"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="Auto"
Height="Auto"
Padding="50"
Title="100 random numbers">
<Charting:ColumnSeries Title="Skills"
IndependentValuePath="Name"
DependentValuePath="Pts"
IsSelectionEnabled="True">
</Charting:ColumnSeries>
</Charting:Chart>
如何旋转标签(例如,在-90度上旋转)以使其更具可读性?
最佳答案
可以旋转标签。不幸的是,由于WinRT XAML布局中缺少一项功能(可能是一个自定义类),因此需要几个步骤。
可以找到here的核心思想。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Charting:Chart x:Name="ColumnChart"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Padding="50"
Title="100 random numbers">
<Charting:ColumnSeries Title="Skills" x:Name="theColumnSeries"
IndependentValuePath="Name"
DependentValuePath="Pts"
IsSelectionEnabled="True">
<Charting:ColumnSeries.IndependentAxis>
<Charting:CategoryAxis Orientation="X">
<Charting:CategoryAxis.AxisLabelStyle>
<Style TargetType="Charting:AxisLabel">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Charting:AxisLabel">
<TextBlock Text="{TemplateBinding FormattedContent}">
<TextBlock.lay
<TextBlock.RenderTransform>
<RotateTransform Angle="-60" />
</TextBlock.RenderTransform>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Charting:CategoryAxis.AxisLabelStyle>
</Charting:CategoryAxis>
</Charting:ColumnSeries.IndependentAxis>
</Charting:ColumnSeries>
</Charting:Chart>
</Grid>
我使用的C#代码:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var rnd = new Random(444);
ObservableCollection<Demo> values = new ObservableCollection<Demo>();
for (int i = 0; i < 15; i++)
{
values.Add(new Demo() { Name = (rnd.NextDouble() * 100).ToString(), Pts = i });
}
((ColumnSeries)ColumnChart.Series[0]).ItemsSource = values;
}
}
class Demo
{
public string Name { get; set; }
public double Pts { get; set; }
}
但是,不幸的是,这并不是您想要的。问题在于WPF中不存在LayoutTransform。如果您按上述方式运行代码,则标签会旋转,但它们会与其他内容重叠。
该博客的作者编写了一个LayoutTransformer类,尽管该类是为Silverlight设计的(因此它可以移植并与WinRT XAML一起使用),但它可能有助于解决问题。
关于c# - 如何旋转WinRTXamlToolkit柱形图的标签?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30636496/