ResultsBrowser : Printing a table with statistics, ver. 2.12.392

This commit is contained in:
Milan Hanajik 2016-11-23 14:07:04 +01:00
parent 59c0f73dd2
commit 7477a536da
5 changed files with 96 additions and 56 deletions

View File

@ -32,6 +32,8 @@ namespace ResultsBrowser.Forms
int[,] occurances;
string title;
public StatisticsDlg(IList<WaterMeter> waterMeters, bool twoDim,
Results.WMeterRsltItemSpec rowItem,
@ -52,13 +54,15 @@ namespace ResultsBrowser.Forms
if (!string.IsNullOrEmpty(Program.LocalSettings.TimePeriodFormat) && (startDate != new DateTime(0)) && (endDate != new DateTime(0)))
{
Text = string.Format(Program.LocalSettings.TimePeriodFormat, startDate, endDate);
title = string.Format(Program.LocalSettings.TimePeriodFormat, startDate, endDate);
}
else
{
Text = Strings.Statistics;
title = Strings.Statistics;
}
Text = title;
closeButton.Text = Strings.OkBtnText;
this.waterMeters = waterMeters;
@ -200,7 +204,17 @@ namespace ResultsBrowser.Forms
for (int column = 0; column < columnsCount; column++) table[row + 1, column + 1] = occurances[row, column].ToString();
}
new StatisticsPrintDocument(table, Config.Entities.PageOrientation.Portrait).Print();
switch (MessageBox.Show(Strings.Yes_Portrait_No_Landscape, Strings.Page_orientation, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
{
case DialogResult.Yes:
new StatisticsPrintDocument(title, table, Config.Entities.PageOrientation.Portrait).Print();
break;
case DialogResult.No:
new StatisticsPrintDocument(title, table, Config.Entities.PageOrientation.Landscape).Print();
break;
default:
break;
}
}
}
}

View File

