首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >复制/克隆和打印图表。ActualWidth和ActualHeight的问题

复制/克隆和打印图表。ActualWidth和ActualHeight的问题
EN

Stack Overflow用户
提问于 2012-11-01 16:10:48
回答 1查看 682关注 0票数 0

我正在尝试打印报告过程中生成的图表。我可以将图表放在DocumentPaginator文档上,但在调整图表大小以适应页面时遇到了问题。我注意到,如果我改变报告程序的大小,这将改变图表的大小,这将影响图表的缩放。这让我意识到图表的ActualWidthActualHeight与缩放是直接相关的。

我试过了:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
myChart.Width = newWidth;
myChart.Height = newHeight;

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

但这会导致报表程序中的visual Chart调整大小,并且在第二次打印之前,可打印图表不会调整到新的大小。

意识到myChart连接到reportChart后,我尝试将reportChart复制/克隆到myChart。

我试过了:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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;
    }

}

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
myChart = XamlReader.Parse(XamlWriter.Save(reportChart.DataContext)) as Chart

但是string xaml = XamlWriter.Save(element);会占用太长时间,而且两者都会导致堆栈溢出。

我正在使用

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
myChart = new Chart() { DataContext = reportChart.DataContext }

来制作我的副本,但是ActualWidth和ActualHeight遇到了'0‘,所以我不能判断图表的DataContext复制是否正确。

我确实使用以下命令调整了图表的大小

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
myChart.Width = newWidth;
myChart.Height = newHeight;

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

或者说我的图表的ActualWidth和ActualHeight更新为我想要的大小,但我在我的文档中图表应该在的地方得到了一个黑色的图像。

那么,如何将图表正确缩放到选定的纸张大小来打印图表呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-05 16:29:38

所以我找到了对我有用的东西。我觉得这不是最干净的方式。

由于我需要缩放正在尝试打印的图表,因此需要复制/克隆该图表。

我像前面说过的那样使用了myNewChart = new Chart() { DataContext = myOldChart.DataContext }

我在我的项目中添加了一个新窗口,并在其中呈现了我的新图表,这样我就不会得到黑色的图像。

ConvertingWindow.xaml代码。此代码与我的原始图表的代码相匹配。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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中调用。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    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();
    }

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13181066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文