Ligne 1 : //using GemBox.Document;
Ligne 2 : using System;
Ligne 3 : using System.Collections.Generic;
Ligne 4 : using System.Drawing;
Ligne 5 : using System.IO;
Ligne 6 : using System.Linq;
Ligne 7 : using System.Web;
Ligne 8 : using System.Web.UI.DataVisualization.Charting;
Ligne 9 : using System.Web.UI.WebControls;
Ligne 10 :
Ligne 11 : /// <summary>
Ligne 12 : /// Description résumée de ChartMemoryStream
Ligne 13 : /// </summary>
Ligne 14 : public class ChartMemoryStream
Ligne 15 : {
Ligne 16 : public SeriesChartType ChartType { get; set; }
Ligne 17 : public int ChartWidth { get; set; }
Ligne 18 : public int ChartHeight { get; set; }
Ligne 19 : public string ChartTitle { get; set; }
Ligne 20 :
Ligne 21 : public PieChartData PieData { get; set; }
Ligne 22 : public RadarChartData RadarData { get; set; }
Ligne 23 : public HistogrammeChartDatas HistogrammeDatas { get; set; }
Ligne 24 :
Ligne 25 : public ChartMemoryStream()
Ligne 26 : {
Ligne 27 : ChartType = SeriesChartType.Pie;
Ligne 28 : ChartWidth = 580;
Ligne 29 : ChartHeight = 240;
Ligne 30 : }
Ligne 31 :
Ligne 32 : public ChartMemoryStream(SeriesChartType chartType, int largeur, int hauteur, string titre)
Ligne 33 : {
Ligne 34 : ChartType = chartType;
Ligne 35 : ChartWidth = largeur;
Ligne 36 : ChartHeight = hauteur;
Ligne 37 : ChartTitle = titre;
Ligne 38 : }
Ligne 39 :
Ligne 40 : public class PieChartData
Ligne 41 : {
Ligne 42 : public string[] Legendes { get; set; }
Ligne 43 : public double[] Valeurs { get; set; }
Ligne 44 : public Color[] Couleurs { get; set; }
Ligne 45 : }
Ligne 46 :
Ligne 47 : public class RadarChartData
Ligne 48 : {
Ligne 49 : public string[] Legendes { get; set; }
Ligne 50 : public double[] Valeurs { get; set; }
Ligne 51 : public Color[] Couleurs { get; set; }
Ligne 52 : }
Ligne 53 :
Ligne 54 : public class HistogrammeChartDatas : List<HistogrammeChartData>
Ligne 55 : {
Ligne 56 : public bool ShowPercentage { get; set; }
Ligne 57 :
Ligne 58 : public HistogrammeChartDatas(bool showPercentage)
Ligne 59 : {
Ligne 60 : ShowPercentage = showPercentage;
Ligne 61 : }
Ligne 62 : }
Ligne 63 : public class HistogrammeChartData
Ligne 64 : {
Ligne 65 : public string[] XAxisLibelles { get; set; }
Ligne 66 : public double[] Valeurs { get; set; }
Ligne 67 : public Color Couleur { get; set; }
Ligne 68 : public string TitreLegende { get; set; }
Ligne 69 : }
Ligne 70 :
Ligne 71 : public MemoryStream GetChart()
Ligne 72 : {
Ligne 73 : if (ChartType == SeriesChartType.Pie) { return CreatePieChart(); }
Ligne 74 : else if (ChartType == SeriesChartType.Doughnut) { return CreateDoughnutChart(); }
Ligne 75 : else if (ChartType == SeriesChartType.Radar) { return CreateRadarChart(); }
Ligne 76 : else if (ChartType == SeriesChartType.StackedColumn) { return CreateHistogrammeChart(); }
Ligne 77 : else if (ChartType == SeriesChartType.Column) { return CreateHistogrammeChart(); }
Ligne 78 : return null;
Ligne 79 : }
Ligne 80 :
Ligne 81 : private MemoryStream CreateRadarChart()
Ligne 82 : {
Ligne 83 : Chart c = new Chart { AntiAliasing = AntiAliasingStyles.All, TextAntiAliasingQuality = TextAntiAliasingQuality.High, Width = ChartWidth, Height = ChartHeight };
Ligne 84 : c.Titles.Add(ChartTitle);
Ligne 85 : ChartArea ca = new ChartArea { BackColor = Color.White };
Ligne 86 : c.ChartAreas.Add(ca);
Ligne 87 :
Ligne 88 : Series s = new Series
Ligne 89 : {
Ligne 90 : Font = new Font("Lucida Sans Unicode", 6f),
Ligne 91 : Color = Color.FromArgb(215, 47, 6),
Ligne 92 : BorderColor = Color.FromArgb(159, 27, 13),
Ligne 93 : BackSecondaryColor = Color.FromArgb(173, 32, 11),
Ligne 94 : ChartType = ChartType,
Ligne 95 : Name = "Cotation"
Ligne 96 : };
Ligne 97 : c.Legends.Add(new Legend() { LegendStyle = LegendStyle.Column, Docking = Docking.Right, Alignment = StringAlignment.Center });
Ligne 98 :
Ligne 99 : for (int i = 0; i < RadarData.Legendes.Length; i++)
Ligne 100 : {
Ligne 101 : s.Points.AddXY(RadarData.Legendes[i], CalcPercent(RadarData.Valeurs[i], RadarData.Valeurs.Sum()));
Ligne 102 : }
Ligne 103 : c.Series.Add(s);
Ligne 104 :
Ligne 105 : var chartimage = new MemoryStream();
Ligne 106 : c.SaveImage(chartimage, ChartImageFormat.Png);
Ligne 107 : return chartimage;
Ligne 108 : }
Ligne 109 :
Ligne 110 : private MemoryStream CreatePieChart()
Ligne 111 : {
Ligne 112 : Chart c = new Chart { AntiAliasing = AntiAliasingStyles.All, TextAntiAliasingQuality = TextAntiAliasingQuality.High, Width = ChartWidth, Height = ChartHeight };
Ligne 113 : c.Titles.Add(new Title() { Text = ChartTitle, Font = new Font("Lucida Sans Unicode", 14f) });
Ligne 114 : ChartArea ca = new ChartArea { BackColor = Color.White };
Ligne 115 : c.ChartAreas.Add(ca);
Ligne 116 : Series s = new Series
Ligne 117 : {
Ligne 118 : Font = new Font("Lucida Sans Unicode", 6f),
Ligne 119 : Color = Color.FromArgb(215, 47, 6),
Ligne 120 : BorderColor = Color.FromArgb(159, 27, 13),
Ligne 121 : BackSecondaryColor = Color.FromArgb(173, 32, 11),
Ligne 122 : ChartType = ChartType
Ligne 123 : };
Ligne 124 : c.Legends.Add(new Legend() { LegendStyle = LegendStyle.Column, Docking = Docking.Right, Alignment = StringAlignment.Center });
Ligne 125 : for (int i = 0; i < PieData.Legendes.Length; i++)
Ligne 126 : {
Ligne 127 : DataPoint p = new DataPoint
Ligne 128 : {
Ligne 129 : XValue = i,
Ligne 130 : YValues = new double[] { PieData.Valeurs[i] },
Ligne 131 : LegendText = PieData.Legendes[i],
Ligne 132 : Label = CalcPercentToString(PieData.Valeurs[i], PieData.Valeurs.Sum()),
Ligne 133 : Color = PieData.Couleurs[i]
Ligne 134 : };
Ligne 135 : s.Points.Add(p);
Ligne 136 : }
Ligne 137 : c.Series.Add(s);
Ligne 138 : var chartimage = new MemoryStream();
Ligne 139 : c.SaveImage(chartimage, ChartImageFormat.Png);
Ligne 140 : return chartimage;
Ligne 141 : }
Ligne 142 :
Ligne 143 : private MemoryStream CreateDoughnutChart()
Ligne 144 : {
Ligne 145 : Chart c = new Chart { AntiAliasing = AntiAliasingStyles.All, TextAntiAliasingQuality = TextAntiAliasingQuality.High, Width = ChartWidth, Height = ChartHeight };
Ligne 146 : c.Titles.Add(new Title() { Text = ChartTitle, Font = new Font("Lucida Sans Unicode", 14f) });
Ligne 147 :
Ligne 148 : ChartArea ca = new ChartArea { BackColor = Color.White };
Ligne 149 : c.ChartAreas.Add(ca);
Ligne 150 : Series s = new Series
Ligne 151 : {
Ligne 152 : Font = new Font("Lucida Sans Unicode", 6f),
Ligne 153 : Color = Color.FromArgb(215, 47, 6),
Ligne 154 : BorderColor = Color.FromArgb(159, 27, 13),
Ligne 155 : BackSecondaryColor = Color.FromArgb(173, 32, 11),
Ligne 156 : ChartType = ChartType
Ligne 157 : };
Ligne 158 : c.Legends.Add(new Legend() { LegendStyle = LegendStyle.Column, Docking = Docking.Right, Alignment = StringAlignment.Center });
Ligne 159 :
Ligne 160 : for (int i = 0; i < PieData.Legendes.Length; i++)
Ligne 161 : {
Ligne 162 : DataPoint p = new DataPoint
Ligne 163 : {
Ligne 164 : XValue = i,
Ligne 165 : YValues = new double[] { PieData.Valeurs[i] },
Ligne 166 : LegendText = "(" + PieData.Valeurs[i] + ") " + PieData.Legendes[i],
Ligne 167 : Label = CalcPercentToString(PieData.Valeurs[i], PieData.Valeurs.Sum()),
Ligne 168 : Color = PieData.Couleurs[i]
Ligne 169 : };
Ligne 170 : s.Points.Add(p);
Ligne 171 : }
Ligne 172 : c.Series.Add(s);
Ligne 173 : var chartimage = new MemoryStream();
Ligne 174 : c.SaveImage(chartimage, ChartImageFormat.Png);
Ligne 175 : return chartimage;
Ligne 176 : }
Ligne 177 :
Ligne 178 : private MemoryStream CreateHistogrammeChart()
Ligne 179 : {
Ligne 180 : Chart c = new Chart { AntiAliasing = AntiAliasingStyles.All, TextAntiAliasingQuality = TextAntiAliasingQuality.High, Width = ChartWidth, Height = ChartHeight };
Ligne 181 : c.Titles.Add(new Title() { Text = ChartTitle, Font = new Font("Lucida Sans Unicode", 14f) });
Ligne 182 :
Ligne 183 : ChartArea ca = new ChartArea { BackColor = Color.White };
Ligne 184 : ca.AxisX.MajorGrid.LineColor = Color.LightGray;
Ligne 185 : ca.AxisY.MajorGrid.LineColor = Color.LightGray;
Ligne 186 :
Ligne 187 : c.ChartAreas.Add(ca);
Ligne 188 :
Ligne 189 : c.Legends.Add(new Legend() { LegendStyle = LegendStyle.Row, Docking = Docking.Bottom, Alignment = StringAlignment.Center });
Ligne 190 :
Ligne 191 : foreach (HistogrammeChartData histoData in HistogrammeDatas)
Ligne 192 : {
Ligne 193 : Series s = new Series
Ligne 194 : {
Ligne 195 : Font = new Font("Lucida Sans Unicode", 6f),
Ligne 196 : Color = histoData.Couleur,
Ligne 197 : BorderColor = Color.FromArgb(159, 27, 13),
Ligne 198 : BackSecondaryColor = Color.FromArgb(173, 32, 11),
Ligne 199 : ChartType = ChartType,
Ligne 200 : LegendText = histoData.TitreLegende
Ligne 201 : };
Ligne 202 :
Ligne 203 : for (int i = 0; i< histoData.XAxisLibelles.Length; i++)
Ligne 204 : {
Ligne 205 : DataPoint p = new DataPoint
Ligne 206 : {
Ligne 207 : XValue = i,
Ligne 208 : YValues = new double[] { histoData.Valeurs[i] },
Ligne 209 : LegendText = histoData.XAxisLibelles[i],
Ligne 210 : AxisLabel = histoData.XAxisLibelles[i],
Ligne 211 : Label = histoData.Valeurs[i] == 0 ? "" : HistogrammeDatas.ShowPercentage ? CalcPercentToString(histoData.Valeurs[i], histoData.Valeurs.Sum()) : histoData.Valeurs[i].ToString(),
Ligne 212 : Color = histoData.Couleur
Ligne 213 : };
Ligne 214 : s.Points.Add(p);
Ligne 215 : }
Ligne 216 :
Ligne 217 : c.Series.Add(s);
Ligne 218 : }
Ligne 219 :
Ligne 220 : var chartimage = new MemoryStream();
Ligne 221 : c.SaveImage(chartimage, ChartImageFormat.Png);
Ligne 222 : return chartimage;
Ligne 223 : }
Ligne 224 :
Ligne 225 : private string CalcPercentToString(double val, double tot)
Ligne 226 : {
Ligne 227 : return Math.Round(val * 100 / tot, 2, MidpointRounding.AwayFromZero).ToString() + " %";
Ligne 228 : }
Ligne 229 :
Ligne 230 : private double CalcPercent(double val, double tot)
Ligne 231 : {
Ligne 232 : return Math.Round(val * 100 / tot, 2, MidpointRounding.AwayFromZero);
Ligne 233 : }
Ligne 234 : }
|