@ -13,27 +13,18 @@ namespace ResultsBrowser.Output.Printers.Statistics
{
const string FontFamilyName = "Arial"; //"Times New Roman";
const int SpacingOne = 18;
const int SpacingOneAndHalf = (3 * SpacingOne) / 2;
const int TitleX = 240;
const int TitleY = 180; /// Depends on the size of the header
const int FirstLineNr = 14; /// Depends on the size of the header
const int Tab1 = 80; /// Positions of columns above the table
const int Tab2 = 240;
const int Tab3 = 430;
const int Tab4 = 590;
const int SpacingOne = 20;
const int BorderW = 1; /// Table border line width
const int MarginTop = 7; /// Margins of the text from table lines
const int MarginLeft = 4; /// Margins of the text from table lines
const int TopPadding = 2; /// Margins of the text from table lines
const int LeftPadding = 2; /// Margins of the text from table lines
const int TableTop = 50;
///
/// Data to print
///
string title;
string[,] table;
/// Top coordinate of the table
int top;
/// <summary>
/// Property variable for the Font the user wishes to use
/// </summary>
@ -45,8 +36,9 @@ namespace ResultsBrowser.Output.Printers.Statistics
/// Constructor
/// </summary>
/// <param name="textToPrint">Text to be printed</param>
public StatisticsPrintDocument(string[,] table, PageOrientation pageOrientation)
public StatisticsPrintDocument(string title, string[,] table, PageOrientation pageOrientation)
{
this.title = title;
this.table = table;
DefaultPageSettings.Landscape = (pageOrientation == PageOrientation.Landscape);
}
@ -62,7 +54,7 @@ namespace ResultsBrowser.Output.Printers.Statistics
if (font == null) { font = new Font(FontFamilyName, 10, FontStyle.Regular); }
if (boldFont == null) { boldFont = new Font(FontFamilyName, 10, FontStyle.Bold); }
if (fontTitle == null) { fontTitle = new Font(FontFamilyName, 24, FontStyle.Bold); }
if (fontTitle == null) { fontTitle = new Font(FontFamilyName, 14, FontStyle.Bold); }
}
/// <summary>
@ -79,7 +71,7 @@ namespace ResultsBrowser.Output.Printers.Statistics
int printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
int printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
int leftMargin = base.DefaultPageSettings.Margins.Left; /// X
int topMargin = base.DefaultPageSettings.Margins.Top; /// Y
int topMargin = base.DefaultPageSettings.Margins.Top; /// Y
/// Check if the user selected to print in Landscape mode
/// if they did then we need to swap height/width parameters
@ -90,14 +82,11 @@ namespace ResultsBrowser.Output.Printers.Statistics
printWidth = tmp;
}
int x = leftMargin;
int y = topMargin;
/// Use the StringFormat class for the text layout of our document
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
RectangleF printArea = new RectangleF(TitleX, TitleY, 667, 50);
e.Graphics.DrawString("TITLE", this.fontTitle, Brushes.Black, printArea);
RectangleF printArea = new RectangleF(leftMargin, topMargin, 667, TableTop);
e.Graphics.DrawString(title, this.fontTitle, Brushes.Black, printArea);
/// Measure texts
float[] columnWidths = new float[table.GetLength(1)];
@ -111,45 +100,64 @@ namespace ResultsBrowser.Output.Printers.Statistics
}
}
float[] rowHeights = new float[table.GetLength(0)];
for (int i = 0; i < rowHeights.Length; i++) rowHeights[i] = SpacingOne;
float top = (float)(topMargin + TableTop);
DrawTable(e, leftMargin, top, columnWidths, rowHeights);
/// Print texts
float top = topMargin;
for (int r = 0; r < table.GetLength(0); r++)
{
float left = leftMargin;
for (int c = 0; c < columnWidths.Length; c++)
{
PrintAt(e, new Point((int)left, (int)top), table[r, c]);
float textWidth = e.Graphics.MeasureString(table[r, c], font).Width;
float leftOffset = (columnWidths[c] - textWidth) / 2;
PrintAt(e, new Point((int)(left + leftOffset + LeftPadding), (int)top + 2), table[r, c]);
left += columnWidths[c];
}
top += SpacingOne;
top += rowHeights[r];
}
}
/// <summary>
/// Draw table lines.
/// </summary>
void DrawTable(System.Drawing.Printing.PrintPageEventArgs e)
void DrawTable(System.Drawing.Printing.PrintPageEventArgs e, float left, float top, float[] columnWidths, float[] rowHeights)
{
//int mid = top + SpacingOneAndHalf;
//int bot = top + (rowValues.Count + 1) * SpacingOneAndHalf;
float width = 0;
foreach (var cw in columnWidths) width += cw;
//// Horizontal lines
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, top), new Point(747, top));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, mid), new Point(747, mid));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, bot), new Point(747, bot));
/// First horizontal line
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)left, (int)top), new Point((int)(left + width), (int)top));
//// Vertical lines
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point( 80, top), new Point( 80, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(267, top), new Point(267, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(347, top), new Point(347, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(427, top), new Point(427, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(507, top), new Point(507, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(587, top), new Point(587, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(667, top), new Point(667, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(747, top), new Point(747, bot));
float height = 0;
foreach (var rh in rowHeights)
{
height += rh;
/// Horizontal line
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)left, (int)(top + height)), new Point((int)(left + width), (int)(top + height)));
}
/// First vertical line
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)left, (int)top), new Point((int)left, (int)(top + height)));
width = 0;
foreach (var cw in columnWidths)
{
width += cw;
/// Vertical line
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)(left + width), (int)top), new Point((int)(left + width), (int)(top + height)));
}
}
void PrintAt(System.Drawing.Printing.PrintPageEventArgs e, Point p, string text)
{
PrintAt(e, p.X, p.Y, text);
@ -171,17 +179,5 @@ namespace ResultsBrowser.Output.Printers.Statistics
RectangleF printArea = new RectangleF(x, y, 667, SpacingOne);
e.Graphics.DrawString(text, this.boldFont, Brushes.Black, printArea);
}
Point FromTest(int testNr, int parNr)
{
const int NrParamsInOneTableRow = 6;
int x = 267 + (parNr % NrParamsInOneTableRow) * 80 + MarginLeft;
int y = top + SpacingOneAndHalf + MarginTop + testNr * SpacingOneAndHalf;
if (parNr < 0) x = 80 + MarginLeft; /// When parNr == -1 return the left column x coordinate
return new Point(x, y);
}
}
}

View File

@ -237,4 +237,10 @@
<data name="Count" xml:space="preserve">
<value>Anzahl</value>
</data>
<data name="Page_orientation" xml:space="preserve">
<value>Seitenausrichtung</value>
</data>
<data name="Yes_Portrait_No_Landscape" xml:space="preserve">
<value>Ja = Porträt, Nein = Landschaft</value>
</data>
</root>

View File

@ -801,4 +801,10 @@
<data name="Format" xml:space="preserve">
<value>Format</value>
</data>
<data name="Page_orientation" xml:space="preserve">
<value>Page orientation</value>
</data>
<data name="Yes_Portrait_No_Landscape" xml:space="preserve">
<value>Yes = Portrait, No = Landscape</value>
</data>
</root>

View File

@ -1716,6 +1716,15 @@ namespace ResultsBrowser.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to Page orientation.
/// </summary>
internal static string Page_orientation {
get {
return ResourceManager.GetString("Page_orientation", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Print query results.
/// </summary>
@ -2111,5 +2120,14 @@ namespace ResultsBrowser.Resources {
return ResourceManager.GetString("Water_Meter", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Yes = Portrait, No = Landscape.
/// </summary>
internal static string Yes_Portrait_No_Landscape {
get {
return ResourceManager.GetString("Yes_Portrait_No_Landscape", resourceCulture);
}
}
}
}