1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using HLWebOKBQueryPlugin.OKBQueryService;
|
---|
6 | using System.Drawing;
|
---|
7 | using System.Web.UI.DataVisualization.Charting;
|
---|
8 | using System.Collections;
|
---|
9 |
|
---|
10 | namespace 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 | }
|
---|
89 | else {
|
---|
90 | this.CategoricalMap[columnName][value] = this.CategoricalMap[columnName].Values.Max() + 1.0;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return this.CategoricalMap[columnName][value];
|
---|
94 | }
|
---|
95 |
|
---|
96 | private double? GetValue(Run run, String columnName) {
|
---|
97 | if ((run == null) || string.IsNullOrEmpty(columnName)) {
|
---|
98 | return null;
|
---|
99 | }
|
---|
100 | Value value = run.ResultValues.FirstOrDefault(x => x.Name == columnName);
|
---|
101 |
|
---|
102 | if (value == null) {
|
---|
103 | return null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | DoubleValue doubleValue = value as DoubleValue;
|
---|
107 | IntValue intValue = value as IntValue;
|
---|
108 | BoolValue boolValue = value as BoolValue;
|
---|
109 | FloatValue floatValue = value as FloatValue;
|
---|
110 | BinaryValue binaryValue = value as BinaryValue;
|
---|
111 | LongValue longValue = value as LongValue;
|
---|
112 | StringValue stringValue = value as StringValue;
|
---|
113 |
|
---|
114 | double? ret = null;
|
---|
115 | if (doubleValue != null) {
|
---|
116 | if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value)) {
|
---|
117 | ret = doubleValue.Value;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | else if (intValue != null) {
|
---|
121 | ret = intValue.Value;
|
---|
122 | }
|
---|
123 | else if (floatValue != null) {
|
---|
124 | ret = floatValue.Value;
|
---|
125 | }
|
---|
126 | else if (longValue != null) {
|
---|
127 | ret = longValue.Value;
|
---|
128 | }
|
---|
129 | else {
|
---|
130 | ret = GetCategoricalValue(columnName, value.ToString());
|
---|
131 | }
|
---|
132 | return ret;
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void AddDataPoint(Run run) {//, int pos) {
|
---|
136 | double? xValue;
|
---|
137 | double? yValue;
|
---|
138 | double? sValue;
|
---|
139 | // Series series = this.BubbleChart.Series[0];
|
---|
140 | // Series series = new Series();
|
---|
141 | xValue = GetValue(run, this.xAxisValue);
|
---|
142 | yValue = GetValue(run, this.yAxisValue);
|
---|
143 | sValue = GetValue(run, this.sizeAxisValue);
|
---|
144 |
|
---|
145 | if (xValue.HasValue && yValue.HasValue && sValue.HasValue) {
|
---|
146 | xValue = xValue.Value;
|
---|
147 | yValue = yValue.Value;
|
---|
148 | sValue = sValue.Value;
|
---|
149 | double[] yValues = { (double)yValue, (double)sValue };
|
---|
150 | DataPoint point = new DataPoint(xValue.Value, yValues);
|
---|
151 | point.Tag = run;
|
---|
152 | point.Color = DataPointColor;
|
---|
153 | this.Series.Points.Add(point);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void UpdateComboBoxValues() {
|
---|
158 | if (Runs == null) {
|
---|
159 | return;
|
---|
160 | }
|
---|
161 | ArrayList comboBoxValues = new ArrayList();
|
---|
162 | Value[] resultValues = null;
|
---|
163 | foreach (Run run in Runs) {
|
---|
164 | resultValues = run.ResultValues;
|
---|
165 | //comboBoxValues.Add(selectString);
|
---|
166 | foreach (Value v in resultValues) {
|
---|
167 | String resultValueName = v.Name.ToString();
|
---|
168 | if (!comboBoxValues.Contains(resultValueName)) {
|
---|
169 | comboBoxValues.Add(resultValueName);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | this.valuesAxisY.Clear();
|
---|
176 | this.valuesAxisX.Clear();
|
---|
177 | this.bubbleSize.Clear();
|
---|
178 |
|
---|
179 | foreach (String s in comboBoxValues) {
|
---|
180 | if (!valuesAxisY.Contains(s)) {
|
---|
181 | this.valuesAxisY.Add(s);
|
---|
182 | this.valuesAxisX.Add(s);
|
---|
183 | this.bubbleSize.Add(s);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | private void UpdateDataPoints() {
|
---|
189 | Run[] runs = this.Runs;
|
---|
190 | Series series = this.Series;
|
---|
191 | series.Points.Clear();
|
---|
192 | if (runs != null) {
|
---|
193 | foreach (Run run in runs) {
|
---|
194 | this.AddDataPoint(run);
|
---|
195 | }
|
---|
196 | UpdateMarkerSize();
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void UpdateMarkerSize() {
|
---|
201 | double[] sizeValues = Series.Points.Select(p => p.YValues[1]).ToArray();
|
---|
202 | double minSizeValue = 0;
|
---|
203 | double maxSizeValue = 0;
|
---|
204 | if (sizeValues.Length > 0) {
|
---|
205 | minSizeValue = sizeValues.Min();
|
---|
206 | maxSizeValue = sizeValues.Max();
|
---|
207 | }
|
---|
208 | //int sizeFaktor = int.Parse(bubbleSizeFactor.SelectedItem.Value);
|
---|
209 |
|
---|
210 | for (int i = 0; i < sizeValues.Length; i++) {
|
---|
211 | DataPoint dataPoint = this.Series.Points[i];
|
---|
212 |
|
---|
213 | double sizeRange = maxSizeValue - minSizeValue;
|
---|
214 | double relativeSize = dataPoint.YValues[1] - minSizeValue;
|
---|
215 |
|
---|
216 | if (sizeRange > double.Epsilon) {
|
---|
217 | relativeSize /= sizeRange;
|
---|
218 | }
|
---|
219 | else {
|
---|
220 | relativeSize = 1;
|
---|
221 | }
|
---|
222 | //int markerSize = (int)Math.Round(bubbleSizeFaktor * relativeSize);
|
---|
223 | //if (markerSize < 1) {
|
---|
224 | // markerSize = 1;
|
---|
225 | //}
|
---|
226 | double[] yValues = dataPoint.YValues;
|
---|
227 | // double minBubbleSizeFactor = double.Parse(bubbleSizeFactor.Items[0].Value);
|
---|
228 | //int markerSize = (int)Math.Round(
|
---|
229 | // (sizeFaktor - minBubbleSizeFactor) * relativeSize + minBubbleSizeFactor);
|
---|
230 | //yValues[1] = markerSize;
|
---|
231 | //dataPoint.MarkerSize = markerSize;
|
---|
232 | dataPoint.YValues = yValues;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | public void UpdateBubbleChart(string yAxisValue, string xAxisValue, string sizeAxisValue) {
|
---|
237 | this.yAxisValue = yAxisValue;
|
---|
238 | this.xAxisValue = xAxisValue;
|
---|
239 | this.sizeAxisValue = sizeAxisValue;
|
---|
240 |
|
---|
241 | UpdateDataPoints();
|
---|
242 | }
|
---|
243 |
|
---|
244 | //protected void valuesAxisY_SelectedIndexChanged(object sender, EventArgs e)
|
---|
245 | //{
|
---|
246 | // if (AllAxisSelected())
|
---|
247 | // {
|
---|
248 | // UpdateBubbleChart();
|
---|
249 | // }
|
---|
250 | //}
|
---|
251 |
|
---|
252 | //protected void valuesAxisX_SelectedIndexChanged(object sender, EventArgs e)
|
---|
253 | //{
|
---|
254 | // if (AllAxisSelected())
|
---|
255 | // {
|
---|
256 | // UpdateBubbleChart();
|
---|
257 | // }
|
---|
258 | //}
|
---|
259 |
|
---|
260 | //protected void bubbleSize_SelectedIndexChanged(object sender, EventArgs e)
|
---|
261 | //{
|
---|
262 | // if (AllAxisSelected())
|
---|
263 | // {
|
---|
264 | // UpdateBubbleChart();
|
---|
265 | // }
|
---|
266 | //}
|
---|
267 |
|
---|
268 | //protected void bubbleSizeFactor_SelectedIndexChanged(object sender, EventArgs e)
|
---|
269 | //{
|
---|
270 | // if (AllAxisSelected())
|
---|
271 | // {
|
---|
272 | // UpdateBubbleChart();
|
---|
273 | // }
|
---|
274 | //}
|
---|
275 |
|
---|
276 | //private bool AllAxisSelected()
|
---|
277 | //{
|
---|
278 | // return (((valuesAxisY.SelectedValue).CompareTo(selectString) != 0) &&
|
---|
279 | // ((valuesAxisX.SelectedValue).CompareTo(selectString) != 0) &&
|
---|
280 | // ((bubbleSize.SelectedValue).CompareTo(selectString) != 0));
|
---|
281 | //}
|
---|
282 | }
|
---|
283 | } |
---|