I'm currently migrating a project from .NET framework to .NET 7.
In the project we have a few reports we generate. Each report has a template file (.rfx
) which we fill with data depending on user input.
In the .NET 7 version of FastReport, there is no MSChartObject
. In the C# code I replace the MSChartObject
by the Chart
class from System.Windows.Forms.DataVisualization.Charting
.
There are no more compile errors in the code, BUT there is still a reference to the MSChartObject
in the report template file (.rfx
).
I tried replacing the it with Chart just like in the code, but that doesn't work.
When I open the .rfx
file with the new fast report designer it says it doesn't know the MSChartObject
(as expected) but I couldn't find any other chart object to replace it.
ChartTemplate.rfx
:
<?xml version="1.0" encoding="utf-8"?>
<Report>
<ScriptText>
<!-- bits of code with no reference to the chartObjects -->
<Dictionary>
<!-- a few business objects -->
<MSChartObject Name="MSChart1" Left="727.65" Top="37.8" Width="302.4" Height="368.55">
<MSChartSeries Name="Series1"/>
</MSChartObject>
<MSChartObject Name="MSChart2" Left="9.45" Top="37.8" Width="699.3" Height="189">
<MSChartSeries Name="Series2"/>
</MSChartObject>
<MSChartObject Name="MSChart3" Left="9.45" Top="236.25" Width="699.3" Height="170.1">
<MSChartSeries Name="Series3"/>
</MSChartObject>
</Dictionary>
</Report>
The error happens when I try to load the template file in the code with the FastReport Report
class.
var templateReport = new Report();
templateReport.Load(_TemplatePath);
With what do I replace the MSChartObject
in the .rfx
file?
Or what do I have to do so that it recognizes the System.Windows.Forms.DataVisualization.Charting
Chart
object in the .rfx
file?
I also tried to import the System.Windows.Forms.DataVisualization.Charting
in the <Script>
part of the .rfx
file, but that didn't change anything.
After much searching and trial and error i found a solution for my problem. In the .Net 7 version of FastReport there is no more MSChartObject, so we have to remove it from the report templates (.rfx file). Then we have to replace the MSChartObjects in the .Net code.
My first Solution was half right, don't use the System.Windows.Forms.DataVisualization.Charting
Chart
, use the namespace FastReport.DataVisualization.Charting
.
Replace all object the chart needs( series, legend, chartarea, ...) by the same classes from this namespace FastReport.DataVisualization.Charting
.
I didn't find a way to integrate the charts directly into a FastReport ReportPage, so i used the this workaround.
In the chart object is the chart i created, convert it to a image, then add in to the report page by setting its parent to the DataBand of the page. Set the position of the picture by setting a Bounds Rectangle.
var stream = new MemoryStream();
chart.SaveImage(stream, FastReport.DataVisualization.Charting.ChartImageFormat.Bmp);
var pictureObject = new PictureObject() { Image = new Bitmap(stream) };
pictureObject.Parent = dataBand1;
pictureObject.Bounds = new RectangleF((int)Math.Ceiling(Units.Centimeters * 17.0f), 0, chart.Width, chart.Height);
I hope this helps someone in the Future. I found almost no information on the internet about this.