2025-04-15 12:10:19 +02:00

332 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using ceTe.DynamicPDF;
using ceTe.DynamicPDF.Text;
using PDFGenerator;
using PDFGenerator.BusinessLayer;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.Presentation.Section.Charts;
using PDFGenerator.Presentation.Section;
using Dundas.Charting.WebControl;
using System.Collections.Generic;
public class S85 : ISezione
{
string Titolo = " ";
int Resolution = 300;
private bool _IncludiScenarioMedio;
private string _testointroduttivo = "";
public string TestoIntroduttivo
{
get
{
return _testointroduttivo;
}
set
{
_testointroduttivo = value;
}
}
public S85()
{
//
// TODO: Add constructor logic here
//
}
#region ISezione Members
/// <summary>
/// Recupera il massimo valore dell'arraylist
/// </summary>
/// <param name="arrList"></param>
/// <returns></returns>
public static object GetMaxValue(ArrayList arrList)
{
ArrayList sortArrayList = arrList;
sortArrayList.Sort();
sortArrayList.Reverse();
return sortArrayList[0];
}
/// <summary>
/// Recupera il minimo valore dell'arraylist
/// </summary>
/// <param name="arrList"></param>
/// <returns></returns>
public static object GetMinValue(ArrayList arrList)
{
ArrayList sortArrayList = arrList;
sortArrayList.Sort();
return sortArrayList[0];
}
private static int MonthDifference(DateTime lValue, DateTime rValue)
{
return Math.Abs((lValue.Month - rValue.Month) + 12 * (lValue.Year - rValue.Year));
}
private List<double> GetEqualAxisXLabels(DateTime minValue, DateTime maxValue, int labelsCount, int margin, double labelMarginLeft, double labelMarginRight, out double newMinValue, out double newMaxValue)
{
double difference = (maxValue.ToOADate() - minValue.ToOADate());
double newDifference = difference * margin / 100;
double intervalLength = newDifference / labelsCount;
double differenceToAdd = (newDifference - difference) / 2;
newMinValue = minValue.ToOADate() - differenceToAdd;
newMaxValue = maxValue.ToOADate() + differenceToAdd;
// with labels margins now
double lblNewDifference = newDifference * ((((double)labelMarginLeft + (double)labelMarginRight) + 100d) / 100d);
double lblIntervalLength = lblNewDifference / labelsCount;
double lblNewMinValue = newMinValue - (newDifference * ((double)labelMarginLeft) / 100d);
List<double> labels = new List<double>();
//for (int i = 0; i< labelsCount; i++)
//{
// labels.Add(newMinValue + i * intervalLength + intervalLength / 2);
//}
for (int i = 0; i < labelsCount; i++)
{
labels.Add(lblNewMinValue + i * lblIntervalLength + lblIntervalLength / 2);
}
return labels;
}
public void writeSezione(DataThread dataThread)
{
DocumentPDF document = dataThread.DocumentPDF;
DataSetS85 set = (DataSetS85)dataThread.Data.DatiSezione;
int ChartHeigth = 215;
//eventuali operazioni sul dataset
//calcola se entra nella pagina altrimenti aggiunge una nuova pagina. In questa sezione il # di righe è fissato
//if (document.checkMargin(ChartHeigth + 50))
// //if (document.checkMargin(ChartHeigth + 100))
// document.addPage();
document.setLastPos((document.getLastPos() * -1) + 410);
ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
//page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Possibile evoluzione del rendimento", document.getMargineLeft(), document.getLastPos() - 30, 500, 12, Globals.OpenTypeFontVerdanaB, 12, ceTe.DynamicPDF.CmykColor.Black));
document.setTitolo(Titolo);
document.setHeaderSpace(5);
//document.setChapterHeader(_testointroduttivo);
document.setChapterHeader(_testointroduttivo, 0, 520, 8);
#region Valori Asse Y
//Impostare lasse delle ordinate in funzione del valore massimo e minimo
// rappresentati nel grafico (tra i valori del cono e il valore del rendimento)
DataTable dtDati = new DataTable();
DataTable dtGrafico = new DataTable();
DataTable dtRendimento = new DataTable();
dtGrafico = set.Tables["DatiGrafico"].Copy();
dtRendimento = set.Tables["Rendimento"];
dtGrafico.Merge(dtRendimento, true);
DataView dwValoriY = new DataView(dtGrafico);
dtDati = dwValoriY.ToTable(false, "Ottimistico", "Pessimistico", "Valore");
// in un arraylist che poi andrò ad ordinare per recuperare massimo e minimo inserisco tutti valore delle 3 colonne
// "Ottimistico", "Pessimistico", "Valore"
ArrayList al = new ArrayList();
// MODIFICA TORRESI
/*
foreach (DataRow r in dtDati.Rows)
foreach (DataColumn c in dtDati.Columns)
{
if (r[c].ToString() != string.Empty)
al.Add(Convert.ToDouble(r[c].ToString()));
}
*/
foreach (DataColumn c in dtDati.Columns)
{
foreach (DataRow r in dtDati.Rows)
if ((r[c].ToString() != string.Empty) && (r[c].ToString() != ""))
al.Add(Convert.ToDouble(r[c].ToString()));
}
//FINE MODIFICA TORRESI
#endregion
DatiGrafico dati = new DatiGrafico();
dati.dataTab = set.Tables["DatiGrafico"];
dati.setHeight(ChartHeigth);
dati.setWidth(440);
LineeRendimentoCumulato grafico = new LineeRendimentoCumulato();
grafico.MaximumY = Convert.ToDouble(GetMaxValue(al));
grafico.MinimumY = Convert.ToDouble(GetMinValue(al));
if (set.Tables["Rendimento"].Rows.Count > 0)
{
if (set.Tables["Rendimento"].Rows[0]["Data"] != DBNull.Value)
grafico.DataRendimento = (DateTime)set.Tables["Rendimento"].Rows[0]["Data"];
if (set.Tables["Rendimento"].Rows[0]["Valore"] != DBNull.Value)
grafico.Rendimento = Convert.ToDouble(set.Tables["Rendimento"].Rows[0]["Valore"]);
}
grafico.IncludiScenarioMedio = false;
grafico.getGrafico(dati);
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(100, document.getLastPos() + ChartHeigth - 17.5f, 405, 25, 0, new RgbColor(232, 236, 237)));
var chartGrafico = grafico.getGrafico(dati);
// add 6 months to maximum axis X
//chartGrafico.ChartAreas[0].ReCalc();
//chartGrafico.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(chartGrafico.ChartAreas[0].AxisX.Maximum).AddMonths(6).ToOADate();
// ---------------------
FormatNum format = new FormatNum();
DataTable datiAsse = set.Tables["DatiAsse"];
if (datiAsse.Rows.Count > 0)
{
var maxAxisX = grafico.maxAxisX;
var minAxisX = grafico.minAxisX;
//ArrayList labels = format.CreaCustomDataTime(Convert.ToDateTime(datiAsse.Rows[0]["DataInizio"]), Convert.ToInt32(datiAsse.Rows[0]["IntervalloMesi"]));
//float startX = getX(Convert.ToInt32(datiAsse.Rows[0]["IntervalloMesi"])) - 15;
//float endX = 489; // punto fisso 489.
//float intervallo = (endX - startX) / (labels.Count - 1);
//var labelWidth = (maxAxisX - minAxisX) / 10;
//foreach (string label in labels)
//{
// int month = int.Parse(label.ToString().Split('/')[0]);
// int year = int.Parse(label.ToString().Split('/')[1]) + 2000;
// double labelValue = new DateTime(year, month, 1).ToOADate();
// var leftLabel = labelValue - labelWidth / 2;
// var rightLabel = labelValue + labelWidth / 2;
// CustomLabel axisXLabel = new CustomLabel(leftLabel, rightLabel, new DateTime(year, month, 15).ToString("MM/yy"), 0, LabelMark.None);
// chartGrafico.ChartAreas[0].AxisX.CustomLabels.Add(axisXLabel);
//}
// ----------- new
double newMinAxisX;
double newMaxAxisX;
var labels = GetEqualAxisXLabels(DateTime.FromOADate(minAxisX), DateTime.FromOADate(maxAxisX).AddMonths(6), 10, 108, 1.35d, 3, out newMinAxisX, out newMaxAxisX);
chartGrafico.ChartAreas[0].AxisX.Maximum = newMaxAxisX;
chartGrafico.ChartAreas[0].AxisX.Minimum = newMinAxisX;
chartGrafico.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
chartGrafico.ChartAreas[0].AxisX.MajorTickMark.LineColor = System.Drawing.Color.Transparent;
chartGrafico.ChartAreas[0].AxisX.MajorTickMark.Size = 1.5f;
//chartGrafico.ChartAreas[0].AxisX.LineWidth = 2;
var labelWidth = (maxAxisX - minAxisX) / 10;
foreach (double label in labels)
{
var leftLabel = label - labelWidth / 2;
var rightLabel = label + labelWidth / 2;
CustomLabel axisXLabel = new CustomLabel(leftLabel, rightLabel, DateTime.FromOADate(label).ToString("MM/yy"), 0, LabelMark.None);
chartGrafico.ChartAreas[0].AxisX.CustomLabels.Add(axisXLabel);
}
// ----------
}
var pos = document.getLastPos();
chartGrafico.BackColor = System.Drawing.Color.FromArgb(232, 236, 237);
var currentResolution = chartGrafico.ImageResolution;
document.InsertGrafico(chartGrafico, 70, pos, Resolution);
chartGrafico.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);
chartGrafico.ImageResolution = currentResolution;
document.InsertGrafico(chartGrafico, 70, pos, Resolution, 6.35f);
var linesYPosition = document.getLastPos() + ChartHeigth - 18;
// white rectangles
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(69, linesYPosition, 31, 26, 0, new RgbColor(255, 255, 255)));
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(73 + 425, linesYPosition, 31, 26, 0, new RgbColor(255, 255, 255)));
//page.Elements.Add(new ceTe.DynamicPDF.PageElements.Rectangle(130 + (float)chartGrafico.Width.Value, linesYPosition, 170, 60, 0, new RgbColor(255, 0, 0)));
// end of white rectangles
float yLegendOffset = -3.5f;
float xLegendOffset = -3;
ceTe.DynamicPDF.PageElements.Rectangle rect = new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + xLegendOffset + 95, document.getLastPos() + yLegendOffset + ChartHeigth + 5, 10, 2, 0, new RgbColor(0, 0, 255));
page.Elements.Add(rect);
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Scenario ottimistico", document.getMargineLeft() + xLegendOffset + 110, document.getLastPos() + yLegendOffset + ChartHeigth, 120, 10, Globals.OpenTypeFontVerdana, 9));
rect = new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + xLegendOffset + 225, document.getLastPos() + yLegendOffset + ChartHeigth + 2, 5, 5, 0, new RgbColor(0, 0, 0));
rect.CornerRadius = 2.5F;
page.Elements.Add(rect);
//to tutaj
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Rendimento cumulato", document.getMargineLeft() + xLegendOffset + 235, document.getLastPos() + yLegendOffset + ChartHeigth, 120, 10, Globals.OpenTypeFontVerdana, 9));
rect = new ceTe.DynamicPDF.PageElements.Rectangle(document.getMargineLeft() + xLegendOffset + 345, document.getLastPos() + yLegendOffset + ChartHeigth + 5, 10, 2, 0, new RgbColor(239, 65, 53));
page.Elements.Add(rect);
page.Elements.Add(new ceTe.DynamicPDF.PageElements.Label("Scenario pessimistico", document.getMargineLeft() + xLegendOffset + 360, document.getLastPos() + yLegendOffset + ChartHeigth, 120, 10, Globals.OpenTypeFontVerdana, 9));
document.setLastPos(ChartHeigth + 40);
dataThread.UltimaSezioneStampata = "S85";
}
/// <summary>
/// Ritorna la x su cui scrivere la prima etichetta dell'asse x. ///
/// </summary>
/// <param name="intervallomesi"></param>
/// <returns></returns>
private float getX(int intervallomesi)
{
decimal massimo = 360;
decimal xPartenza = 133;
decimal xFine = xPartenza + 23;
decimal intervallo = xFine - xPartenza;
decimal rapporto = intervallo / massimo;
return Convert.ToSingle(xFine - (rapporto * intervallomesi));
}
public void setTitolo(string label)
{
Titolo = label;
}
public bool IncludiScenarioMedio
{
get
{
return _IncludiScenarioMedio;
}
set
{
_IncludiScenarioMedio = value;
}
}
#endregion
}