0

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Bluto
  • 166
  • 1
  • 15

1 Answers1

1

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.

Bluto
  • 166
  • 1
  • 15