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.