WPF图表 - 旋转XAXIS标签

时间:2014-04-12 22:19:47

标签: c# wpf xaml wpftoolkit

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

在下面的代码中,我只是接受任何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);
    }

1 个答案:

答案 0 :(得分:0)

查看此帖子[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>