Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/ChartController.cs @ 6242

Last change on this file since 6242 was 6242, checked in by cfleisch, 12 years ago

#1503 data of runs is now shown correctly in chart

File size: 8.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HLWebOKBQueryPlugin.Models;
7using HLWebOKBQueryPlugin.Helpers;
8using HLWebOKBQueryPlugin.OKBQueryService;
9using System.Web.UI.DataVisualization.Charting;
10using System.Drawing;
11using System.IO;
12
13namespace HLWebOKBQueryPlugin.Controllers {
14  public class ChartController : Controller {
15    //
16    // GET: /Chart/
17
18
19    public ActionResult UpdateBubbleChart(FormCollection collection) {
20
21
22      String selectedAxisX = collection.Get("valuesAxisXCombobox");
23      String selectedAxisY = collection.Get("valuesAxisYCombobox");
24      String selectedBubbleSize = collection.Get("bubbleSizeCombobox");
25
26      Response.Write("axis x => " + selectedAxisX);
27      Response.Write("axis y => " + selectedAxisY);
28      Response.Write("size => " + selectedBubbleSize);
29
30      ChartModel cm = new ChartModel();
31
32      // Later, we will get the runs from the session ...
33      QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
34
35      long[] runIds = new long[104];
36      for (int i = 0; i < 104; i++) {
37        runIds[i] = i;
38      }
39      Run[] runs = client.GetRuns(runIds, false);
40      //cm.Runs = runs;
41      cm.UpdateRunCollection(runs);
42      cm.UpdateBubbleChart(selectedAxisY, selectedAxisX, selectedBubbleSize);
43
44      return View("Index",cm);
45    }
46
47
48
49    public FileResult CreateBubbleChart(ChartModel model)
50    {
51        //IList<int> items = new List<int>();
52        //items.Add(1);
53        //items.Add(2);
54        //items.Add(5);
55        //items.Add(10);
56
57        Chart chart = new Chart();
58        chart.Width = 700;
59        chart.Height = 300;
60        chart.BackColor = Color.FromArgb(211, 223, 240);
61        chart.BorderlineDashStyle = ChartDashStyle.Solid;
62        chart.BackSecondaryColor = Color.White;
63        chart.BackGradientStyle = GradientStyle.TopBottom;
64        chart.BorderlineWidth = 1;
65        chart.Palette = ChartColorPalette.BrightPastel;
66        chart.BorderlineColor = Color.FromArgb(26, 59, 105);
67        chart.RenderType = RenderType.BinaryStreaming;
68        chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
69        chart.AntiAliasing = AntiAliasingStyles.All;
70        chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;
71        chart.Titles.Add("Bubble Chart");
72        //chart.Legends.Add(CreateLegend());
73        chart.Series.Add(CreateSeries(model,SeriesChartType.Bubble));
74        chart.ChartAreas.Add(CreateChartArea());
75
76
77        MemoryStream ms = new MemoryStream();
78        chart.SaveImage(ms);
79        return File(ms.GetBuffer(), @"image/png");
80    }
81
82    private Dictionary<string, Dictionary<object, double>> categoricalMap = new Dictionary<string,Dictionary<object,double>>();
83    private Dictionary<string, Dictionary<object, double>> CategoricalMap {
84        get { return categoricalMap; }
85        set { categoricalMap = value; }
86    }
87
88    private double GetCategoricalValue(string columnName, string value) {
89        if (!this.CategoricalMap.ContainsKey(columnName)) {
90            this.CategoricalMap[columnName] = new Dictionary<object, double>();
91        }
92        if (!this.CategoricalMap[columnName].ContainsKey(value)) {
93            if (this.CategoricalMap[columnName].Values.Count == 0) {
94                this.CategoricalMap[columnName][value] = 1.0;
95            }
96            else {
97                this.CategoricalMap[columnName][value] = this.CategoricalMap[columnName].Values.Max() + 1.0;
98            }
99        }
100        return this.CategoricalMap[columnName][value];
101    }
102
103    private double? GetValue(Run run, String columnName) {
104        if ((run == null) || string.IsNullOrEmpty(columnName)) {
105            return null;
106        }
107        Value value = run.ResultValues.FirstOrDefault(x => x.Name == columnName);
108
109        if (value == null) {
110            return null;
111        }
112
113        DoubleValue doubleValue = value as DoubleValue;
114        IntValue intValue = value as IntValue;
115        BoolValue boolValue = value as BoolValue;
116        FloatValue floatValue = value as FloatValue;
117        BinaryValue binaryValue = value as BinaryValue;
118        LongValue longValue = value as LongValue;
119        StringValue stringValue = value as StringValue;
120
121        double? ret = null;
122        if (doubleValue != null) {
123            if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value)) {
124                ret = doubleValue.Value;
125            }
126        }
127        else if (intValue != null) {
128            ret = intValue.Value;
129        }
130        else if (floatValue != null) {
131            ret = floatValue.Value;
132        }
133        else if (longValue != null) {
134            ret = longValue.Value;
135        }
136        else {
137            ret = GetCategoricalValue(columnName, value.ToString());
138        }
139        return ret;
140    }
141
142    public Series CreateSeries(ChartModel model, SeriesChartType chartType)
143    {
144        Series seriesDetail = new Series();
145        seriesDetail.Name = "Result Chart";
146        seriesDetail.IsValueShownAsLabel = false;
147        seriesDetail.Color = Color.FromArgb(198, 99, 99);
148        seriesDetail.ChartType = chartType;
149        seriesDetail.BorderWidth = 2;
150        DataPoint point;
151
152        foreach (Run run in model.Runs) {
153            double? xValue, yValue, sValue;
154            xValue = GetValue(run, model.xAxisValue);
155            yValue = GetValue(run, model.yAxisValue);
156            sValue = GetValue(run, model.sizeAxisValue);
157
158            if (xValue.HasValue && yValue.HasValue && sValue.HasValue) {
159                xValue = xValue.Value;
160                yValue = yValue.Value;
161                sValue = sValue.Value;
162                double[] yValues = { (double)yValue, (double)sValue };
163                point = new DataPoint(xValue.Value, yValues);
164                point.Tag = run;
165                point.MarkerStyle = MarkerStyle.Circle;
166                point.Color = Color.Blue; ;
167                seriesDetail.Points.Add(point);
168            }
169        }
170
171        //foreach (int result in results)
172        //{
173        //    point = new DataPoint();
174        //    point.AxisLabel = result.ToString();
175        //    point.YValues = new double[] { (double)result };
176        //    seriesDetail.Points.Add(point);
177        //}
178        seriesDetail.ChartArea = "Result Chart";
179        return seriesDetail;
180    }
181
182
183
184    public ChartArea CreateChartArea()
185    {
186        ChartArea chartArea = new ChartArea();
187        chartArea.Name = "Result Chart";
188        chartArea.BackColor = Color.Transparent;
189        chartArea.AxisX.IsLabelAutoFit = false;
190        chartArea.AxisY.IsLabelAutoFit = false;
191        chartArea.AxisX.LabelStyle.Font =
192           new Font("Verdana,Arial,Helvetica,sans-serif",
193                    8F, FontStyle.Regular);
194        chartArea.AxisY.LabelStyle.Font =
195           new Font("Verdana,Arial,Helvetica,sans-serif",
196                    8F, FontStyle.Regular);
197        chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
198        chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
199        chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
200        chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
201        chartArea.AxisX.Interval = 1;
202
203        return chartArea;
204    }
205
206
207
208
209    public ActionResult Index() {
210      ChartModel cm = new ChartModel();
211
212      // Later, we will get the runs from the session ...
213      QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
214
215      long[] runIds = new long[5];
216      for (int i = 0; i < 5; i++) {
217        runIds[i] = i;
218      }
219      Run[] runs = client.GetRuns(runIds, false);
220
221      cm.UpdateRunCollection(runs);
222
223
224      return View(cm);
225    }
226
227    // [ChildActionOnly]
228    public ActionResult BubbleChart() {
229      ChartModel cm = new ChartModel();
230
231      //// Later, we will get the runs from the session ...
232      //QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
233
234      //long[] runIds = new long[5];
235      //for (int i = 0; i < 5; i++) {
236      //    runIds[i] = i;
237      //}
238      //Run[] runs = client.GetRuns(runIds, false);
239      ////cm.Runs = runs;
240      //cm.UpdateRunCollection(runs);
241      ////cm.UpdateBubbleChart(selectedAxisY, selectedAxisX, selectedBubbleSize);
242
243      return PartialView("BubbleChart", cm); // name of usercontrol
244    }
245
246
247    //public ActionResult BoxPlot()
248    //{
249    //    return View();
250    //}
251
252  }
253}
Note: See TracBrowser for help on using the repository browser.