【问题标题】:WPF Charts - Rotate XAXIS labelsWPF 图表 - 旋转 XAXIS 标签
【发布时间】:2014-05-27 00:57:36
【问题描述】:

我希望有人可以帮助我。我是一名业余开发人员(充其量),主要编写代码来帮助解析日志文件,而我正在努力制作图表。

在下面的代码中,我只是接收任何 DataTable(具有正确命名的列和数据类型)并使用 WPF 工具包创建一个堆叠图表。我很难旋转 XAXIS 标签。

我查看了所有这些链接并尝试将它们整合到我已经完成的工作中,但没有任何效果,或者我不确定如何解释这些链接。在查看我的图表是如何创建的之后,谁能指导我如何旋转我的 XAXIS?

Silverlight: How to change AxisLabelStyle in code behind?

http://blogs.msdn.com/b/delay/archive/2010/03/06/turn-your-head-and-check-out-this-post-how-to-easily-rotate-the-axis-labels-of-a-silverlight-wpf-toolkit-chart.aspx

Create style to rotate axis label in code behind

这是我的代码:

XAML:

<Window x:Class="gtseq_stats.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Main" Height="451" Width="685" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" RenderTransform="{Binding StringFormat=\{0:g\}}">
    <Grid ForceCursor="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="552*" />
            <ColumnDefinition Width="142*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="2,2,0,0" Name="cbx_report_name" VerticalAlignment="Top" Width="320" DisplayMemberPath="report_name_alias" SelectedValuePath="report_data_id" SelectionChanged="cbx_report_name_SelectionChanged" />
        <toolkit:Chart Margin="10,31,12,12" Name="OutputChart" Grid.ColumnSpan="2" />        
        <Button Content="Button" Height="26" HorizontalAlignment="Left" Margin="350,2,0,0" Name="button1" VerticalAlignment="Top" Width="53" Click="button1_Click" FlowDirection="RightToLeft" />
    </Grid>

</Window>

然后是我的 C#:

    public void BuildStackedChart(DataTable dtResults)
    {
        OutputChart.Series.Clear();
        var palette = OutputChart.Palette;
        OutputChart.Palette = null;
        OutputChart.Palette = palette;

        Dictionary<string, int> dictYAxis = new Dictionary<string,int>();

        try
        {
            //Get a list of distinct Y axis and add them to a dictionary.  This will be used later so we can assigne values to the proper dataValues.
            var distinctYAxis = (from row in dtResults.AsEnumerable()
                                 //orderby row.Field<string>("fix_line") ascending
                                 select row.Field<string>("YAXIS")).Distinct();
            int i = 0;
            foreach (var name in distinctYAxis)
            {
                dictYAxis.Add(name, i);
                i++;
            }
        }
        catch
        {
            MessageBox.Show("Couldn't Properly Load Data.  The data must have only 3 columns:  XAXIS-DateTime Data Type" + Environment.NewLine + "YAXIS-String Value" + Environment.NewLine + "PLOTVALUES-Number");
            return;
        }

        var dataValues = new List<List<SimpleDataValue>>();
        try
        {
            //Add a new entry for however many YAxis we have (No Data is used at this point are we aren't linking the #'s in the dictionary to the data elements here.
            //However, we will use the dictorary to add the enteries and use the numbers to respresent the rows.
            foreach(KeyValuePair<string, int> pair in dictYAxis)
            {
                dataValues.Add(new List<SimpleDataValue>());
            }

            foreach (DataRow row in dtResults.Rows) // Loop over the rows.
            {
                dataValues[dictYAxis[row["YAXIS"].ToString()]].Add(new SimpleDataValue { DependentValue = Convert.ToDouble(row["PLOTVALUES"]), IndependentValue = Convert.ToDateTime(row["XAXIS"])});
            }               

        }
        catch
        {
            dataValues.Clear();
        }

        int i2 = 0;
        var stackedSeries = Activator.CreateInstance(typeof(StackedColumnSeries)) as DefinitionSeries;
        foreach (var values in dataValues)
        {
            var definition = new SeriesDefinition();
            definition.DependentValuePath = "DependentValue";
            definition.IndependentValuePath = "IndependentValue";
            definition.Title = dictYAxis.FirstOrDefault(x => x.Value == i2).Key; ;
            definition.ItemsSource = values;                
            stackedSeries.SeriesDefinitions.Add(definition);
            i2++;
        }
        OutputChart.Series.Add(stackedSeries);
    }

【问题讨论】:

  • 您想旋转 x 轴文本值吗?
  • 是的。我正在尝试旋转 x 轴文本值。
  • 请帮忙。我刚试过这个 RotateTransform rotateTransform1 = new RotateTransform(-45); OutputChart.RenderTransform = rotateTransform1;,但是现在hold chart是旋转的:

标签: c# wpf xaml wpftoolkit


【解决方案1】:

查看这个帖子[How to: Easily rotate the axis labels of a Silverlight/WPF Toolkit chart]

<charting:ColumnSeries.IndependentAxis>
                <charting:CategoryAxis
                    Orientation="X">
                    <charting:CategoryAxis.AxisLabelStyle>
                        <Style TargetType="charting:AxisLabel">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="charting:AxisLabel">
                                        <layout:LayoutTransformer>
                                            <layout:LayoutTransformer.LayoutTransform>
                                                <RotateTransform Angle="-60"/>
                                            </layout:LayoutTransformer.LayoutTransform>
                                            <TextBlock Text="{TemplateBinding FormattedContent}"/>
                                        </layout:LayoutTransformer>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </charting:CategoryAxis.AxisLabelStyle>
                </charting:CategoryAxis>
            </charting:ColumnSeries.IndependentAxis>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多