Combine chart data into single image
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
@@ -25,7 +27,7 @@ namespace LeafWeb.Web.Controllers
|
|||||||
return View(leafInputId);
|
return View(leafInputId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult LeafCharts(int leafInputId, int number)
|
public ActionResult LeafCharts(int leafInputId)
|
||||||
{
|
{
|
||||||
var leafOutputFile =
|
var leafOutputFile =
|
||||||
DataService
|
DataService
|
||||||
@@ -36,30 +38,68 @@ namespace LeafWeb.Web.Controllers
|
|||||||
if (leafOutputFile == null)
|
if (leafOutputFile == null)
|
||||||
throw new ArgumentOutOfRangeException(); // TODO: break
|
throw new ArgumentOutOfRangeException(); // TODO: break
|
||||||
|
|
||||||
return File(GetChartImage(leafOutputFile.Contents, number), "image/png", "mychart.png");
|
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 byte[] GetChartImage(byte[] fileContents, int number, ChartImageFormat format = ChartImageFormat.Png)
|
private CurveData[] GetCurveData(byte[] fileContents)
|
||||||
{
|
{
|
||||||
LeafGasComparison[] leafGasComparisons;
|
LeafGasComparison[] leafGasComparisons;
|
||||||
using (var parser = new LeafGasComparisonParser(fileContents))
|
using (var parser = new LeafGasComparisonParser(fileContents))
|
||||||
leafGasComparisons = parser.Parse();
|
leafGasComparisons = parser.Parse();
|
||||||
|
|
||||||
var curveData = CurveDataConverter.Convert(leafGasComparisons).ToArray();
|
return CurveDataConverter.Convert(leafGasComparisons).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerable<Bitmap> GetChartBitmaps(CurveData[] curveData)
|
||||||
|
{
|
||||||
var charts = LeafWebCharter.ProduceCharts(curveData[0]);
|
var charts = LeafWebCharter.ProduceCharts(curveData[0]);
|
||||||
|
|
||||||
//Image.FromFile()
|
foreach (var chart in charts)
|
||||||
|
|
||||||
using (var ms = new MemoryStream())
|
|
||||||
{
|
{
|
||||||
charts.Skip(number).First().SaveImage(ms, format);
|
// 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);
|
ms.Seek(0, SeekOrigin.Begin);
|
||||||
|
yield return new Bitmap(ms);
|
||||||
return ms.ToArray();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Bitmap CombineBitmaps(IList<Bitmap> 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()
|
public ActionResult ChartSample()
|
||||||
{
|
{
|
||||||
var chart = new Chart
|
var chart = new Chart
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
@using LeafWeb.Web.Charter
|
@model int
|
||||||
@model int
|
|
||||||
|
|
||||||
@for (int i = 0; i < 8; i++)
|
<img src="@Url.Action("LeafCharts", new {leafInputId = Model})" alt="image" />
|
||||||
{
|
|
||||||
<img src="@Url.Action("LeafCharts", new {leafInputId = Model, number=@i})" alt="image" height="@LeafWebCharter.ChartHeight" width="@LeafWebCharter.ChartWidth" />
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user