Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6303 was 6303, checked in by mjesner, 13 years ago

#1499 result page, styling, functionality (add/remove) for post actions

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