using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using HLWebOKBQueryPlugin.Models; using HLWebOKBQueryPlugin.Helpers; using HLWebOKBQueryPlugin.OKBQueryService; using System.Web.UI.DataVisualization.Charting; using System.Drawing; using System.IO; namespace HLWebOKBQueryPlugin.Controllers { public class ChartController : Controller { // // GET: /Chart/ public ActionResult UpdateBubbleChart(FormCollection collection) { String selectedAxisX = collection.Get("valuesAxisXCombobox"); String selectedAxisY = collection.Get("valuesAxisYCombobox"); String selectedBubbleSize = collection.Get("bubbleSizeCombobox"); Response.Write("axis x => " + selectedAxisX); Response.Write("axis y => " + selectedAxisY); Response.Write("size => " + selectedBubbleSize); ChartModel cm = new ChartModel(); // Later, we will get the runs from the session ... QueryServiceClient client = Query.GetClientFactory(); //long[] runIds = new long[104]; //for (int i = 0; i < 104; i++) { // runIds[i] = i; //} long[] runIds = new long[0]; CombinedFilter cf = (CombinedFilter)Session["Content"]; if (cf != null) runIds = client.GetRunIds(cf); Run[] runs = client.GetRuns(runIds, false); // Run[] runs = Session[""]; cm.Runs = runs; cm.UpdateRunCollection(runs); cm.UpdateBubbleChart(selectedAxisY, selectedAxisX, selectedBubbleSize); return View("Index",cm); } public FileResult CreateBubbleChart(ChartModel model) { //IList items = new List(); //items.Add(1); //items.Add(2); //items.Add(5); //items.Add(10); Chart chart = new Chart(); chart.Width = 700; chart.Height = 300; chart.BackColor = Color.FromArgb(211, 223, 240); chart.BorderlineDashStyle = ChartDashStyle.Solid; chart.BackSecondaryColor = Color.White; chart.BackGradientStyle = GradientStyle.TopBottom; chart.BorderlineWidth = 1; chart.Palette = ChartColorPalette.BrightPastel; chart.BorderlineColor = Color.FromArgb(26, 59, 105); chart.RenderType = RenderType.BinaryStreaming; chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss; chart.AntiAliasing = AntiAliasingStyles.All; chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal; chart.Titles.Add("Bubble Chart"); //chart.Legends.Add(CreateLegend()); chart.Series.Add(CreateSeries(model,SeriesChartType.Bubble)); chart.ChartAreas.Add(CreateChartArea()); MemoryStream ms = new MemoryStream(); chart.SaveImage(ms); return File(ms.GetBuffer(), @"image/png"); } private Dictionary> categoricalMap = new Dictionary>(); private Dictionary> CategoricalMap { get { return categoricalMap; } set { categoricalMap = value; } } 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; } public Series CreateSeries(ChartModel model, SeriesChartType chartType) { Series seriesDetail = new Series(); seriesDetail.Name = "Result Chart"; seriesDetail.IsValueShownAsLabel = false; seriesDetail.Color = Color.FromArgb(198, 99, 99); seriesDetail.ChartType = chartType; seriesDetail.BorderWidth = 2; DataPoint point; foreach (Run run in model.Runs) { double? xValue, yValue, sValue; xValue = GetValue(run, model.xAxisValue); yValue = GetValue(run, model.yAxisValue); sValue = GetValue(run, model.sizeAxisValue); if (xValue.HasValue && yValue.HasValue && sValue.HasValue) { xValue = xValue.Value; yValue = yValue.Value; sValue = sValue.Value; double[] yValues = { (double)yValue, (double)sValue }; point = new DataPoint(xValue.Value, yValues); point.Tag = run; point.MarkerStyle = MarkerStyle.Circle; point.Color = Color.Blue; ; seriesDetail.Points.Add(point); } } //foreach (int result in results) //{ // point = new DataPoint(); // point.AxisLabel = result.ToString(); // point.YValues = new double[] { (double)result }; // seriesDetail.Points.Add(point); //} seriesDetail.ChartArea = "Result Chart"; return seriesDetail; } public ChartArea CreateChartArea() { ChartArea chartArea = new ChartArea(); chartArea.Name = "Result Chart"; chartArea.BackColor = Color.Transparent; chartArea.AxisX.IsLabelAutoFit = false; chartArea.AxisY.IsLabelAutoFit = false; chartArea.AxisX.LabelStyle.Font = new Font("Verdana,Arial,Helvetica,sans-serif", 8F, FontStyle.Regular); chartArea.AxisY.LabelStyle.Font = new Font("Verdana,Arial,Helvetica,sans-serif", 8F, FontStyle.Regular); chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64); chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64); chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64); chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64); chartArea.AxisX.Interval = 1; return chartArea; } public ActionResult Index() { ChartModel cm = new ChartModel(); // Later, we will get the runs from the session ... QueryServiceClient client = Query.GetClientFactory(); long[] runIds = new long[0]; CombinedFilter cf = (CombinedFilter)Session["Content"]; if (cf != null) runIds = client.GetRunIds(cf); Run[] runs = client.GetRuns(runIds, false); cm.Runs = runs; cm.UpdateRunCollection(runs); return View(cm); } // [ChildActionOnly] public ActionResult BubbleChart() { ChartModel cm = new ChartModel(); //// Later, we will get the runs from the session ... //QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester"); //long[] runIds = new long[5]; //for (int i = 0; i < 5; i++) { // runIds[i] = i; //} //Run[] runs = client.GetRuns(runIds, false); ////cm.Runs = runs; //cm.UpdateRunCollection(runs); ////cm.UpdateBubbleChart(selectedAxisY, selectedAxisX, selectedBubbleSize); return PartialView("BubbleChart", cm); // name of usercontrol } //public ActionResult BoxPlot() //{ // return View(); //} } }