Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebApplication/MVC2/HLWebOKBQueryPlugin/Controllers/ChartController.cs @ 6171

Last change on this file since 6171 was 6171, checked in by mjesner, 12 years ago

#1503 added controller for creating the image

File size: 5.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using HLWebOKBQueryPlugin.Models;
7using HLWebOKBQueryPlugin.Helpers;
8using HLWebOKBQueryPlugin.OKBQueryService;
9using System.Web.UI.DataVisualization.Charting;
10using System.Drawing;
11using System.IO;
12
13namespace HLWebOKBQueryPlugin.Controllers {
14  public class ChartController : Controller {
15    //
16    // GET: /Chart/
17
18
19    public ActionResult UpdateBubbleChart(FormCollection collection) {
20
21
22      String selectedAxisX = collection.Get("valuesAxisXCombobox");
23      String selectedAxisY = collection.Get("valuesAxisYCombobox");
24      String selectedBubbleSize = collection.Get("bubbleSizeCombobox");
25
26      Response.Write("axis x => " + selectedAxisX);
27      Response.Write("axis y => " + selectedAxisY);
28      Response.Write("size => " + selectedBubbleSize);
29
30      ChartModel cm = new ChartModel();
31
32      // Later, we will get the runs from the session ...
33      QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
34
35      long[] runIds = new long[5];
36      for (int i = 0; i < 5; i++) {
37        runIds[i] = i;
38      }
39      Run[] runs = client.GetRuns(runIds, false);
40
41      cm.UpdateRunCollection(runs);
42      cm.UpdateBubbleChart(selectedAxisY, selectedAxisX, selectedBubbleSize);
43
44      return View("Index",cm);
45    }
46
47
48
49    public FileResult CreateBubbleChart(ChartModel model)
50    {
51        IList<int> items = new List<int>();
52
53        items.Add(1);
54        items.Add(2);
55        items.Add(5);
56        items.Add(10);
57
58        Chart chart = new Chart();
59        chart.Width = 700;
60        chart.Height = 300;
61        chart.BackColor = Color.FromArgb(211, 223, 240);
62        chart.BorderlineDashStyle = ChartDashStyle.Solid;
63        chart.BackSecondaryColor = Color.White;
64        chart.BackGradientStyle = GradientStyle.TopBottom;
65        chart.BorderlineWidth = 1;
66        chart.Palette = ChartColorPalette.BrightPastel;
67        chart.BorderlineColor = Color.FromArgb(26, 59, 105);
68        chart.RenderType = RenderType.BinaryStreaming;
69        chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
70        chart.AntiAliasing = AntiAliasingStyles.All;
71        chart.TextAntiAliasingQuality = TextAntiAliasingQuality.Normal;
72        chart.Titles.Add("Bubble Chart");
73        //chart.Legends.Add(CreateLegend());
74        chart.Series.Add(CreateSeries(items,SeriesChartType.Bubble));
75        chart.ChartAreas.Add(CreateChartArea());
76
77
78
79        MemoryStream ms = new MemoryStream();
80        chart.SaveImage(ms);
81        return File(ms.GetBuffer(), @"image/png");
82    }
83
84
85
86    public Series CreateSeries(IList<int> results, SeriesChartType chartType)
87    {
88        Series seriesDetail = new Series();
89        seriesDetail.Name = "Result Chart";
90        seriesDetail.IsValueShownAsLabel = false;
91        seriesDetail.Color = Color.FromArgb(198, 99, 99);
92        seriesDetail.ChartType = chartType;
93        seriesDetail.BorderWidth = 2;
94        DataPoint point;
95
96        foreach (int result in results)
97        {
98            point = new DataPoint();
99            point.AxisLabel = result.ToString();
100            point.YValues = new double[] { (double)result };
101            seriesDetail.Points.Add(point);
102        }
103        seriesDetail.ChartArea = "Result Chart";
104        return seriesDetail;
105    }
106
107
108
109    public ChartArea CreateChartArea()
110    {
111        ChartArea chartArea = new ChartArea();
112        chartArea.Name = "Result Chart";
113        chartArea.BackColor = Color.Transparent;
114        chartArea.AxisX.IsLabelAutoFit = false;
115        chartArea.AxisY.IsLabelAutoFit = false;
116        chartArea.AxisX.LabelStyle.Font =
117           new Font("Verdana,Arial,Helvetica,sans-serif",
118                    8F, FontStyle.Regular);
119        chartArea.AxisY.LabelStyle.Font =
120           new Font("Verdana,Arial,Helvetica,sans-serif",
121                    8F, FontStyle.Regular);
122        chartArea.AxisY.LineColor = Color.FromArgb(64, 64, 64, 64);
123        chartArea.AxisX.LineColor = Color.FromArgb(64, 64, 64, 64);
124        chartArea.AxisY.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
125        chartArea.AxisX.MajorGrid.LineColor = Color.FromArgb(64, 64, 64, 64);
126        chartArea.AxisX.Interval = 1;
127
128        return chartArea;
129    }
130
131
132
133
134    public ActionResult Index() {
135      ChartModel cm = new ChartModel();
136
137      // Later, we will get the runs from the session ...
138      QueryServiceClient client = Query.GetClientFactory("okbtester", "okbtester");
139
140      long[] runIds = new long[5];
141      for (int i = 0; i < 5; i++) {
142        runIds[i] = i;
143      }
144      Run[] runs = client.GetRuns(runIds, false);
145
146      cm.UpdateRunCollection(runs);
147
148
149      return View(cm);
150    }
151
152    // [ChildActionOnly]
153    public ActionResult BubbleChart() {
154      ChartModel cm = new ChartModel();
155
156      return PartialView("BubbleChart", cm); // name of usercontrol
157    }
158
159
160    //public ActionResult BoxPlot()
161    //{
162    //    return View();
163    //}
164
165  }
166}
Note: See TracBrowser for help on using the repository browser.