Question

I have report page that uses dynamic xtracharts. To dynamically create the xtracharts, I used the XtraReport's BeforePrint event. But there are duplicate XtraCharts generated. From a scenario, the expected quantity of charts are 2, but there are 24 charts generated (creating 12 duplicates of the expected charts).

Here is my class for the report

public class SystemReport : DevExpress.XtraReports.UI.XtraReport
{
    private DevExpress.XtraReports.UI.DetailBand Detail;
    private DevExpress.XtraReports.UI.PageFooterBand PageFooter;
    private ReportHeaderBand ReportHeader;
    ...

    public SystemReport(List<SystemLog> logs)
    {
        InitializeComponent();
        Detail.Report.DataSource = logs
    }

    protected override void Dispose(bool disposing)
    {
        ...
    }

    #region Designer generated code

    private void InitializeComponent()
    {
        this.Detail = new DevExpress.XtraReports.UI.DetailBand();
        this.PageFooter = new DevExpress.XtraReports.UI.PageFooterBand();
        this.xrPageInfo1 = new DevExpress.XtraReports.UI.XRPageInfo();
        this.ReportHeader = new DevExpress.XtraReports.UI.ReportHeaderBand();
        this.xrLblSystemReport = new DevExpress.XtraReports.UI.XRLabel();
        ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
        // 
        // Detail
        // 
        this.Detail.Name = "Detail";
        this.Detail.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.Detail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        // 
        // PageFooter
        // 
        this.PageFooter.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
        this.xrPageInfo1});
        this.PageFooter.HeightF = 25.83332F;
        this.PageFooter.Name = "PageFooter";
        this.PageFooter.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.PageFooter.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        // 
        // xrPageInfo1
        // 
        this.xrPageInfo1.ForeColor = System.Drawing.Color.Gray;
        this.xrPageInfo1.Format = "Page {0} of {1}";
        this.xrPageInfo1.LocationFloat = new DevExpress.Utils.PointFloat(388.7917F, 0F);
        this.xrPageInfo1.Name = "xrPageInfo1";
        this.xrPageInfo1.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
        this.xrPageInfo1.SizeF = new System.Drawing.SizeF(120F, 25F);
        this.xrPageInfo1.StylePriority.UseForeColor = false;
        this.xrPageInfo1.StylePriority.UseTextAlignment = false;
        this.xrPageInfo1.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
        // 
        // ReportHeader
        // 
        this.ReportHeader.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
        this.xrLblSystemReport});
        this.ReportHeader.HeightF = 107.0417F;
        this.ReportHeader.Name = "ReportHeader";
        this.ReportHeader.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F);
        this.ReportHeader.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        // 
        // xrLblSystemReport
        // 
        this.xrLblSystemReport.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold);
        this.xrLblSystemReport.LocationFloat = new DevExpress.Utils.PointFloat(363.3333F, 10.00001F);
        this.xrLblSystemReport.Name = "xrLblSystemReport";
        this.xrLblSystemReport.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F);
        this.xrLblSystemReport.SizeF = new System.Drawing.SizeF(175F, 25F);
        this.xrLblSystemReport.Text = "System Report";
        this.xrLblSystemReport.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopCenter;
        // 
        // SystemReport
        // 
        this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] {
        this.Detail,
        this.PageFooter,
        this.ReportHeader});
        this.Font = new System.Drawing.Font("Arial", 9.75F);
        this.ForeColor = System.Drawing.Color.Gray;
        this.Landscape = true;
        this.PageHeight = 850;
        this.PageWidth = 1100;
        this.Version = "11.2";
        this.BeforePrint += new System.Drawing.Printing.PrintEventHandler(Report_BeforePrint);
        ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
    }

    private void Report_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
        DetailBand detail = (DetailBand)((XtraReport)sender).Bands[BandKind.Detail];
        List<SystemLog> logs = (List<SystemLog>)detail.Report.DataSource;
        int chartQuantity = GetChartQuantity(logs);

        PopulateDetailWithCharts(detail, chartQuantity);
        PopulateDetailWithValues(logs, detail, chartQuantity);
    }

    #endregion

    private int GetChartQuantity(List<SystemLog> logs)
    {
        return logs.Count / 15 + (logs.Count % 15 > decimal.Zero ? 1 : 0);
    }

    private void PopulateDetailWithCharts(DetailBand detail, int chartQuantity)
    {
        for (int i = 0; i < chartQuantity; i++)
        {
            if (detail.FindControl(string.Format("xrChart{0}", i), false) == null)
            {
                XRChart chart = GetChart(i);
                detail.Controls.Add(chart);
                chart.Location = new Point(0, 300 * (i));
            }
        }
    }

    private XRChart GetChart(int i)
    {
        XRChart chart = new XRChart();

        chart.AppearanceNameSerializable = "Gray";
        chart.BackColor = System.Drawing.Color.Transparent;
        chart.BorderColor = System.Drawing.SystemColors.ControlText;
        chart.Borders = DevExpress.XtraPrinting.BorderSide.None;
        chart.ImageType = DevExpress.XtraReports.UI.ChartImageType.Bitmap;
        chart.Name = string.Format("xrChart{0}", i);
        chart.PaletteBaseColorNumber = 11;
        chart.PaletteName = "Nature Colors";
        chart.SizeF = new System.Drawing.SizeF(852.4167F, 300F);
        chart.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft;
        chart.Legend.Visible = false;

        return chart;
    }

    private void PopulateDetailWithValues(List<SystemLog> logs, DetailBand detail, int chartQuantity)
    {            
        for (int i = 0; i < chartQuantity; i++)
        {
            XRChart chart = (XRChart)detail.FindControl(string.Format("xrChart{0}", i), false);
            chart.DataSource = logs.SkipWhile((log, index) => index < i * 15).TakeWhile((log, index) => index < i + 15).ToList();
            InitializeChartValues(chart);
        }
    }

    private void InitializeChartValues(XRChart chart)
    {
        Series salesSeries = new Series();
        salesSeries.ArgumentDataMember = "DateTime";
        salesSeries.ValueDataMembers.AddRange(new string[] { "Quantity" });
        salesSeries.Name = "System Log Quantity";

        chart.Series.AddRange(new Series[] { salesSeries });

        XYDiagram diagram = (XYDiagram)chart.Diagram;
        diagram.AxisY.Title.Text = "Quantity of System Logs";
        diagram.AxisX.Title.Text = "Date of System Logs";
        diagram.AxisY.Title.Visible = true;
        diagram.AxisX.Title.Visible = true;
        diagram.AxisX.Label.Angle = -30;
    }
}

I do not see any logical error on my codes. Maybe there are some behaviors that I do not understand. Hope anyone can help me with this. Thanks a lot.

Was it helpful?

Solution

Do not set the DataSource of the XtraReport. It will duplicate your chart. Set only the DataSource of your XtraChart.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top