using System; using System.Collections.Generic; using System.Linq; using System.Web; using HLWebOKBQueryPlugin.OKBQueryService; using System.Drawing; using System.Web.UI.DataVisualization.Charting; using System.Collections; namespace HLWebOKBQueryPlugin.Models { public class ChartModel { private Color DataPointColor = System.Drawing.Color.Black; //private String xAxisValue; //private String yAxisValue; //private String sizeAxisValue; public String xAxisValue { get { return (HttpContext.Current.Session["xAxisValue"] == null) ? null : (String)HttpContext.Current.Session["xAxisValue"]; } set { HttpContext.Current.Session["xAxisValue"] = value; } } public String yAxisValue { get { return (HttpContext.Current.Session["yAxisValue"] == null) ? null : (String)HttpContext.Current.Session["yAxisValue"]; } set { HttpContext.Current.Session["yAxisValue"] = value; } } public String sizeAxisValue { get { return (HttpContext.Current.Session["sizeAxisValue"] == null) ? null : (String)HttpContext.Current.Session["sizeAxisValue"]; } set { HttpContext.Current.Session["sizeAxisValue"] = value; } } private Dictionary> CategoricalMap { get; set; } //public Run[] Runs { // set; // get; // // { // //UpdateComboBoxValues(value); // //} //} public Run[] Runs { get { return (HttpContext.Current.Session["runs"] == null) ? null : (Run[])HttpContext.Current.Session["runs"]; } set { HttpContext.Current.Session["runs"] = value; } } public Series Series { set; get; } public void UpdateRunCollection(Run[] runs) { this.Runs = runs; UpdateComboBoxValues(); // } public List valuesAxisY { get; set; } public List valuesAxisX { get; set; } public List bubbleSize { get; set; } // COnstrucotr public ChartModel() { CategoricalMap = new Dictionary>(); valuesAxisY = new List(); valuesAxisX = new List(); bubbleSize = new List(); Series = new Series(); this.Series.ChartType = SeriesChartType.Bubble; } private double GetCategoricalValue(string columnName, string value) { if (!this.CategoricalMap.ContainsKey(columnName)) { this.CategoricalMap[columnName] = new Dictionary(); } if (!this.CategoricalMap[columnName].ContainsKey(value)) { if (this.CategoricalMap[columnName].Values.Count == 0) { this.CategoricalMap[columnName][value] = 1.0; } else { this.CategoricalMap[columnName][value] = this.CategoricalMap[columnName].Values.Max() + 1.0; } } return this.CategoricalMap[columnName][value]; } private double? GetValue(Run run, String columnName) { if ((run == null) || string.IsNullOrEmpty(columnName)) { return null; } Value value = run.ResultValues.FirstOrDefault(x => x.Name == columnName); if (value == null) { return null; } DoubleValue doubleValue = value as DoubleValue; IntValue intValue = value as IntValue; BoolValue boolValue = value as BoolValue; FloatValue floatValue = value as FloatValue; BinaryValue binaryValue = value as BinaryValue; LongValue longValue = value as LongValue; StringValue stringValue = value as StringValue; double? ret = null; if (doubleValue != null) { if (!double.IsNaN(doubleValue.Value) && !double.IsInfinity(doubleValue.Value)) { ret = doubleValue.Value; } } else if (intValue != null) { ret = intValue.Value; } else if (floatValue != null) { ret = floatValue.Value; } else if (longValue != null) { ret = longValue.Value; } else { ret = GetCategoricalValue(columnName, value.ToString()); } return ret; } private void AddDataPoint(Run run) {//, int pos) { double? xValue; double? yValue; double? sValue; // Series series = this.BubbleChart.Series[0]; // Series series = new Series(); xValue = GetValue(run, this.xAxisValue); yValue = GetValue(run, this.yAxisValue); sValue = GetValue(run, this.sizeAxisValue); if (xValue.HasValue && yValue.HasValue && sValue.HasValue) { xValue = xValue.Value; yValue = yValue.Value; sValue = sValue.Value; double[] yValues = { (double)yValue, (double)sValue }; DataPoint point = new DataPoint(xValue.Value, yValues); point.Tag = run; point.Color = DataPointColor; this.Series.Points.Add(point); } } private void UpdateComboBoxValues() { if (Runs == null) { return; } ArrayList comboBoxValues = new ArrayList(); Value[] resultValues = null; foreach (Run run in Runs) { resultValues = run.ResultValues; //comboBoxValues.Add(selectString); foreach (Value v in resultValues) { String resultValueName = v.Name.ToString(); if (!comboBoxValues.Contains(resultValueName)) { comboBoxValues.Add(resultValueName); } } } this.valuesAxisY.Clear(); this.valuesAxisX.Clear(); this.bubbleSize.Clear(); foreach (String s in comboBoxValues) { if (!valuesAxisY.Contains(s)) { this.valuesAxisY.Add(s); this.valuesAxisX.Add(s); this.bubbleSize.Add(s); } } } private void UpdateDataPoints() { Run[] runs = this.Runs; Series series = this.Series; series.Points.Clear(); if (runs != null) { foreach (Run run in runs) { this.AddDataPoint(run); } UpdateMarkerSize(); } } private void UpdateMarkerSize() { double[] sizeValues = Series.Points.Select(p => p.YValues[1]).ToArray(); double minSizeValue = 0; double maxSizeValue = 0; if (sizeValues.Length > 0) { minSizeValue = sizeValues.Min(); maxSizeValue = sizeValues.Max(); } //int sizeFaktor = int.Parse(bubbleSizeFactor.SelectedItem.Value); for (int i = 0; i < sizeValues.Length; i++) { DataPoint dataPoint = this.Series.Points[i]; double sizeRange = maxSizeValue - minSizeValue; double relativeSize = dataPoint.YValues[1] - minSizeValue; if (sizeRange > double.Epsilon) { relativeSize /= sizeRange; } else { relativeSize = 1; } //int markerSize = (int)Math.Round(bubbleSizeFaktor * relativeSize); //if (markerSize < 1) { // markerSize = 1; //} double[] yValues = dataPoint.YValues; // double minBubbleSizeFactor = double.Parse(bubbleSizeFactor.Items[0].Value); //int markerSize = (int)Math.Round( // (sizeFaktor - minBubbleSizeFactor) * relativeSize + minBubbleSizeFactor); //yValues[1] = markerSize; //dataPoint.MarkerSize = markerSize; dataPoint.YValues = yValues; } } public void UpdateBubbleChart(string yAxisValue, string xAxisValue, string sizeAxisValue) { this.yAxisValue = yAxisValue; this.xAxisValue = xAxisValue; this.sizeAxisValue = sizeAxisValue; UpdateDataPoints(); } //protected void valuesAxisY_SelectedIndexChanged(object sender, EventArgs e) //{ // if (AllAxisSelected()) // { // UpdateBubbleChart(); // } //} //protected void valuesAxisX_SelectedIndexChanged(object sender, EventArgs e) //{ // if (AllAxisSelected()) // { // UpdateBubbleChart(); // } //} //protected void bubbleSize_SelectedIndexChanged(object sender, EventArgs e) //{ // if (AllAxisSelected()) // { // UpdateBubbleChart(); // } //} //protected void bubbleSizeFactor_SelectedIndexChanged(object sender, EventArgs e) //{ // if (AllAxisSelected()) // { // UpdateBubbleChart(); // } //} //private bool AllAxisSelected() //{ // return (((valuesAxisY.SelectedValue).CompareTo(selectString) != 0) && // ((valuesAxisX.SelectedValue).CompareTo(selectString) != 0) && // ((bubbleSize.SelectedValue).CompareTo(selectString) != 0)); //} } }