Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Web/Controllers/ChartController.cs @ 9062

Last change on this file since 9062 was 9062, checked in by fschoepp, 11 years ago

#1888:
Backend changes:

  • Simplified job state detection (only one hive call will be made to detect all states now, instead of one additional call per job)
  • Reorganized classes (moved model classes into Model folder)

Website changes:

  • Website now heavily uses JavaScript to achieve better user experience
  • JavaScript degrades gracefully, except for plots
  • Tables: Added jquery-datatable-plugin to extend tables (pagination + search functionality)
  • OaaS-Website now uses the design of the HL websites (found in WebApplication branch)
  • Added jqplot to render zoomable line plots for HL-Datatables
  • Styling.js: Plots will be generated by using an ajax call; additional jquery-styling occurs within this file.
  • Added jquery-ui-1.9.2 which is capable of handling/rendering tabs, accordions and resizers.
File size: 7.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HeuristicLab.Services.Optimization.Web.Models;
7using HeuristicLab.Services.Optimization.ControllerService.Model;
8using System.Web.UI.DataVisualization.Charting;
9using System.Drawing;
10using System.IO;
11using System.Text;
12using System.Diagnostics;
13
14namespace 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}
Note: See TracBrowser for help on using the repository browser.