c# - 使用代码隐藏堆叠列系列在 WPF 图表上旋转独立轴标签

标签 c# wpf xaml charts wpftoolkit

我有一个 WPF 堆积柱形图,显示每个日期范围内每种类型的伤害数量。

每个独立(X 轴)值都是一个月或一年,因此我不能只使用直接的 DateTime 作为标签类型。

我试图将这些标签旋转 45 度,但它实际上为这种样式添加了一组额外的标签,从 0 到 1.0,并将我想要的标签移动到图表顶部(添加分隔线以节省图像宽度):

enter image description here

生成图表数据的代码:

var dataSeries = new StackedColumnSeries();
// injuryTypes is of type List<KeyValuePair<string, string>>.  KVP example: "Heat Burns", "HB" 
// injuries is of type List<List<KeyValuePair<string, decimal>>>.  KVP example: "2015 Avg. Mo.", 12

chtHoursLost.Series.Clear();

for (var i = 0; i < injuries.Count; i++)
{
    var columnColor = new Style();
    var injurySeriesDef = new SeriesDefinition()
    {
        ItemsSource = injuries[i],
        DependentValuePath = "Value",
        IndependentValuePath = "Key"
    };

    injurySeriesDef.Title = string.Format("{0} ({1})", ((KeyValuePair<string, string>)
        (injuryTypes[i])).Value, injuryTypes[i].Key);
    columnColor.Setters.Add(new Setter(BackgroundProperty, new SolidColorBrush(
        Utility.GetInjuryColor(((KeyValuePair<string, string>)(injuryTypes[i])).Key))));
    injurySeriesDef.DataPointStyle = columnColor;
    dataSeries.SeriesDefinitions.Add(injurySeriesDef);
}

chtHoursLost.Series.Add(dataSeries);

var tiltedAxisLabelStyle = ((Style)(this.FindResource("stlAngledDates")));
axsHoursLostX.AxisLabelStyle = tiltedAxisLabelStyle;

相关XAML:

xmlns:chartingToolkit="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
<Window.Resources>
    <Style x:Key="stlAngledDates" TargetType="{x:Type chartingToolkit:AxisLabel}">
        <!--<Setter Property="StringFormat" Value="{}{0:d-MMM}" />-->
        <Setter Property="RenderTransformOrigin" Value="1,0.5" />
        <Setter Property="RenderTransform">
            <Setter.Value>
                <RotateTransform Angle="-45" />
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
...
<chartingToolkit:Chart x:Name="chtHoursLost" Grid.Row="1" Grid.Column="0" Margin="10,0,10,10" Title="Hours Lost/Restricted By Injury" Height="auto" Width="auto">
    <chartingToolkit:StackedColumnSeries x:Name="scsHoursLost" />
    <chartingToolkit:Chart.Axes>
        <chartingToolkit:LinearAxis x:Name="axsHoursLostX" Orientation="X"/>
        <chartingToolkit:LinearAxis Orientation="Y"  Title="Hours Lost" Minimum="0" Interval="35" ShowGridLines="True" />
    </chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>

我也尝试过类似的方法,虽然额外的标签集消失了,但正确的标签没有旋转。

var injurySeriesDef = new SeriesDefinition()
{
    ItemsSource = injuries[i],
    DependentValuePath = "Value",
    IndependentValuePath = "Key",
    RenderTransformOrigin = new Point(1, 0.5),
    RenderTransform = new RotateTransform() { Angle = -45 }
};

如何让日期范围标签旋转?谢谢...

最佳答案

您可能想改用CategoryAxis。另外,增加其高度以正确容纳标签,如下所示:

<chartingToolkit:Chart x:Name="chtHoursLost" Grid.Row="1" Grid.Column="0" Margin="10,0,10,10" Title="Hours Lost/Restricted By Injury" Height="auto" Width="auto">
    <chartingToolkit:StackedColumnSeries x:Name="scsHoursLost" />
    <chartingToolkit:Chart.Axes>
        <chartingToolkit:CategoryAxis x:Name="axsHoursLostX" Orientation="X" Height="50" />
        <chartingToolkit:LinearAxis Orientation="Y"  Title="Hours Lost" Minimum="0" Interval="35" ShowGridLines="True" />
    </chartingToolkit:Chart.Axes>
</chartingToolkit:Chart>

enter image description here

关于c# - 使用代码隐藏堆叠列系列在 WPF 图表上旋转独立轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621927/

相关文章:

c# - 使用 .NET/WPF 预加载图像资源

c# - WebAPI 无法解析 multipart/form-data post

c# - 实现接口(interface) C#

c# - Windows 窗体 : Change application mainwindow at runtime

c# - 如何在 Monogame 中全屏启动?

c# - Thread.CurrentPrincipal.Identity.Name 在 WPF 中为空

c# - 总虚拟内存为我的应用程序返回不切实际的数字

WPF 数据网格 : Binding DataGridColumn visibility to ContextMenu MenuItems IsChecked (MVVM)

c# - 构建支持我自己的自定义事件的 EventTriggerBehavior

xaml - 具有固定颜色的 Windows Phone 跳转列表