Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6318 was 6318, checked in by cfleisch, 13 years ago

#1499 styling filter and bubblechart, filter bugfixes

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