2012-11-01 2 views
0

보고서 중에 생성 된 차트를 인쇄하려고합니다. 차트를 DocumentPaginator 문서에 넣을 수는 있지만 페이지 크기에 맞게 차트의 크기를 조정하는 데 문제가 있습니다. 차트 크기를 변경하는보고 프로그램의 크기를 변경하면 차트의 크기 조정에 영향을 미친다는 것을 알았습니다. 이 실현은 차트의 ActualWidthActualHeight이 스케일링과 직접적으로 관련되어 있음을 보여주었습니다.차트 복사/복사 및 인쇄. ActualWidth & ActualHeight의 문제점

myChart.Width = newWidth; 
myChart.Height = newHeight; 

Measure(myChart.RenderSize); 
Arrange(new Rect(0, 0, newWidth, newHeight)); 

을하지만이보고 프로그램에서 내 비주얼 차트의 크기를 조정하고 인쇄 할 수있는 차트가 두 번째가 인쇄 될 때까지 새로운 크기로 크기를 조정하지 않을 발생 :

나는 시도했다.

myChart가 reportChart에 연결되었음을 알았습니다. reportChart를 myChart에 복사/복제하려했습니다.

내가 시도 :

public class Copy<T> 
{ 
    public static T DeepCopy<T>(T element) 
    { 
     string xaml = XamlWriter.Save(element); 
     StringReader xamlString = new StringReader(xaml); 
     XmlTextReader xmlTextReader = new XmlTextReader(xamlString); 
     var DeepCopyobject = (T)XamlReader.Load(xmlTextReader); 
     return DeepCopyobject; 
    } 

} 

또는

myChart = XamlReader.Parse(XamlWriter.Save(reportChart.DataContext)) as Chart 

하지만 string xaml = XamlWriter.Save(element);이 너무 오래 걸릴 것 모두가 유래을 일으킬 것입니다.

내 복사본을 만들

myChart = new Chart() { DataContext = reportChart.DataContext } 

를 사용하고 있지만, ActualWidth와의 ActualHeight는 건너 '0'차트의 DataContext를 올바르게 복사 한 경우 그래서 나는 말할 수 없다.

내가 원하는 크기로 업데이트 내 차트의 ActualWidth과의 ActualHeight 말을

myChart.Width = newWidth; 
myChart.Height = newHeight; 

myChart.Measure(new System.Windows.Size(newWidth, newHeight)); 
myChart.Arrange(new Rect(0, 0, newWidth, newHeight)); 

또는를 사용하여 크기를 조정할 내 차트를 얻을 않았지만, 차트가 있어야 할 곳에 내 문서에 검은 이미지를 얻고

.

그럼 선택한 용지 크기에 맞도록 차트를 올바르게 인쇄하려면 어떻게합니까?

답변

1

그래서 나는 저에게 효과가있는 것을 발견했습니다. 나는 그것을하는 가장 깨끗한 방법이라고 생각하지 않는다.

차트를 확장해야하므로 인쇄하려고하는데 차트를 복사/복제해야합니다.

나는 전에 언급 한 바와 같이 myNewChart = new Chart() { DataContext = myOldChart.DataContext }을 사용했습니다.

프로젝트에 새 창을 추가하고 거기에 새 차트를 렌더링하여 블랙 이미지를 얻지 못했습니다.

ConvertingWindow.xaml 코드. 이 코드는 내 orignal 차트의 코드와 일치합니다.

<Window x:Class="QScanReportPrinting.ConvertingWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="ConvertingWindow" 
    xmlns:cht="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"> 

<Grid> 
    <Grid.Resources> 
     <!-- CutomColumnStyle Style --> 
     <Style x:Key="CutomColumnStyle" TargetType="cht:ColumnDataPoint"> 
      <!--Background Color--> 
      <Setter Property="Background" Value="{Binding barColor}"/> 

      <!--Annotations, or column value labels--> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="cht:ColumnDataPoint"> 
         <Grid> 
          <Rectangle Fill="{TemplateBinding Background}" Stroke="Black"/> 
          <Grid Margin="0 -20 0 0" HorizontalAlignment="Center" VerticalAlignment="Top"> 
           <TextBlock Text="{TemplateBinding FormattedDependentValue}" FontWeight="Bold" Margin="2"> 
            <TextBlock.RenderTransform> 
             <RotateTransform Angle="-60" /> 
            </TextBlock.RenderTransform> 
           </TextBlock> 
          </Grid> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 

    <!--Chart for Graph--> 
    <cht:Chart x:Name="UI_Chart" Title="{Binding GraphTitle}" Background="White"> 
     <cht:Chart.Series> 
      <cht:ColumnSeries Title="{Binding ChartKey}" ItemsSource="{Binding GraphDataCollection}" IndependentValueBinding="{Binding Path=X}" DependentValueBinding="{Binding Path=Y}" 
           DataPointStyle="{StaticResource CutomColumnStyle}"> 
       <cht:ColumnSeries.IndependentAxis> 
        <cht:CategoryAxis Orientation="X"> 
         <cht:CategoryAxis.AxisLabelStyle> 
          <Style TargetType="cht:AxisLabel"> 
           <Setter Property="Template"> 
            <Setter.Value> 
             <ControlTemplate TargetType="cht:AxisLabel"> 
              <TextBlock Text="{TemplateBinding FormattedContent}"> 
              <TextBlock.LayoutTransform> 
               <RotateTransform Angle="-60"/> 
              </TextBlock.LayoutTransform> 
              </TextBlock> 
             </ControlTemplate> 
            </Setter.Value> 
           </Setter> 
          </Style> 
         </cht:CategoryAxis.AxisLabelStyle> 
        </cht:CategoryAxis> 
       </cht:ColumnSeries.IndependentAxis> 
      </cht:ColumnSeries> 
     </cht:Chart.Series> 
    </cht:Chart> 
</Grid> 
내 VM I 전화에서 다음

.

private void GetChartVisual() 
    { 
     // Initialize variable 
     cw = new ConvertingWindow(); 

     cw.UI_Chart.DataContext = myNewChart.DataContext; 

     // Set MainWindow to be the owner of this window 
     cw.Owner = Application.Current.MainWindow; 
     // Set DataContext to this DataContext 
     // Allows binding with variables already loaded 
     cw.DataContext = this; 

     cw.Show(); 

     myNewChart = cw.UI_Chart; 

     cw.Close(); 
    } 

이렇게하면 시각적으로 표현됩니다. 그런 다음 원래 크기의 차트에 영향을주지 않고 원하는대로 크기를 조정할 수 있습니다. 가장 예쁜 것은 아니지만 작동합니다.