Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:50:14+00:00 2026-06-13T21:50:14+00:00

I am trying to print a chart generated during a report. I am able

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:50:16+00:00Added an answer on June 13, 2026 at 9:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 145064642 chars long HTML String which I am trying to print. When
Im trying print Excel file data on a page. To do it i used
Im trying to print the realtime output based on user input for a search.
I'm trying to print a number of '+' characters followed by a number of
I'm trying to print a large string to alivepdf. I'm splitting the string by
I'm trying to print out a list of products on a page. This is
I am trying to print HTML with this JavaScript in the webview, however it
I am trying to print out true if there is such letter/word and false
I am trying to print line[4] if there are 4 items, or line [4]
I am trying to print a particular text using awk if string is empty.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.