I want Export a chart in pdf file and image file. I use MVVM. I define a cartesian chart in view model and export that. but is some problem in export.
My chart is like "Ui.png" and in xaml that work correct.
* But in pdf file ("Export.pdf"):
1- label of Axis overwrite on corner.
2- some of curve have dasharray (you can see in "Ui.png") but dosen't set.
3- I define a Plot band Annotation (you can see in "Ui.png") but dosen't set.
* And in png file ("Export.png") only show Axis label.
My code for export
--------------------------------------------------------------------------------------------------------
RadFixedDocument document = new RadFixedDocument();
Chart = CreateChart();
document.Pages.Add(CreateChartPdfPart());
PdfFormatProvider provider = new PdfFormatProvider();
provider.Export(document, fileStream);
--------------------------------------------------------------------------------------------------------
public RadCartesianChart CreateChart()
{
RadCartesianChart Chart = new RadCartesianChart();
DoubleCollection dashArray = new DoubleCollection { 10, 1, 1, 1 };
FontFamily fontFamily = new FontFamily(Properties.Settings.Default.FontFamily);
Chart.BeginInit();
DateTimeContinuousAxis AxisX = new DateTimeContinuousAxis();
AxisX.PlotMode = AxisPlotMode.OnTicks;
AxisX.LabelFitMode = AxisLabelFitMode.Rotate;
AxisX.ShowLabels = true;
Chart.HorizontalAxis = AxisX;
LinearAxis LengthAxis = new LinearAxis();
LengthAxis.HorizontalLocation = AxisHorizontalLocation.Right;
LengthAxis.LineDashArray = dashArray;
TextBlock txt = new TextBlock();
var RotateLabel = new Style(typeof(TextBlock));
Setter setter = new Setter(TextBlock.RenderTransformProperty, new RotateTransform() { Angle = 180, CenterX = 50, CenterY = 50 });
RotateLabel.Setters.Add(setter);
txt.Style = RotateLabel;
txt.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "Difference (cm)");
LengthAxis.Title = txt.Text;
var ColorLabel = new Style(typeof(TextBlock));
Setter sett = new Setter(TextBlock.ForegroundProperty, new SolidColorBrush(Colors.Black));
LengthAxis.LabelStyle = ColorLabel;
LengthAxis.ShowLabels = true;
LinearAxis PercentageAxis = new LinearAxis();
PercentageAxis.HorizontalLocation = AxisHorizontalLocation.Left;
PercentageAxis.Title = string.Format(CultureInfo.CreateSpecificCulture("en-US"), ""); //"Difference (%)"
PercentageAxis.ShowLabels = true;
Chart.VerticalAxis = PercentageAxis;
Chart.Width = sizeA4.Width - 40;
Chart.Height = (sizeA4.Width - 40) * (2.0 / 3.0);
CartesianPlotBandAnnotation BandAnnotation = new CartesianPlotBandAnnotation();
BandAnnotation.Axis = PercentageAxis;
BandAnnotation.From = DownWarn;
BandAnnotation.To = UpWarn;
BandAnnotation.BorderBrush = new SolidColorBrush(Colors.Green);
BandAnnotation.Fill = new SolidColorBrush(Colors.Green);
Chart.Annotations.Add(BandAnnotation);
CartesianGridLineAnnotation GridLineAnnotationMinFail = new CartesianGridLineAnnotation();
GridLineAnnotationMinFail.Axis = PercentageAxis;
GridLineAnnotationMinFail.Value = DownFailure;
GridLineAnnotationMinFail.Stroke = new SolidColorBrush(Colors.Red);
GridLineAnnotationMinFail.DashArray = new DoubleCollection { 5, 5 };
GridLineAnnotationMinFail.StrokeThickness = 0.5;
Chart.Annotations.Add(GridLineAnnotationMinFail);
CartesianGridLineAnnotation GridLineAnnotationMaxFail = new CartesianGridLineAnnotation();
GridLineAnnotationMaxFail.Axis = PercentageAxis;
GridLineAnnotationMaxFail.Value = UpFailure;
GridLineAnnotationMaxFail.Stroke = new SolidColorBrush(Colors.Red);
GridLineAnnotationMaxFail.DashArray = new DoubleCollection { 5, 5 };
GridLineAnnotationMaxFail.StrokeThickness = 0.5;
Chart.Annotations.Add(GridLineAnnotationMaxFail);
CartesianChartGrid ChartGrid = new CartesianChartGrid();
ChartGrid.MajorLinesVisibility = GridLineVisibility.X;
ChartGrid.StripLinesVisibility = GridLineVisibility.Y;
ChartGrid.IsTabStop = false;
ChartGrid.YStripeBrushes.Add(new SolidColorBrush(Colors.Gray));
Chart.Grid = ChartGrid;
int i = 0;
foreach (var data in Data)
{
LineSeries lineSeries;
if (chekboxSeries[i])
{
if (i == 0 || i == 1 || i == 2 || i == 7 || i == 8)
{
lineSeries = new LineSeries
{
Stroke = new SolidColorBrush(newColor[i]),
StrokeThickness = 0.5,
DashArray = dashArray
};
}
else
{
lineSeries = new LineSeries
{
Stroke = new SolidColorBrush(newColor[i]),
VerticalAxis = LengthAxis,
DashArray = dashArray,
StrokeThickness = 0.5
};
}
foreach (var item in data.Items)
{
lineSeries.DataPoints.Add(new CategoricalDataPoint { Category = item.Date, Value = item.Value });
}
Chart.Series.Add(lineSeries);
}
i++;
}
Chart.EndInit();
Chart.Measure(new Size(Chart.Width, Chart.Height));
Chart.Arrange(new Rect(new Point(0, 0), Chart.DesiredSize));
return Chart;
}
--------------------------------------------------------------------------------------------------------
private RadFixedPage CreateChartPdfPart()
{
int margin = 20;
var page = new RadFixedPage();
page.Size = sizeA4;
var editor = new FixedContentEditor(page, Telerik.Windows.Documents.Fixed.Model.Data.MatrixPosition.Default);
using (editor.SavePosition())
{
editor.Position.Translate(margin, margin);
ExportUIElement.ExportHelper.ExportToPdf(Chart, editor);
}
return page;
}
--------------------------------------------------------------------------------------------------------
private void ExportPNGToImage(FrameworkElement element, Stream stream)
{
Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(element, stream, new PngBitmapEncoder());
}
What is wrong?