BioGTK  6.7.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
Plot.cs
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using AForge;
9using Gdk;
10using GLib;
11using Gtk;
12using System.Threading;
13using System.IO;
14using ScottPlot;
15
16namespace BioGTK
17{
18 public class Plot : Gtk.Window
19 {
20 ScottPlot.Plot model;
21 //PlotModel model;
22 string file;
23 string name;
24 Bitmap bitmap;
25 List<double[]> data = new List<double[]>();
26 static Dictionary<string, Plot> plots = new Dictionary<string, Plot>();
27
28 public enum PlotType
29 {
30 Bar,
31 Scatter
32 }
33 PlotType type;
34 public PlotType Type
35 {
36 get { return type; }
37 set { type = value; }
38 }
39 public static Dictionary<string,Plot> Plots
40 {
41 get { return plots; }
42 }
43 public List<double[]> Data
44 {
45 get { return data; }
46 set { data = value; UpdateImage(); }
47 }
48 public Bitmap Image
49 {
50 get { return bitmap; }
51 set { bitmap = value; }
52 }
53 public ScottPlot.Plot Model
54 {
55 get { return model; }
56 set { model = value; UpdateImage(); }
57 }
60 public void UpdateImage()
61 {
62 model.SavePng(file, AllocatedWidth, AllocatedHeight);
63 this.Title = name;
64 pixbuf = new Pixbuf(file);
65 image.QueueDraw();
66 }
68 private void InitScatter()
69 {
70 model.Clear();
71 List<Coordinates> cs = new List<Coordinates>();
72 for (int i = 0; i < data.Count; i++)
73 {
74 for (int x = 0; x < data[i].Length; x++)
75 {
76 cs.Add(new ScottPlot.Coordinates(x, data[i][x]));
77 }
78 }
79 model.Add.Scatter(cs);
80 }
83 private void InitBars()
84 {
85 model.Clear();
86 foreach (double[] items in data)
87 {
88 int i = 0;
89 List<Bar> bars = new List<Bar>();
90 foreach (double item in items)
91 {
92 Bar b = new Bar();
93 b.Value = item;
94 b.Position = i;
95 bars.Add(b);
96 i++;
97 }
98 model.Add.Bars(bars);
99 }
100 }
101 #region Properties
102 Pixbuf pixbuf;
103 private Builder _builder;
104#pragma warning disable 649
105 [Builder.Object]
106 private Gtk.DrawingArea image;
107 [Builder.Object]
108 private Gtk.MenuItem saveImageMenu;
109 [Builder.Object]
110 private Gtk.MenuItem saveCSVMenu;
111
112 [Builder.Object]
113 private Gtk.CheckMenuItem barMenu;
114 [Builder.Object]
115 private Gtk.CheckMenuItem scatterMenu;
116
117#pragma warning restore 649
118 #endregion
119
120 #region Constructors / Destructors
121
125 public static Plot Create(double[] vals, string name)
126 {
127 Builder builder = new Builder(new FileStream(System.IO.Path.GetDirectoryName(Environment.ProcessPath) + "/" + "Glade/Plot.glade", FileMode.Open));
128 return new Plot(builder, builder.GetObject("plot").Handle, vals, name, PlotType.Scatter);
129 }
133 public static Plot Create(double[] vals, string name, PlotType typ)
134 {
135 Builder builder = new Builder(new FileStream("Glade/Plot.glade", FileMode.Open));
136 return new Plot(builder, builder.GetObject("plot").Handle, vals, name, typ);
137 }
138
139 /* It's the constructor of the class. */
140 protected Plot(Builder builder, IntPtr handle, double[] vals, string name, PlotType typ) : base(handle)
141 {
142 _builder = builder;
143 builder.Autoconnect(this);
144 model = new ScottPlot.Plot();
145 data.Add(vals);
146 if (typ == PlotType.Scatter)
147 InitScatter();
148 else
149 if (typ == PlotType.Bar)
150 InitBars();
151 if (!name.EndsWith(".png") || !name.EndsWith(".PNG"))
152 file = name + ".png";
153 this.Title = name;
154 image.Drawn += Image_Drawn;
155 image.SizeAllocated += Image_SizeAllocated;
156 image.ButtonPressEvent += Image_ButtonPressEvent;
157 barMenu.ButtonPressEvent += BarMenu_ButtonPressEvent;
158 scatterMenu.ButtonPressEvent += ScatterMenu_ButtonPressEvent;
159 saveImageMenu.ButtonPressEvent += SaveImageMenu_ButtonPressEvent;
160 saveCSVMenu.ButtonPressEvent += SaveCSVMenu_ButtonPressEvent;
161 this.DeleteEvent += About_DeleteEvent;
162 this.name = name;
163 selected = this;
164 if(plots.ContainsKey(name))
165 plots[name] = this;
166 else
167 plots.Add(name,this);
168 App.ApplyStyles(this);
169 }
170
181 private void SaveCSVMenu_ButtonPressEvent(object o, ButtonPressEventArgs args)
182 {
183 Gtk.FileChooserDialog filechooser =
184 new Gtk.FileChooserDialog("Save CSV Plot data.",
185 this,
186 FileChooserAction.Save,
187 "Cancel", ResponseType.Cancel,
188 "Save", ResponseType.Accept);
189 if (filechooser.Run() != (int)ResponseType.Accept)
190 return;
191
192 string s = "";
193 foreach (double[] item in data)
194 {
195 for (int i = 0; i < data.Count; i++)
196 {
197 s += i + ",";
198 }
199 s += Environment.NewLine;
200 foreach (double d in item)
201 {
202 s += d + "," + Environment.NewLine;
203 }
204 }
205 File.WriteAllText(filechooser.Filename, s);
206 filechooser.Destroy();
207
208 }
209
222 private void SaveImageMenu_ButtonPressEvent(object o, ButtonPressEventArgs args)
223 {
224 Gtk.FileChooserDialog filechooser =
225 new Gtk.FileChooserDialog("Save PNG Image of Plot.",
226 this,
227 FileChooserAction.Save,
228 "Cancel", ResponseType.Cancel,
229 "Save", ResponseType.Accept);
230 if (filechooser.Run() != (int)ResponseType.Accept)
231 return;
232 pixbuf.Save(filechooser.Filename,"png");
233 filechooser.Destroy();
234
235 }
236
246 private void ScatterMenu_ButtonPressEvent(object o, ButtonPressEventArgs args)
247 {
248 barMenu.Active = false;
249 type = PlotType.Scatter;
250 InitScatter();
251 UpdateImage();
252 }
253
262 private void BarMenu_ButtonPressEvent(object o, ButtonPressEventArgs args)
263 {
264 scatterMenu.Active = false;
265 type = PlotType.Bar;
266 InitBars();
267 UpdateImage();
268 }
269
279 private void Image_ButtonPressEvent(object o, ButtonPressEventArgs args)
280 {
281 selected = this;
282 BioLib.Recorder.AddLine("Plot.SelectedPlot = Plot.Plots[" + name + "];",false);
283 }
284
285 System.Threading.Thread th;
286 static Plot selected;
287 public static Plot SelectedPlot
288 {
289 get { return selected; }
290 }
292 static void ShowPlot()
293 {
294 selected.Show();
295 }
296
304 private void Image_SizeAllocated(object o, SizeAllocatedArgs args)
305 {
306 UpdateImage();
307 }
308
317 private void About_DeleteEvent(object o, DeleteEventArgs args)
318 {
319 args.RetVal = true;
320 plots.Remove(name);
321 Hide();
322 }
323
332 private void Image_Drawn(object o, DrawnArgs e)
333 {
334 Pixbuf pf = pixbuf.ScaleSimple(image.AllocatedWidth, image.AllocatedHeight, InterpType.Bilinear);
335 Gdk.CairoHelper.SetSourcePixbuf(e.Cr, pf, 0, 0);
336 e.Cr.Paint();
337 e.Cr.Stroke();
338 }
339
340
341 #endregion
342
343 }
344}
static Plot Create(double[] vals, string name)
Definition Plot.cs:125
static Plot Create(double[] vals, string name, PlotType typ)
Definition Plot.cs:133
void UpdateImage()
Definition Plot.cs:60
Definition ML.cs:17
Definition Zarr.cs:20