using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Telerik.ReportViewer.Silverlight;
using Telerik.Reporting.Service.SilverlightClient;
using System.ServiceModel;
using Telerik.Reporting.Service;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using System.Windows.Printing;
using System.Windows.Media.Imaging;
using System.Windows.Markup;
using System.Collections.ObjectModel;
namespace ReportsSample
{
public partial class MainPage : UserControl, IReportServiceClientFactory
{
public MainPage()
{
InitializeComponent();
this.rptViewer.ReportServiceClientFactory = this;
this.rptViewer.RenderBegin += new RenderBeginEventHandler(rptViewer_RenderBegin);
}
ReportViewerModel viewerModel;
ReportViewerModel ViewerModel
{
get
{
if (viewerModel == null && VisualTreeHelper.GetChildrenCount(this.rptViewer) > 0)
{
var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(this.rptViewer, 0);
viewerModel = (ReportViewerModel)layoutRoot.DataContext;
}
return viewerModel;
}
}
void rptViewer_RenderBegin(object sender, RenderBeginEventArgs args)
{
this.rptViewer.RenderBegin -= this.rptViewer_RenderBegin;
this.ViewerModel.PropertyChanged += this.ViewerModel_PropertyChanged;
}
void ViewerModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
//if (e.PropertyName == "State" && IsReadyToPrint())
//{
// this.ViewerModel.PropertyChanged -= this.ViewerModel_PropertyChanged;
// this.PrintReport();
//}
}
bool IsReadyToPrint()
{
return this.ViewerModel.State == ReportViewerStates.ViewerPage;
}
void PrintReport()
{
this.viewerModel.PrintReportCommand.Execute(null);
}
#region IReportServiceClientFactory Members
ReportServiceClient IReportServiceClientFactory.Create(System.Uri remoteAddress)
{
// remoteAddress = new Uri("../Reports/ReportService.svc", UriKind.RelativeOrAbsolute);
var binding = new BasicHttpBinding()
{
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
ReceiveTimeout = new TimeSpan(0, 15, 0),
SendTimeout = new TimeSpan(0, 15, 0)
};
var endpointAddress = new EndpointAddress(remoteAddress);
return new ReportServiceClient(binding, endpointAddress);
}
#endregion
Collection<Canvas> pagesList;
string instanceID;
private void btnPritNew_Click(object sender, RoutedEventArgs e)
{
try
{
this.ViewerModel.PropertyChanged -= this.ViewerModel_PropertyChanged;
this.PrintReport();
}
catch(Exception ex)
{
}
}
}
}
public class ReportSettingsMaster
{
public int CodeId { get; set; }
public string CodeName { get; set; }
public string CodeKey { get; set; }
}
#region [Assemblies References]
using System;
using Telerik.Reporting;
using Telerik.Reporting.Drawing;
using System.Data;
using System.Collections.Generic;
using Telerik.Reporting.Charting;
using Telerik.Reporting.Charting.Styles;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using System.Drawing;
using System.Linq;
#endregion
namespace Telstrat.Reports
{
/// <summary>
/// Summary description for Report1.
/// </summary>
public partial class SampleEvaluationsReport : Telerik.Reporting.Report
{
DataTable dtResults = new DataTable();
#region [Constructor]
public SampleEvaluationsReport()
{
//Providing the DataSource event for the report
InitializeComponent();
for (int i = 0; i < 15; i++)
{
dtResults.Columns.Add(" Column " + i.ToString(), i == 0 ? typeof(string) : typeof(int));
}
for (int i = 0; i < 70; i++)
{
Random r = new Random();
dtResults.Rows.Add(new object[] { "Chart Series "+i.ToString(),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100),
r.Next(100) }
);
}
generateChart(dtResults,1,2);
}
#endregion
#region [generateChart]
private float _standardHeight = 11.7f;
private double _reportMargin = 0;
private int formatedLabelmaxlength = 0;
private int formatedLabelmaxdepth = 0;
private string formatTextWrap(string sourceText)
{
int divider = 80;
string[] sourceTextlist = sourceText.Split(' ');
int lineCount = 0;
string strResultText = string.Empty;
string strLine = string.Empty;
foreach (string strWord in sourceTextlist)
{
if (strLine.Length < divider)
{
strLine += strWord + " ";
formatedLabelmaxlength = formatedLabelmaxlength < strLine.Length ? strLine.Length : formatedLabelmaxlength;
}
else
{
formatedLabelmaxlength = formatedLabelmaxlength < strLine.Length ? strLine.Length : formatedLabelmaxlength;
lineCount++;
strResultText += strLine + "\n";
strLine = string.Empty;
}
}
if (strLine.Length > 0)
{
strResultText += strLine;
}
formatedLabelmaxdepth = formatedLabelmaxdepth < lineCount ? lineCount : formatedLabelmaxdepth;
return strResultText;
}
private void generateChart(DataTable dtResults, int chartType, int GraphType)
{
ChartSeriesType CurrentChartType = ChartSeriesType.Bar;
ChartSeriesOrientation ChartOrientation = ChartSeriesOrientation.Vertical;
float reportWidth = 5;
float reportHeight = 5;
float xAxisMargin = 0f;
float xAxisLableMargin = 0f;
int xAxisRotationAngle = 0;
AlignedPositions xAxisTextAlignedPositions = AlignedPositions.Left;
AlignedPositions xAxisLabelAlignedPositions = AlignedPositions.Top;
AlignedPositions xAxisAxisLabel = AlignedPositions.Top;
#region Format Data
for (int i = 0; i < dtResults.Rows.Count; i++)
{
dtResults.Rows[i][0] = formatTextWrap(dtResults.Rows[i][0].ToString());
}
#endregion
#region Graph Type
switch (chartType)
{
case 1://Column
CurrentChartType = ChartSeriesType.Bar;
ChartOrientation = ChartSeriesOrientation.Vertical;
xAxisTextAlignedPositions = AlignedPositions.Left;
xAxisLabelAlignedPositions = AlignedPositions.Top;
xAxisAxisLabel = AlignedPositions.Top;
xAxisRotationAngle = 270;
break;
case 2: //Bar
CurrentChartType = ChartSeriesType.Bar;
ChartOrientation = ChartSeriesOrientation.Horizontal;
xAxisTextAlignedPositions = AlignedPositions.Left;
xAxisLabelAlignedPositions = AlignedPositions.Right;
xAxisAxisLabel = AlignedPositions.Left;
xAxisRotationAngle = 0;
#region Reverse the Data
DataTable clonedtResults;
clonedtResults = dtResults.Clone();
for (int intRow = dtResults.Rows.Count - 1; intRow >= 0; intRow--)
{
clonedtResults.ImportRow(dtResults.Rows[intRow]);
}
dtResults.Rows.Clear();
foreach (DataRow dr in clonedtResults.Rows)
{
dtResults.ImportRow(dr);
}
#endregion
break;
case 3: //Line
CurrentChartType = ChartSeriesType.Line;
ChartOrientation = ChartSeriesOrientation.Vertical;
xAxisTextAlignedPositions = AlignedPositions.Left;
xAxisLabelAlignedPositions = AlignedPositions.Top;
xAxisAxisLabel = AlignedPositions.Top;
xAxisRotationAngle = 270;
break;
default: break;
}
#endregion
int totalBarCount = dtResults.Rows.Count * (dtResults.Columns.Count - 1);
int maxXAxisTextlength = formatedLabelmaxlength > 80 ? 80 : formatedLabelmaxlength;
GetChartProperties(ChartOrientation, totalBarCount, maxXAxisTextlength, ref reportWidth, ref reportHeight, ref xAxisMargin, ref xAxisLableMargin);
#region Chart Formating
this.chartGeneric.Skin = "Mac";
this.chartGeneric.AutoLayout = false;
this.chartGeneric.AutoTextWrap = false;
this.chartGeneric.BitmapResolution = 96F;
this.chartGeneric.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
this.chartGeneric.ChartTitle.Visible = false;
this.chartGeneric.PlotArea.Appearance.Position.X = 0;
this.chartGeneric.PlotArea.Appearance.Position.Y = 0;
this.chartGeneric.PlotArea.Chart.Appearance.Position.X = 0;
this.chartGeneric.PlotArea.Chart.Appearance.Position.Y = 0;
this.chartGeneric.PlotArea.EmptySeriesMessage.TextBlock.Text = "There is nothing to display";
this.chartGeneric.PlotArea.XAxis.AxisLabel.Visible = true;
this.chartGeneric.PlotArea.YAxis.AxisLabel.Visible = true;
this.chartGeneric.SeriesOrientation = ChartOrientation;
this.chartGeneric.PlotArea.XAxis.AxisLabel.Appearance.Dimensions.Margins = new ChartMargins(0);
this.chartGeneric.PlotArea.XAxis.AxisLabel.Appearance.Dimensions.Paddings = new ChartPaddings(0);
this.chartGeneric.PlotArea.XAxis.AutoShrink = false;
this.chartGeneric.PlotArea.XAxis.AutoScale = false;
this.chartGeneric.PlotArea.XAxis.Appearance.TextAppearance.MaxLength = 255;
this.chartGeneric.PlotArea.XAxis.Appearance.TextAppearance.AutoTextWrap = AutoTextWrap.False;
this.chartGeneric.Appearance.TextQuality = TextQuality.SystemDefault;
this.chartGeneric.PlotArea.XAxis.Appearance.Color = Color.Black;
this.chartGeneric.PlotArea.XAxis.LayoutMode = ChartAxisLayoutMode.Between;
this.chartGeneric.DefaultType = CurrentChartType;
this.chartGeneric.PlotArea.XAxis.AxisLabel.TextBlock.Text = dtResults.Columns[0].ColumnName;
this.chartGeneric.PlotArea.YAxis.AxisLabel.TextBlock.Text = "(in numbers) ";
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Top = 50;
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Right = 250;
this.chartGeneric.Appearance.BarWidthPercent = totalBarCount > 5 ? 100 : 20;
this.chartGeneric.Appearance.BarOverlapPercent = -10;
this.chartGeneric.PlotArea.XAxis.AxisLabel.Appearance.Dimensions.Margins.Left = new Telerik.Reporting.Charting.Styles.Unit(5);
this.chartGeneric.PlotArea.XAxis.Appearance.TextAppearance.Position.AlignedPosition = xAxisTextAlignedPositions;
this.chartGeneric.PlotArea.XAxis.Appearance.LabelAppearance.Position.AlignedPosition = xAxisLabelAlignedPositions;
this.chartGeneric.PlotArea.XAxis.AxisLabel.Appearance.Position.AlignedPosition = xAxisAxisLabel;
this.chartGeneric.PlotArea.XAxis.Appearance.LabelAppearance.RotationAngle = xAxisRotationAngle;
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Left = 50;
if (ChartOrientation == ChartSeriesOrientation.Vertical)
{
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Left = 50;
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Bottom = xAxisMargin;
this.chartGeneric.PlotArea.XAxis.Appearance.TextAppearance.Dimensions.Margins.Bottom = xAxisLableMargin;
}
else
{
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Left = xAxisMargin;
this.chartGeneric.PlotArea.Appearance.Dimensions.Margins.Bottom = 50;
this.chartGeneric.PlotArea.XAxis.Appearance.TextAppearance.Dimensions.Margins.Left = xAxisLableMargin;
}
#endregion
#region Legend Formating
this.chartGeneric.Legend.Appearance.Position.AlignedPosition = AlignedPositions.TopRight;
this.chartGeneric.Legend.Appearance.Dimensions.Margins = new ChartMargins(5);
this.chartGeneric.Legend.Appearance.Dimensions.Paddings = new ChartPaddings(5);
this.chartGeneric.Legend.Appearance.Overflow = Overflow.Column;
this.chartGeneric.Legend.Appearance.Dimensions.Width = 75;
this.chartGeneric.Legend.Appearance.ItemTextAppearance.AutoTextWrap = AutoTextWrap.True;
this.chartGeneric.Legend.Appearance.Location = LabelLocation.OutsidePlotArea;
this.chartGeneric.Legend.Appearance.ItemTextAppearance.Position.AlignedPosition = AlignedPositions.Top;
#endregion
#region Chart Series
chartGeneric.Series.Clear();
chartGeneric.DataSource = dtResults;
List<ChartSeries> chartSeries = new List<ChartSeries>();
chartGeneric.PlotArea.XAxis.DataLabelsColumn = dtResults.Columns[0].ColumnName.Trim();
for (int counter = 1; counter < dtResults.Columns.Count; counter++)
{
ChartSeries series = new ChartSeries()
{
DataYColumn = dtResults.Columns[counter].ColumnName,
Name = dtResults.Columns[counter].Caption,
YAxisType = ChartYAxisType.Primary,
Type = chartGeneric.DefaultType
};
//series.Appearance.FillStyle.FillType = FillType.Gradient;
series.Appearance.LabelAppearance.LabelLocation = Telerik.Reporting.Charting.Styles.StyleSeriesItemLabel.ItemLabelLocation.Inside;
//series.Appearance.TextAppearance.FillStyle.MainColor = Color.White;
//series.Appearance.TextAppearance.FillStyle.FillType = FillType.Solid;
if (ChartOrientation == ChartSeriesOrientation.Vertical)
{
series.Appearance.LabelAppearance.Position.AlignedPosition = AlignedPositions.Top;
}
else
{
series.Appearance.LabelAppearance.Position.AlignedPosition = AlignedPositions.Right;
}
series.Appearance.BarWidthPercent = 90;
series.Appearance.TextAppearance.TextProperties.Color = Color.Black;
series.Appearance.TextAppearance.Dimensions.Margins = new ChartMargins(0);
series.Appearance.TextAppearance.Dimensions.Paddings = new ChartPaddings(0);
//series.Appearance.TextAppearance.TextProperties.Font = new System.Drawing.Font(FontFamily.GenericSansSerif, 12, GraphicsUnit.Pixel);
chartSeries.Add(series);
}
// add the series to the chart, chart to page.
chartGeneric.Series.AddRange(chartSeries.ToArray());
#endregion
#region Chart Dimensions
this.chartGeneric.Docking = DockingStyle.None;
this.chartGeneric.Appearance.Position.AlignedPosition = AlignedPositions.Center;
this.chartGeneric.Location = new PointU(new Telerik.Reporting.Drawing.Unit(0), new Telerik.Reporting.Drawing.Unit(0));
this.chartGeneric.Width = new Telerik.Reporting.Drawing.Unit(reportWidth, Telerik.Reporting.Drawing.UnitType.Inch);
this.chartGeneric.Height = new Telerik.Reporting.Drawing.Unit(reportHeight, Telerik.Reporting.Drawing.UnitType.Inch);
//this.chartGeneric.Width = new Telerik.Reporting.Drawing.Unit(8, Telerik.Reporting.Drawing.UnitType.Inch);
//this.chartGeneric.Height = new Telerik.Reporting.Drawing.Unit(10, Telerik.Reporting.Drawing.UnitType.Inch);
#endregion
#region Page Dimensions
this.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom;
this.PageSettings.Landscape = true;
this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(8.27, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(11.69f, Telerik.Reporting.Drawing.UnitType.Inch));
//this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(reportWidth + 0.5, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(reportHeight + 1.0 + this.pageHeaderSection1.Height.Value, Telerik.Reporting.Drawing.UnitType.Inch));
//this.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom;
//Setting the page Width and Height
// this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(7.0f + 0.5, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(10.25f, Telerik.Reporting.Drawing.UnitType.Inch));
//if (GraphType == 2)
//{
// this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(reportWidth + 0.5, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(_standardHeight, Telerik.Reporting.Drawing.UnitType.Inch));
//}
//else
//{
// this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(_standardHeight, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(reportHeight + 1.0 + this.pageHeaderSection1.Height.Value, Telerik.Reporting.Drawing.UnitType.Inch));
//}
//this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(10, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(12, Telerik.Reporting.Drawing.UnitType.Inch));
// this.PageSettings.Landscape = true;
// this.PageSettings.PaperSize = new Telerik.Reporting.Drawing.SizeU(new Telerik.Reporting.Drawing.Unit(10, Telerik.Reporting.Drawing.UnitType.Inch), new Telerik.Reporting.Drawing.Unit(reportHeight + 1.0 + this.pageHeaderSection1.Height.Value, Telerik.Reporting.Drawing.UnitType.Inch));
#endregion
}
private void GetChartProperties(ChartSeriesOrientation objChartOrientation, int totalBarCount, int maxXAxisTextlength, ref float reportWidth, ref float reportHeight, ref float xAxisMargin, ref float xAxisLableMargin)
{
if (objChartOrientation == ChartSeriesOrientation.Horizontal)
{
float tempwidth = reportWidth;
reportWidth = reportHeight;
reportHeight = tempwidth;
}
xAxisMargin = (maxXAxisTextlength * 7) + 60;
xAxisLableMargin = (maxXAxisTextlength * 6) + 10;
if (objChartOrientation == ChartSeriesOrientation.Vertical)
{
if (maxXAxisTextlength > 50)
{
reportHeight += (float)(6 - (reportWidth - (maxXAxisTextlength * 0.0625)));
}
if (formatedLabelmaxdepth < totalBarCount)
{
if (totalBarCount > 5)
{
//reportWidth += (float)reportWidth + (totalBarCount - 3) / 9;
reportWidth += (float)reportWidth + (totalBarCount * .3f);
}
}
else
{
if (formatedLabelmaxdepth > 3)
{
reportWidth += (float)reportWidth + (formatedLabelmaxdepth * .1f);
}
}
}
else
{
if (maxXAxisTextlength > 50)
{
reportWidth += (float)(8 - (reportWidth - (maxXAxisTextlength * 0.0625)));
}
if (formatedLabelmaxdepth < totalBarCount)
{
if (totalBarCount > 5)
{
// reportHeight += (float)reportHeight + (totalBarCount - 5) / 9;
reportHeight += (float)reportHeight + (totalBarCount * .3f);
}
}
else
{
if (formatedLabelmaxdepth > 3)
{
reportHeight += (float)reportHeight + (formatedLabelmaxdepth * .1f);
}
}
}
reportHeight = reportHeight > 300 ? 300 : reportHeight;
reportWidth = reportWidth > 300 ? 300 : reportWidth;
}
#endregion
}
}