2014-04-12 79 views
1

我希望有人能幫助我。我是一個業餘開發者(充其量),主要是編寫代碼來幫助解析日誌文件,而我正在努力繪製圖表。WPF圖表 - 旋轉XAXIS標籤

在下面的代碼中,我簡單地使用任何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

要旋轉x軸文本值? – Sajeetharan

+0

是的。我正在嘗試旋轉x軸文本值。 – user2786756

+0

請幫忙。我剛試過這個RotateTransform rotateTransform1 = new RotateTransform(-45); OutputChart.RenderTransform = rotateTransform1 ;,但現在旋轉保持圖表: – user2786756

回答

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>