Creating reports which contain charts is as simple as creating the Chart(s) and adding them to the Report using the Charts property. A data table should also be created which can be used to group a chart to a specific subset of the reports data. For example in the following code block from the YtoYComparisonReport a data table is created which not only specifies the report and chart ids, but also includes the facility code, meter name and so on. This table is then used when the report is rendered to properly insert the correct chart for a given set of data. In the case of the YtoYComparisonReport the chart is associated with a specific facility code, meter name, meter component name, utility code and billing unit combination.
/// <summary> /// Creates the charts used in the report /// </summary> /// <param name="data">The data table which contains the data needed for chart creation</param> /// <param name="settings">The report settings</param> /// <returns>The data table containing references to the charts</returns> private DataTable CreateCharts(DataTable data, YtoYComparisonSettings settings) { DateTime minYear = DateTime.MaxValue; DateTime maxYear = DateTime.MinValue;
this.Charts.Clear(); DataTable chartTable = new DataTable(""); chartTable.Columns.Add("FacilityCode",typeof(string)); chartTable.Columns.Add("MeterName",typeof(string)); chartTable.Columns.Add("MeterComponentName",typeof(string)); chartTable.Columns.Add("UtilityCode",typeof(string)); chartTable.Columns.Add("BillingUnits",typeof(string)); chartTable.Columns.Add("ReportID",typeof(string)); chartTable.Columns.Add("ChartID",typeof(string)); System.Collections.Hashtable hashtable = new System.Collections.Hashtable(); foreach(DataRow row in data.Rows) { Key key = new Key(row["FacilityCode"].ToString(), row["MeterName"].ToString(), row["MeterComponentName"].ToString(), row["UtilityCode"].ToString(), row["BillingUnits"].ToString()); ManagingEnergy.Reports.Charts.Chart chart = null; // have we already created an item? if(!hashtable.ContainsKey(key)) { chart = base.NewChart(700,400); chart.Plot.XAxisTitle = "Monthly Values"; chart.Plot.YAxisTitle = Convert.ToString(row["UtilityType"])+" ($)";
if(settings.GraphOptions.DisplayXGrid) chart.Plot.XGridColor = Plot.DefaultXGridColor; if(settings.GraphOptions.DisplayYGrid) chart.Plot.YGridColor = Plot.DefaultYGridColor;
this.Charts.Add(chart); hashtable.Add(key,chart); DataRow chartRow = chartTable.NewRow(); chartRow["FacilityCode"] = row["FacilityCode"]; chartRow["MeterName"] = row["MeterName"]; chartRow["MeterComponentName"] = row["MeterComponentName"]; chartRow["UtilityCode"] = row["UtilityCode"]; chartRow["BillingUnits"] = row["BillingUnits"]; chartRow["ReportID"] = this.ID; chartRow["ChartID"] = chart.ID; chartTable.Rows.Add(chartRow); } else chart = hashtable[key] as ManagingEnergy.Reports.Charts.Chart;
DateTime currentRowDate = Convert.ToDateTime(row["StartDate"]).AddDays(1).Date; DateTimeSeries series = chart.Plot.PlotData[currentRowDate.Year.ToString()] as DateTimeSeries; // add the graph data if(series == null) { series = new DateTimeSeries(currentRowDate.Year.ToString(), new LinePlotter(Chart.GetColor(currentRowDate.Year),2.0F,true)); series.DateFormat = "MMM"; chart.Plot.PlotData.Add(series); }
DateTime date = new DateTime(1900,currentRowDate.Month,1); series.Add(new DateTimeSeries.DateTimePoint(date,Convert.ToDouble(row["Quantity"]))); }
// set the chart titles foreach(Chart chart in this.Charts) { chart.Title = string.Format("YTD Comparison"); }
return chartTable; } |
COMPONENTONE SPECIFIC
To show charts within a ComponentOne report, a Sub Report (rsubChartViewer) was created to streamline the process of loading and displaying a chart.