In memory chart doesn't render.
Hi we aim print some graph and we thought to use SFCharts, but the plots are always empty, what's wrong?
this is the code:
private SfChart DrawGraph(ObservableCollection<Sample> data, double start, double width, double? min, double? max)
{
var graph = new SfChart
{
Margin = new Thickness(10),
AreaBorderThickness = new Thickness(0, 1, 1, 1),
Background = Brushes.Black,
FontFamily = new FontFamily("Courier New")
};
var gridLineStyle = new System.Windows.Style
{
TargetType = typeof(Line),
Setters = { new Setter { Property = Line.StrokeProperty, Value = Brushes.DimGray } }
};
graph.PrimaryAxis = new CategoryAxis
{
MajorGridLineStyle = gridLineStyle,
MinorGridLineStyle = gridLineStyle,
Header = "Space",
LabelFormat = "0.00",
FontSize = 10,
Foreground = Brushes.LimeGreen,
FontWeight = FontWeights.Normal,
EdgeLabelsDrawingMode = EdgeLabelsDrawingMode.Fit
};
graph.SecondaryAxis = new NumericalAxis
{
MajorGridLineStyle = gridLineStyle,
MinorGridLineStyle = gridLineStyle,
Header = "Value",
Foreground = Brushes.LimeGreen,
FontWeight = FontWeights.Normal,
StripLines = new ChartStripLines(),
Minimum = min,
Maximum = max,
LabelFormat = "0.00",
Interval = (max - min) / 7
};
graph.SecondaryAxis.StripLines.Add(new ChartStripLine
{
Start = start,
Width = width,
Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#8890ee90"),
BorderBrush = Brushes.GreenYellow,
BorderThickness = new Thickness(1.5)
});
var line = new FastLineBitmapSeries
{
Interior = Brushes.Yellow,
StrokeThickness = 0.5,
ItemsSource = data,
XBindingPath = "Space2",
YBindingPath = "Value",
Trendlines = new ChartTrendLineCollection()
};
graph.Series.Add(line);
return graph;
}
private byte[] GraphToByteArray(SfChart graph, double printWidth, double printHeight)
{
Size size = new Size(printWidth, printHeight);
graph.Measure(new Size(printWidth / 2, printHeight / 2));
graph.Arrange(new Rect(new Size(printWidth / 2, printHeight / 2)));
RenderTargetBitmap renderBitmap =
new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
dpi,
dpi,
PixelFormats.Pbgra32);
renderBitmap.Render(graph);
byte[] ret = null;
using (MemoryStream outStream = new MemoryStream())
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(outStream);
//graph.Save(outStream, encoder);
ret = outStream.ToArray();
}
return ret;
}
This is the chart in a xaml window

While this is the chart printed from c# code

We have used the same parameters from the xaml page and the same data is binded to the FastLineBitmapSeries, but as you can see the line does not appear.
this is the code:
private SfChart DrawGraph(ObservableCollection<Sample> data, double start, double width, double? min, double? max)
{
var graph = new SfChart
{
Margin = new Thickness(10),
AreaBorderThickness = new Thickness(0, 1, 1, 1),
Background = Brushes.Black,
FontFamily = new FontFamily("Courier New")
};
var gridLineStyle = new System.Windows.Style
{
TargetType = typeof(Line),
Setters = { new Setter { Property = Line.StrokeProperty, Value = Brushes.DimGray } }
};
graph.PrimaryAxis = new CategoryAxis
{
MajorGridLineStyle = gridLineStyle,
MinorGridLineStyle = gridLineStyle,
Header = "Space",
LabelFormat = "0.00",
FontSize = 10,
Foreground = Brushes.LimeGreen,
FontWeight = FontWeights.Normal,
EdgeLabelsDrawingMode = EdgeLabelsDrawingMode.Fit
};
graph.SecondaryAxis = new NumericalAxis
{
MajorGridLineStyle = gridLineStyle,
MinorGridLineStyle = gridLineStyle,
Header = "Value",
Foreground = Brushes.LimeGreen,
FontWeight = FontWeights.Normal,
StripLines = new ChartStripLines(),
Minimum = min,
Maximum = max,
LabelFormat = "0.00",
Interval = (max - min) / 7
};
graph.SecondaryAxis.StripLines.Add(new ChartStripLine
{
Start = start,
Width = width,
Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#8890ee90"),
BorderBrush = Brushes.GreenYellow,
BorderThickness = new Thickness(1.5)
});
var line = new FastLineBitmapSeries
{
Interior = Brushes.Yellow,
StrokeThickness = 0.5,
ItemsSource = data,
XBindingPath = "Space2",
YBindingPath = "Value",
Trendlines = new ChartTrendLineCollection()
};
graph.Series.Add(line);
return graph;
}
private byte[] GraphToByteArray(SfChart graph, double printWidth, double printHeight)
{
Size size = new Size(printWidth, printHeight);
graph.Measure(new Size(printWidth / 2, printHeight / 2));
graph.Arrange(new Rect(new Size(printWidth / 2, printHeight / 2)));
RenderTargetBitmap renderBitmap =
new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
dpi,
dpi,
PixelFormats.Pbgra32);
renderBitmap.Render(graph);
byte[] ret = null;
using (MemoryStream outStream = new MemoryStream())
{
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
encoder.Save(outStream);
//graph.Save(outStream, encoder);
ret = outStream.ToArray();
}
return ret;
}
This is the chart in a xaml window
While this is the chart printed from c# code
We have used the same parameters from the xaml page and the same data is binded to the FastLineBitmapSeries, but as you can see the line does not appear.
SIGN IN To post a reply.
2 Replies
ST
Stefano Tessarin
May 27, 2016 06:45 AM UTC
We were able to solve it by simply use PngBitmapEncoder instead of BmpBitmapEncoder.
DA
Devi Aruna Maharasi Murugan
Syncfusion Team
May 27, 2016 12:04 PM UTC
Hi Stefano,
Thanks for contacting Syncfusion Support.
We are unable to reproduce the problem while save the chart using your code. We have prepared the sample based on your requirement and please find the sample in the below location.
Sample: WpfApplication-Image4.5.1
Also, by default SfChart having Print support. Please find the below documents for print.
Regards,
Devi M.
SIGN IN To post a reply.
- 2 Replies
- 2 Participants
-
ST Stefano Tessarin
- May 26, 2016 04:05 PM UTC
- May 27, 2016 12:04 PM UTC