1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using HeuristicLab.Services.Optimization.Web.Models;
|
---|
7 | using HeuristicLab.Services.Optimization.ControllerService.Model;
|
---|
8 | using System.Web.UI.DataVisualization.Charting;
|
---|
9 | using System.Drawing;
|
---|
10 | using System.IO;
|
---|
11 | using System.Text;
|
---|
12 | using System.Diagnostics;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Services.Optimization.Web.Controllers
|
---|
15 | {
|
---|
16 | public class ChartController : Controller
|
---|
17 | {
|
---|
18 | //
|
---|
19 | // GET: /Chart/GenerateChart/
|
---|
20 |
|
---|
21 | public ActionResult GenerateChart(int index, string parameterName, string chartType)
|
---|
22 | {
|
---|
23 | var model = Session["jobDetails"] as JobDetailsModel;
|
---|
24 | var parameter = (from result in model.Runs[index].Results where result.Value.Name == parameterName select result.Value).FirstOrDefault();
|
---|
25 | var chartTypeObj = (SeriesChartType) Enum.Parse(typeof(SeriesChartType), chartType);
|
---|
26 | var matrix = parameter as DecimalMatrix;
|
---|
27 | var chart = new Chart();
|
---|
28 | chart.Width = 1024;
|
---|
29 | chart.Height = 768;
|
---|
30 | chart.BackColor = Color.FromArgb(211, 223, 240);
|
---|
31 | chart.BorderlineDashStyle = ChartDashStyle.Solid;
|
---|
32 | chart.BackSecondaryColor = Color.White;
|
---|
33 | chart.BackGradientStyle = GradientStyle.TopBottom;
|
---|
34 | chart.BorderlineWidth = 1;
|
---|
35 | chart.Palette = ChartColorPalette.BrightPastel;
|
---|
36 | chart.BorderlineColor = Color.FromArgb(26, 59, 105);
|
---|
37 | chart.RenderType = RenderType.BinaryStreaming;
|
---|
38 | chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
|
---|
39 | chart.AntiAliasing = AntiAliasingStyles.All;
|
---|
40 | chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;
|
---|
41 | chart.Titles.Add(CreateTitle(parameterName));
|
---|
42 | chart.Legends.Add(CreateLegend());
|
---|
43 | CreateSeries(chart, matrix, chartTypeObj);
|
---|
44 |
|
---|
45 |
|
---|
46 | using (var stream = new MemoryStream()) {
|
---|
47 | string img = "<img alt='Chart' src='data:image/png;base64,{0}' usemap='#ImageMap" + parameterName + "'/>";
|
---|
48 | chart.SaveImage(stream, ChartImageFormat.Png);
|
---|
49 | string encoded = Convert.ToBase64String(stream.ToArray());
|
---|
50 | var resultString = new StringBuilder();
|
---|
51 | string result = string.Format(img, encoded);
|
---|
52 | string imageMap = chart.GetHtmlImageMap("ImageMap" + parameterName);
|
---|
53 | resultString.Append(result);
|
---|
54 | resultString.Append(imageMap);
|
---|
55 | return Content(resultString.ToString());
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private Title CreateTitle(string titleString) {
|
---|
60 | Title title = new Title();
|
---|
61 | title.Text = titleString;
|
---|
62 | title.ShadowColor = Color.FromArgb(32, 0, 0, 0);
|
---|
63 | title.Font = new Font("Trebuchet MS", 14F, FontStyle.Bold);
|
---|
64 | title.ShadowOffset = 3;
|
---|
65 | title.ForeColor = Color.FromArgb(26, 59, 105);
|
---|
66 | return title;
|
---|
67 | }
|
---|
68 |
|
---|
69 | private Legend CreateLegend() {
|
---|
70 | Legend legend = new Legend("Legend");
|
---|
71 | legend.ShadowColor = Color.FromArgb(32, 0, 0, 0);
|
---|
72 | legend.Font = new Font("Trebuchet MS", 14F, FontStyle.Bold);
|
---|
73 | legend.ShadowOffset = 3;
|
---|
74 | legend.ForeColor = Color.FromArgb(26, 59, 105);
|
---|
75 | return legend;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private KnownColor GetRandomColor(List<KnownColor> ignore) {
|
---|
79 | Random randomGen = new Random();
|
---|
80 | //KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
|
---|
81 | KnownColor[] names = { KnownColor.Blue, KnownColor.Red, KnownColor.Green, KnownColor.Yellow, KnownColor.Violet, KnownColor.Purple, KnownColor.Orange };
|
---|
82 | KnownColor randomColorName = KnownColor.Blue;
|
---|
83 | do {
|
---|
84 | randomColorName = names[randomGen.Next(names.Length)];
|
---|
85 | if (ignore.Count == names.Count())
|
---|
86 | // prevent deadlock
|
---|
87 | break;
|
---|
88 | } while (ignore.Contains(randomColorName));
|
---|
89 | return randomColorName;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void CreateSeries(Chart chart, DecimalMatrix matrix, SeriesChartType chartType) {
|
---|
93 | DataPoint point;
|
---|
94 | var usedColors = new List<KnownColor>();
|
---|
95 | for (int i = 0; i < matrix.Value.Length; i++ ) {
|
---|
96 | Series seriesDetail = new Series();
|
---|
97 | seriesDetail.Name = matrix.RowNames[i];
|
---|
98 | seriesDetail.IsValueShownAsLabel = false;
|
---|
99 | var kc = GetRandomColor(usedColors);
|
---|
100 | usedColors.Add(kc);
|
---|
101 | seriesDetail.Url = "#";
|
---|
102 | seriesDetail.Color = Color.FromKnownColor(kc);
|
---|
103 | seriesDetail.ChartType = chartType;
|
---|
104 | seriesDetail.BorderWidth = 2;
|
---|
105 | seriesDetail.ChartArea = "Area";
|
---|
106 | for (int j = 0; j < matrix.Value[i].Length; j++) {
|
---|
107 | point = new DataPoint();
|
---|
108 | point.XValue = j;
|
---|
109 | point.YValues = new double[] { matrix.Value[i][j] };
|
---|
110 | //point.AxisLabel = j + "";
|
---|
111 | seriesDetail.Points.Add(point);
|
---|
112 |
|
---|
113 | }
|
---|
114 | chart.Series.Add(seriesDetail);
|
---|
115 | }
|
---|
116 | chart.ChartAreas.Add(CreateChartArea("Area"));
|
---|
117 | }
|
---|
118 |
|
---|
119 | private ChartArea CreateChartArea(string name) {
|
---|
120 | ChartArea chartArea = new ChartArea();
|
---|
121 | chartArea.Name = name;
|
---|
122 | chartArea.BackColor = Color.Transparent;
|
---|
123 | chartArea.AxisX.IsLabelAutoFit = false;
|
---|
124 | chartArea.AxisY.IsLabelAutoFit = false;
|
---|
125 | chartArea.AxisX.LabelStyle.Font =
|
---|
126 | new Font("Verdana,Arial,Helvetica,sans-serif",
|
---|
127 | 8F, FontStyle.Regular);
|
---|
128 | chartArea.AxisY.LabelStyle.Font =
|
---|
129 | new Font("Verdana,Arial,Helvetica,sans-serif",
|
---|
130 | 8F, FontStyle.Regular);
|
---|
131 | chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
|
---|
132 | chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
|
---|
133 | chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
|
---|
134 | chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
|
---|
135 | //chartArea.AxisX.Interval = 1;
|
---|
136 | return chartArea;
|
---|
137 | }
|
---|
138 |
|
---|
139 | [HttpGet]
|
---|
140 | public JsonResult GetMatrixContentForGraph(int run, int parameterOffset, bool showMarker) {
|
---|
141 | try {
|
---|
142 | var model = Session["jobDetails"] as JobDetailsModel;
|
---|
143 | if (model != null) {
|
---|
144 | var parameter = model.Runs[run].Results[parameterOffset].Value as DecimalMatrix;
|
---|
145 | var plotableData = new PlotableData() { ShowMarker = showMarker };
|
---|
146 | for (var i = 0; i < parameter.Value.Length; i++) {
|
---|
147 | plotableData.Series.Add(new SeriesEntry() { Label = parameter.RowNames[i], Values = parameter.Value[i] });
|
---|
148 | }
|
---|
149 | return Json(plotableData, JsonRequestBehavior.AllowGet);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | catch (Exception e) {
|
---|
153 | Trace.TraceError(e.Message);
|
---|
154 | }
|
---|
155 | Response.StatusCode = 400;
|
---|
156 | Response.Status = "Session timed out or ";
|
---|
157 | return Json("error", JsonRequestBehavior.AllowGet);
|
---|
158 | }
|
---|
159 |
|
---|
160 | [HttpGet]
|
---|
161 | public JsonResult GetDatatableContent(string datatable, string datarow) {
|
---|
162 | try {
|
---|
163 | var model = Session["jobDetails"] as JobDetailsModel;
|
---|
164 | var plotableData = new PlotableData();
|
---|
165 |
|
---|
166 | for (var i=0; i < model.Runs.Count; i++) {
|
---|
167 | foreach (var result in model.Runs[i].Results) {
|
---|
168 | if (result.Value.Name == datatable) {
|
---|
169 | var matrix = result.Value as DecimalMatrix;
|
---|
170 | var index = Array.IndexOf(matrix.RowNames, datarow);
|
---|
171 | plotableData.Series.Add(
|
---|
172 | new SeriesEntry() {
|
---|
173 | Label = "" + (i + 1) + ". Run - " + datatable + " - " + datarow,
|
---|
174 | Values = matrix.Value[index]
|
---|
175 | }
|
---|
176 | );
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | return Json(plotableData, JsonRequestBehavior.AllowGet);
|
---|
183 | }
|
---|
184 | catch (Exception e) {
|
---|
185 | Trace.TraceError(e.Message);
|
---|
186 | }
|
---|
187 | Response.StatusCode = 400;
|
---|
188 | Response.Status = "Session timed out";
|
---|
189 | return Json("error", JsonRequestBehavior.AllowGet);
|
---|
190 | }
|
---|
191 |
|
---|
192 | [HttpGet]
|
---|
193 | public JsonResult GetDatatables() {
|
---|
194 | try {
|
---|
195 | var model = Session["jobDetails"] as JobDetailsModel;
|
---|
196 | var dts = new DatatableContainer();
|
---|
197 | var datatables = new List<string>();
|
---|
198 | foreach (var param in model.Runs[0].Results) {
|
---|
199 | if (param.Value is DecimalMatrix) {
|
---|
200 | datatables.Add(param.Value.Name);
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | dts.Datatables = datatables.ToArray();
|
---|
205 |
|
---|
206 | return Json(dts, JsonRequestBehavior.AllowGet);
|
---|
207 | }
|
---|
208 | catch (Exception e) {
|
---|
209 | Trace.TraceError(e.Message);
|
---|
210 | }
|
---|
211 | Response.StatusCode = 400;
|
---|
212 | Response.Status = "Session timed out or ";
|
---|
213 | return Json("error", JsonRequestBehavior.AllowGet);
|
---|
214 | }
|
---|
215 |
|
---|
216 | [HttpGet]
|
---|
217 | public JsonResult GetRowNames(string datatable) {
|
---|
218 | var model = Session["jobDetails"] as JobDetailsModel;
|
---|
219 | try {
|
---|
220 | foreach (var param in model.Runs[0].Results) {
|
---|
221 | if (param.Value.Name == datatable) {
|
---|
222 | var matrix = param.Value as DecimalMatrix;
|
---|
223 |
|
---|
224 | var rowNames = new RowNameContainer() {
|
---|
225 | RowNames = new List<string>(matrix.RowNames)
|
---|
226 | };
|
---|
227 | return Json(rowNames, JsonRequestBehavior.AllowGet);
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 | catch(Exception ex) {
|
---|
232 | Trace.WriteLine(ex.Message);
|
---|
233 | }
|
---|
234 | Response.StatusCode = 400;
|
---|
235 | Response.Status = "Session timed out";
|
---|
236 | return Json("error", JsonRequestBehavior.AllowGet);
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|