2012-03-02 83 views
1

我有使用动态xtrachart s的报告页面。要动态创建xtrachart,我使用了XtraReportBeforePrint事件。但是生成了重复的XtraChart。从场景中,图表的预期数量为2,但生成了24个图表(创建预期图表的12个重复项)。运行时产生太多XtraChart

这里是我class的报告

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; 
    } 
} 

我没有看到我的代码的任何逻辑上的错误。也许有一些我不明白的行为。希望任何人都可以帮助我。非常感谢。

回答

2

请勿设置XtraReportDataSource。它会复制你的图表。 只设置您的XtraChartDataSource

+0

我从来没有想过设置'XtraReport'的'DataSource'会导致这个问题。像魅力一样工作!非常感谢王子! :) – KaeL 2012-03-05 02:55:04