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?