Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6165 was 6165, checked in by fruehrli, 12 years ago

#1503 BubbleChart without exception

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