using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web.Mvc; using System.Web.UI.DataVisualization.Charting; using System.Web.UI.WebControls; using LeafWeb.Core.Charter; using LeafWeb.Core.Entities; using LeafWeb.Core.Parsers; using LeafWeb.Web.Charter; namespace LeafWeb.Web.Controllers { public class LeafCharterController : ControllerBase { public ActionResult Index(int leafInputId) { var hasLeafOutputFile = DataService .GetLeafInput(leafInputId)? .OutputFiles? .FirstOrDefault(f => f.IsLeafChartFile) != null; return View(leafInputId); } public ActionResult LeafCharts(int leafInputId) { var leafOutputFile = DataService .GetLeafInput(leafInputId)? .OutputFiles? .FirstOrDefault(f => f.IsLeafChartFile); if (leafOutputFile == null) throw new ArgumentOutOfRangeException(); // TODO: break var curveData = GetCurveData(leafOutputFile.Contents); var charts = GetChartBitmaps(curveData).ToList(); var combinedChart = CombineBitmaps(charts); foreach (var chart in charts) chart.Dispose(); // cleanup using (var ms = new MemoryStream()) { combinedChart.Save(ms, ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); return File(ms.ToArray(), "image/png", "mychart.png"); } } private CurveData[] GetCurveData(byte[] fileContents) { LeafGasComparison[] leafGasComparisons; using (var parser = new LeafGasComparisonParser(fileContents)) leafGasComparisons = parser.Parse(); return CurveDataConverter.Convert(leafGasComparisons).ToArray(); } private IEnumerable GetChartBitmaps(CurveData[] curveData) { var charts = LeafWebCharter.ProduceCharts(curveData[0]); foreach (var chart in charts) { // http://stackoverflow.com/a/336396/99492 var ms = new MemoryStream(); // this gets attached to the bmp, dispose of it later chart.SaveImage(ms, ChartImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); yield return new Bitmap(ms); } } private Bitmap CombineBitmaps(IList bitmaps, int columnCount = 2) { var cellWidth = bitmaps[0].Width; var cellHeight = bitmaps[0].Height; var width = cellWidth * 2; var height = cellHeight * bitmaps.Count / 2; var combinedBitmap = new Bitmap(width, height); using (var g = Graphics.FromImage(combinedBitmap)) { var currentCol = 0; var currentRow = 0; foreach (var image in bitmaps) { g.DrawImage(image, currentCol * cellWidth, currentRow * cellHeight); currentCol = (currentCol + 1)%columnCount; currentRow += currentCol == 0 ? 1 : 0; } } return combinedBitmap; } public ActionResult ChartSample() { var chart = new Chart { BackColor = Color.FromArgb(255, 255, 255), Width = Unit.Pixel(250), Height = Unit.Pixel(2500) }; var series = new Series { ChartArea = "ca1", ChartType = SeriesChartType.Line }; //series.Font = new Font("Verdana", 8.25f, FontStyle.Regular); var myRandom = new Random(); for (int i = 0; i < 100; i++) { var dp = new DataPoint(); dp.AxisLabel = String.Format("{0}-{1}", i, Guid.NewGuid().ToString().Substring(0, 4)); dp.YValues = new double[] { myRandom.Next(5, 100) }; series.Points.Add(dp); } chart.Series.Add(series); var area = new ChartArea("ca1"); area.Area3DStyle.Enable3D = false; area.AxisX.Interval = 1; //area.BackColor = Color.Transparent; //var labelStyle = new LabelStyle(); //labelStyle.Enabled = true; //labelStyle.Font = new Font("Arial", 3f); area.AxisX.LabelStyle.Font = new Font("Verdana", 8.25f, FontStyle.Underline);//Why does it recognize the style but not the font!!!??? chart.ChartAreas.Add(area); using (var ms = new MemoryStream()) { chart.SaveImage(ms, ChartImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); return File(ms.ToArray(), "image/png", "mychart.png"); } } } }