Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Models/ChartModel.cs @ 6253

Last change on this file since 6253 was 6242, checked in by cfleisch, 13 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 HLWebOKBQueryPlugin.OKBQueryService;
6using System.Drawing;
7using System.Web.UI.DataVisualization.Charting;
8using System.Collections;
9
10namespace HLWebOKBQueryPlugin.Models {
11  public class ChartModel {
12
13
14
15    private Color DataPointColor = System.Drawing.Color.Black;
16    //private String xAxisValue;
17    //private String yAxisValue;
18    //private String sizeAxisValue;
19
20    public String xAxisValue {
21        get { return (HttpContext.Current.Session["xAxisValue"] == null) ? null : (String)HttpContext.Current.Session["xAxisValue"]; }
22        set { HttpContext.Current.Session["xAxisValue"] = value; }
23    }
24    public String yAxisValue {
25        get { return (HttpContext.Current.Session["yAxisValue"] == null) ? null : (String)HttpContext.Current.Session["yAxisValue"]; }
26        set { HttpContext.Current.Session["yAxisValue"] = value; }
27    }
28    public String sizeAxisValue {
29        get { return (HttpContext.Current.Session["sizeAxisValue"] == null) ? null : (String)HttpContext.Current.Session["sizeAxisValue"]; }
30        set { HttpContext.Current.Session["sizeAxisValue"] = value; }
31    }
32
33
34    private Dictionary<string, Dictionary<object, double>> CategoricalMap {
35      get;
36      set;
37    }
38
39    //public Run[] Runs {
40    //  set;
41    //  get;
42    //  // {
43    //  //UpdateComboBoxValues(value);
44
45    //  //}
46    //}
47
48    public Run[] Runs {
49        get { return (HttpContext.Current.Session["runs"] == null) ? null : (Run[])HttpContext.Current.Session["runs"]; }
50        set { HttpContext.Current.Session["runs"] = value; }
51    }
52
53    public Series Series { set; get; }
54
55    public void UpdateRunCollection(Run[] runs) {
56      this.Runs = runs;
57      UpdateComboBoxValues();
58      //
59    }
60
61    public List<string> valuesAxisY { get; set; }
62    public List<string> valuesAxisX { get; set; }
63    public List<string> bubbleSize { get; set; }
64
65
66
67    // COnstrucotr
68    public ChartModel() {
69
70      CategoricalMap = new Dictionary<string, Dictionary<object, double>>();
71
72      valuesAxisY = new List<string>();
73      valuesAxisX = new List<string>();
74      bubbleSize = new List<string>();
75      Series = new Series();
76      this.Series.ChartType = SeriesChartType.Bubble;
77    }
78
79
80
81    private double GetCategoricalValue(string columnName, string value) {
82      if (!this.CategoricalMap.ContainsKey(columnName)) {
83        this.CategoricalMap[columnName] = new Dictionary<object, double>();
84      }
85      if (!this.CategoricalMap[columnName].ContainsKey(value)) {
86        if (this.CategoricalMap[columnName].Values.Count == 0) {
87          this.CategoricalMap[columnName][value] = 1.0;
88        } else {
89          this.CategoricalMap[columnName][value] = this.CategoricalMap[columnName].Values.Max() + 1.0;
90        }
91      }
92      return this.CategoricalMap[columnName][value];
93    }
94
95    private double? GetValue(Run run, String columnName) {
96      if ((run == null) || string.IsNullOrEmpty(columnName)) {
97        return null;
98      }
99      Value value = run.ResultValues.FirstOrDefault(x => x.Name == columnName);
100
101      if (value == null) {
102        return null;
103      }
104
105      DoubleValue doubleValue = value as DoubleValue;
106      IntValue intValue = value as IntValue;
107      BoolValue boolValue = value as BoolValue;
108      FloatValue floatValue = value as FloatValue;
109      BinaryValue binaryValue = value as BinaryValue;
110      LongValue longValue = value as LongValue;
111      StringValue stringValue = value as StringValue;
112
113      double? ret = null;
114      if (doubleValue != null) {
115        if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value)) {
116          ret = doubleValue.Value;
117        }
118      } else if (intValue != null) {
119        ret = intValue.Value;
120      } else if (floatValue != null) {
121        ret = floatValue.Value;
122      } else if (longValue != null) {
123        ret = longValue.Value;
124      } else {
125        ret = GetCategoricalValue(columnName, value.ToString());
126      }
127      return ret;
128    }
129
130    private void AddDataPoint(Run run) {//, int pos) {
131      double? xValue;
132      double? yValue;
133      double? sValue;
134      // Series series = this.BubbleChart.Series[0];
135      // Series series = new Series();
136      xValue = GetValue(run, this.xAxisValue);
137      yValue = GetValue(run, this.yAxisValue);
138      sValue = GetValue(run, this.sizeAxisValue);
139
140      if (xValue.HasValue && yValue.HasValue && sValue.HasValue) {
141        xValue = xValue.Value;
142        yValue = yValue.Value;
143        sValue = sValue.Value;
144        double[] yValues = { (double)yValue, (double)sValue };
145        DataPoint point = new DataPoint(xValue.Value, yValues);
146        point.Tag = run;
147        point.Color = DataPointColor;
148        this.Series.Points.Add(point);
149      }
150    }
151
152    private void UpdateComboBoxValues() {
153      if (Runs == null) {
154        return;
155      }
156      ArrayList comboBoxValues = new ArrayList();
157      Value[] resultValues = null;
158      foreach (Run run in Runs) {
159        resultValues = run.ResultValues;
160        //comboBoxValues.Add(selectString);
161        foreach (Value v in resultValues) {
162          String resultValueName = v.Name.ToString();
163          if (!comboBoxValues.Contains(resultValueName)) {
164            comboBoxValues.Add(resultValueName);
165          }
166        }
167      }
168
169
170      this.valuesAxisY.Clear();
171      this.valuesAxisX.Clear();
172      this.bubbleSize.Clear();
173
174      foreach (String s in comboBoxValues) {
175        if (!valuesAxisY.Contains(s)) {
176          this.valuesAxisY.Add(s);
177          this.valuesAxisX.Add(s);
178          this.bubbleSize.Add(s);
179        }
180      }
181    }
182
183    private void UpdateDataPoints() {
184      Run[] runs = this.Runs;
185      Series series = this.Series;
186      series.Points.Clear();
187      if (runs != null) {
188        foreach (Run run in runs) {
189          this.AddDataPoint(run);
190        }
191        UpdateMarkerSize();
192      }
193    }
194
195    private void UpdateMarkerSize() {
196      double[] sizeValues = Series.Points.Select(p => p.YValues[1]).ToArray();
197      double minSizeValue = sizeValues.Min();
198      double maxSizeValue = sizeValues.Max();
199      //int sizeFaktor = int.Parse(bubbleSizeFactor.SelectedItem.Value);
200
201      for (int i = 0; i < sizeValues.Length; i++) {
202        DataPoint dataPoint = this.Series.Points[i];
203
204        double sizeRange = maxSizeValue - minSizeValue;
205        double relativeSize = dataPoint.YValues[1] - minSizeValue;
206
207        if (sizeRange > double.Epsilon) {
208          relativeSize /= sizeRange;
209        } else {
210          relativeSize = 1;
211        }
212        //int markerSize = (int)Math.Round(bubbleSizeFaktor * relativeSize);
213        //if (markerSize < 1) {
214        //  markerSize = 1;
215        //}
216        double[] yValues = dataPoint.YValues;
217        // double minBubbleSizeFactor = double.Parse(bubbleSizeFactor.Items[0].Value);
218        //int markerSize = (int)Math.Round(
219        //  (sizeFaktor - minBubbleSizeFactor) * relativeSize + minBubbleSizeFactor);
220        //yValues[1] = markerSize;
221        //dataPoint.MarkerSize = markerSize;
222        dataPoint.YValues = yValues;
223      }
224    }
225
226    public void UpdateBubbleChart(string yAxisValue, string xAxisValue, string sizeAxisValue) {
227      this.yAxisValue = yAxisValue;
228      this.xAxisValue = xAxisValue;
229      this.sizeAxisValue = sizeAxisValue;
230
231      UpdateDataPoints();
232    }
233
234    //protected void valuesAxisY_SelectedIndexChanged(object sender, EventArgs e)
235    //{
236    //    if (AllAxisSelected())
237    //    {
238    //        UpdateBubbleChart();
239    //    }
240    //}
241
242    //protected void valuesAxisX_SelectedIndexChanged(object sender, EventArgs e)
243    //{
244    //    if (AllAxisSelected())
245    //    {
246    //        UpdateBubbleChart();
247    //    }
248    //}
249
250    //protected void bubbleSize_SelectedIndexChanged(object sender, EventArgs e)
251    //{
252    //    if (AllAxisSelected())
253    //    {
254    //        UpdateBubbleChart();
255    //    }
256    //}
257
258    //protected void bubbleSizeFactor_SelectedIndexChanged(object sender, EventArgs e)
259    //{
260    //    if (AllAxisSelected())
261    //    {
262    //        UpdateBubbleChart();
263    //    }
264    //}
265
266    //private bool AllAxisSelected()
267    //{
268    //    return (((valuesAxisY.SelectedValue).CompareTo(selectString) != 0) &&
269    //        ((valuesAxisX.SelectedValue).CompareTo(selectString) != 0) &&
270    //        ((bubbleSize.SelectedValue).CompareTo(selectString) != 0));
271    //}
272  }
273}
Note: See TracBrowser for help on using the repository browser.