复制/克隆和打印图表。问题与ActualWidth& ActualHeight [英] Copy/Clone and Print Chart. Problems with ActualWidth & ActualHeight

查看:210
本文介绍了复制/克隆和打印图表。问题与ActualWidth& ActualHeight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印在报表中生成的图表。我可以把图表放在DocumentPaginator文档,但我无法调整图表的大小以适应页面。我注意到,如果我改变报告程序的大小,这将改变图表的大小会影响图表的缩放。这个实现告诉我,图表的 ActualWidth ActualHeight 直接链接到缩放。



我试过:

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

测量(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); 会花费太长时间,并且会导致stackoverflow。 / p>

我使用

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

来创建我的副本,但ActualWidth和ActualHeight遇到'0'



我已经使用

调整图表大小。

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

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

或者说我的图表的ActualWidth和ActualHeight更新为我想要的大小,



那么,如何打印一个图表,使其适当地缩放到所选的纸张尺寸?

解决方案

所以我发现了一些对我有用的东西。我不觉得这是最干净的方式。



由于我需要缩放图表,我想打印我需要复制/克隆图表。



我使用 myNewChart = new Chart(){DataContext = myOldChart.DataContext} >

我为我的项目添加了一个新窗口,并在其中呈现了我的新图表,所以我没有从它的黑色图像。



ConvertingWindow.xaml代码。

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

< Grid>
< Grid.Resources>
<! - CutomColumnStyle样式 - >
< Style x:Key =CutomColumnStyleTargetType =cht:ColumnDataPoint>
<! - 背景颜色 - >
< Setter Property =BackgroundValue ={Binding barColor}/>

<! - 注释或列值标签 - >
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType =cht:ColumnDataPoint>
< Grid>
< Rectangle Fill ={TemplateBinding Background}Stroke =Black/>
< Grid Margin =0 -20 0 0Horizo​​ntalAlignment =CenterVerticalAlignment =Top>
< TextBlock Text ={TemplateBinding FormattedDependentValue}FontWeight =BoldMargin =2>
< TextBlock.RenderTransform>
< RotateTransform Angle = - 60/>
< /TextBlock.RenderTransform>
< / TextBlock>
< / Grid>
< / Grid>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /Grid.Resources>

<! - 图表图表 - >
< cht:Chart x:Name =UI_ChartTitle ={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>



  private void GetChartVisual()
{
//初始化变量
cw = new ConvertingWindow();

cw.UI_Chart.DataContext = myNewChart.DataContext;

//将MainWindow设置为此窗口的所有者
cw.Owner = Application.Current.MainWindow;
//将DataContext设置为此DataContext
//允许与已加载的变量绑定
cw.DataContext = this;

cw.Show();

myNewChart = cw.UI_Chart;

cw.Close();
}

通过这样做,它呈现我的视觉效果。然后我可以调整它的大小,我想要的,不会影响我的原始图表。不是最漂亮的东西,但它的工作原理。


I am trying to print a chart generated during a report. I am able to put the chart on a DocumentPaginator document, but I am having trouble resizing the chart to fit the page. I noticed that if I changed the size of the reporting program which would change the Charts size would affect the scaling of the Chart. This realization showed me that the Chart's ActualWidth and ActualHeight were directly linked to the scaling.

I tried:

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

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

But this caused my visual Chart in the reporting program to resize and the printable chart wouldn't resize to the new size until the second print.

Realizing that myChart was connected to reportChart I tried to copy/clone reportChart to myChart.

I tried:

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;
    }

}

or

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

but string xaml = XamlWriter.Save(element); would take too long and both would cause a stackoverflow.

I am using

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

to make my copy, but ActualWidth and ActualHeight come across '0' so I can't tell if the Chart's DataContext copied correctly.

I did get my Chart to resize using

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

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

or to say my Chart's ActualWidth and ActualHeight to update to the size I want, but I am getting a black image on my document where the chart should be.

So how do I print a chart with it properly scaled to a selected paper size?

解决方案

So I found something that works for me. I don't feel it is the cleanest way to do it.

Since I need to scale the Chart I am trying to print I need to copy/clone the Chart.

I used myNewChart = new Chart() { DataContext = myOldChart.DataContext } like stated before.

I added a new window to my project, and rendered my new chart in there so I don't get a black image from it.

ConvertingWindow.xaml Code. This code matches my orignal Chart's code.

<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>

Then in my VM I call.

    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();
    }

By doing this it renders my visual. I then can resize it to what I want and not affect my original Chart. Not the prettiest thing, but it works.

这篇关于复制/克隆和打印图表。问题与ActualWidth&amp; ActualHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