This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
/
Web.PlotSurface2D.cs
671 lines (585 loc) · 16.3 KB
/
Web.PlotSurface2D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
NPlot - A charting library for .NET
Web.PlotSurface2d.cs
Copyright (C) 2003
Matt Howlett, Paolo Pierini
Redistribution and use of NPlot or parts there-of in source and
binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Re-distributions in source form must retain at the head of each
source file the above copyright notice, this list of conditions
and the following disclaimer.
2. Any product ("the product") that makes use NPlot or parts
there-of must either:
(a) allow any user of the product to obtain a complete machine-
readable copy of the corresponding source code for the
product and the version of NPlot used for a charge no more
than your cost of physically performing source distribution,
on a medium customarily used for software interchange, or:
(b) reproduce the following text in the documentation, about
box or other materials intended to be read by human users
of the product that is provided to every human user of the
product:
"This product includes software developed as
part of the NPlot library project available
from: http://www.nplot.com/"
The words "This product" may optionally be replace with
the actual name of the product.
------------------------------------------------------------------------
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Collections;
using System.Text;
namespace NPlot
{
namespace Web
{
/// <summary>
/// A PlotSurface2D web control. Rather than use this control, I generally create bitmaps
/// using Bitmap.PlotSurface2D, then use the ToBrowser() method in Bitmap.PlotSurface2D to
/// return them as a page request response (and point the src in an image tag to this page).
///
/// This is not as nice from a users perspective but is more efficient.
///
/// Note: this control can chew up memory until the user session ends if the client cancels
/// the page load before the image has loaded.
/// </summary>
[
DefaultProperty("Title"),
ToolboxData("<{0}:PlotSurface2D runat=server></{0}:PlotSurface2D>"),
Designer(typeof(NPlot.Web.Design.PlotSurface2D))
]
public class PlotSurface2D : System.Web.UI.WebControls.WebControl, IPlotSurface2D
{
private NPlot.PlotSurface2D ps_ = new NPlot.PlotSurface2D();
/// <summary>
/// Default constructor.
/// </summary>
public PlotSurface2D() :
base()
{
}
/// <summary>
/// The URL to redirect for the plot.
/// </summary>
private string plotUrl;
/// <summary>
/// the prefix used for the session variables
/// </summary>
private string prefix()
{
string toReturn = "__PlotSurface2D_";
toReturn += this.ClientID;
toReturn += "_";
toReturn += this.Page.ToString();
toReturn += "_";
return toReturn;
}
/// <summary>
/// Clears the plot.
/// </summary>
public void Clear()
{
ps_.Clear();
}
/// <summary>
/// Adds a drawable object to the plot surface. If the object is an IPlot,
/// the PlotSurface2D axes will also be updated.
/// </summary>
/// <param name="p">The IDrawable object to add to the plot surface.</param>
public void Add( IDrawable p )
{
ps_.Add( p );
}
/// <summary>
/// Adds a drawable object to the plot surface against the specified axes. If
/// the object is an IPlot, the PlotSurface2D axes will also be updated.
/// </summary>
/// <param name="p">the IDrawable object to add to the plot surface</param>
/// <param name="xp">the x-axis to add the plot against.</param>
/// <param name="yp">the y-axis to add the plot against.</param>
public void Add( IDrawable p, NPlot.PlotSurface2D.XAxisPosition xp, NPlot.PlotSurface2D.YAxisPosition yp )
{
ps_.Add( p, xp, yp );
}
/// <summary>
/// Adds a drawable object to the plot surface. If the object is an IPlot,
/// the PlotSurface2D axes will also be updated.
/// </summary>
/// <param name="p">The IDrawable object to add to the plot surface.</param>
/// <param name="zOrder">The z-ordering when drawing (objects with lower numbers are drawn first)</param>
public void Add( IDrawable p, int zOrder )
{
ps_.Add( p, zOrder );
}
/// <summary>
/// Adds a drawable object to the plot surface against the specified axes. If
/// the object is an IPlot, the PlotSurface2D axes will also be updated.
/// </summary>
/// <param name="p">the IDrawable object to add to the plot surface</param>
/// <param name="xp">the x-axis to add the plot against.</param>
/// <param name="yp">the y-axis to add the plot against.</param>
/// <param name="zOrder">The z-ordering when drawing (objects with lower numbers are drawn first)</param>
public void Add( IDrawable p, NPlot.PlotSurface2D.XAxisPosition xp,
NPlot.PlotSurface2D.YAxisPosition yp, int zOrder )
{
ps_.Add( p, xp, yp , zOrder);
}
/// <summary>
/// The plot surface title.
/// </summary>
[
Browsable(true),
Bindable(true)
]
public string Title
{
get
{
return ps_.Title;
}
set
{
ps_.Title = value;
}
}
/// <summary>
/// The plot title font.
/// </summary>
[
Browsable(true)
]
public System.Drawing.Font TitleFont
{
get
{
return ps_.TitleFont;
}
set
{
ps_.TitleFont = value;
}
}
/// <summary>
/// The distance in pixels to leave between of the edge of the bounding rectangle
/// supplied to the Draw method, and the markings that make up the plot.
/// </summary>
[
Browsable(true),
Category("Data"),
Bindable(true)
]
public int Padding
{
get
{
return ps_.Padding;
}
set
{
ps_.Padding = value;
}
}
/// <summary>
/// The first abscissa axis.
/// </summary>
[
Browsable(false),
Bindable(false)
]
public Axis XAxis1
{
get
{
return ps_.XAxis1;
}
set
{
ps_.XAxis1 = value;
}
}
/// <summary>
/// The first ordinate axis.
/// </summary>
[
Browsable(false),
Bindable(false)
]
public Axis YAxis1
{
get
{
return ps_.YAxis1;
}
set
{
ps_.YAxis1 = value;
}
}
/// <summary>
/// The second abscissa axis.
/// </summary>
[
Browsable(false),
Bindable(false)
]
public Axis XAxis2
{
get
{
return ps_.XAxis2;
}
set
{
ps_.XAxis2 = value;
}
}
/// <summary>
/// The second ordinate axis.
/// </summary>
[
Browsable(false),
Bindable(false)
]
public Axis YAxis2
{
get
{
return ps_.YAxis2;
}
set
{
ps_.YAxis2 = value;
}
}
/// <summary>
/// A color used to paint the plot background. Mutually exclusive with PlotBackImage and PlotBackBrush
/// </summary>
[
Bindable(true),
Browsable(true)
]
public System.Drawing.Color PlotBackColor
{
set
{
ps_.PlotBackColor = value;
}
}
/// <summary>
/// An imaged used to paint the plot background. Mutually exclusive with PlotBackColor and PlotBackBrush
/// </summary>
public System.Drawing.Bitmap PlotBackImage
{
set
{
ps_.PlotBackImage = value;
}
}
/// <summary>
/// A Rectangle brush used to paint the plot background. Mutually exclusive with PlotBackColor and PlotBackBrush
/// </summary>
public IRectangleBrush PlotBackBrush
{
set
{
ps_.PlotBackBrush = value;
}
}
/// <summary>
/// Gets or Sets the legend to use with this plot surface.
/// </summary>
[
Bindable(false),
Browsable(false)
]
public NPlot.Legend Legend
{
get
{
return ps_.Legend;
}
set
{
ps_.Legend = value;
}
}
/// <summary>
/// Gets or Sets the legend z-order.
/// </summary>
[
Bindable(true),
Browsable(true)
]
public int LegendZOrder
{
get
{
return ps_.LegendZOrder;
}
set
{
ps_.LegendZOrder = value;
}
}
/// <summary>
/// Smoothing mode to use when drawing plots.
/// </summary>
[
Bindable(true),
Browsable(true)
]
public System.Drawing.Drawing2D.SmoothingMode SmoothingMode
{
get
{
return ps_.SmoothingMode;
}
set
{
ps_.SmoothingMode = value;
}
}
/// <summary>
/// The bitmap background color outside the bounds of the plot surface.
/// </summary>
[
Bindable(true),
Browsable(true)
]
public override Color BackColor
{
set
{
backColor_ = value;
}
}
object backColor_ = null;
/// <summary>
/// Ivan Ivanov wrote this function. From his email:
/// If the request string contains encoded parameters values [e.g. # - %23].
/// The call to request.Url.ToString() will decode values [e.g. instead of %23
/// it will return #]. On the subsequent request to the page that contains the
/// nplot control [when the actual drawing of the image takes place] the request
/// url will be cut up to the unformated value [e.g. #] and since the PlotSurface2D_
/// is added at the end of the query string, it will be missing.
/// </summary>
/// <returns></returns>
private String buildPlotURL()
{
StringBuilder urlParams = new StringBuilder();
foreach (string getParamName in Context.Request.QueryString.AllKeys)
{
urlParams.Append(getParamName + "=" +
Context.Server.UrlEncode(Context.Request.QueryString[getParamName]) + "&");
}
return Context.Request.Url.AbsolutePath +
(urlParams.Length > 0 ?
"?" + urlParams.Append("PlotSurface2D_" + this.ClientID + "=1").ToString() :
"?PlotSurface2D_" + this.ClientID + "=1");
}
/// <summary>
/// Initialization event.
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
System.Web.HttpRequest request = Context.Request;
System.Web.HttpResponse response = Context.Response;
if (request.Params["PlotSurface2D_" + this.ClientID] != null)
{
// retrieve the bitmap and display
response.Clear();
try
{
response.ContentType = "Image/Png";
System.Drawing.Bitmap bmp = (System.Drawing.Bitmap) Context.Session[prefix()+"PNG"];
// don't ask why, but if I write directly to the response
// I have a GDI+ error, if I first write to a MemoryStream and
// then to the response.OutputStream I don't get an error.
System.IO.MemoryStream s = new System.IO.MemoryStream();
bmp.Save( s, System.Drawing.Imaging.ImageFormat.Png);
s.WriteTo(response.OutputStream);
Context.Session.Remove(prefix()+"PNG");
}
catch (Exception ex)
{
response.ContentType = "Text/HTML";
response.Write( ex.Message );
}
finally
{
response.Flush();
response.End();
}
}
this.plotUrl = this.buildPlotURL();
base.OnInit (e);
}
/// <summary>
/// Render this control as an HTML stream.
/// </summary>
/// <param name="output">The HTML writer to write out to.</param>
protected override void Render(HtmlTextWriter output)
{
// first of all render the bitmap;
System.Drawing.Bitmap b = new System.Drawing.Bitmap( (int)this.Width.Value, (int)this.Height.Value );
if (backColor_!=null)
{
Graphics g = Graphics.FromImage( b );
g.FillRectangle( (new Pen( (Color)this.backColor_)).Brush,0,0,b.Width,b.Height );
}
ps_.Draw( Graphics.FromImage(b), new System.Drawing.Rectangle(0,0,b.Width,b.Height) );
// then store in context memory.
Context.Session[prefix()+"PNG"] = b;
// now render html.
if (this.BorderStyle == BorderStyle.None)
{
output.AddAttribute("border","0");
}
else
{
output.AddAttribute("border",this.BorderWidth.ToString());
output.AddAttribute("borderColor",this.BorderColor.ToKnownColor().ToString());
}
output.AddAttribute("cellSpacing","0");
output.AddAttribute("cellPadding","0");
output.RenderBeginTag("table");
output.RenderBeginTag("tr");
output.AddAttribute("vAlign","center");
output.AddAttribute("align","middle");
output.RenderBeginTag("td");
output.RenderBeginTag("P");
output.AddAttribute("src",this.plotUrl);
output.AddAttribute("alt",this.ToolTip);
output.RenderBeginTag("img");
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
output.Flush();
}
/// <summary>
/// Add an axis constraint to the plot surface. Axis constraints can
/// specify relative world-pixel scalings, absolute axis positions etc.
/// </summary>
/// <param name="c">The axis constraint to add.</param>
public void AddAxesConstraint( AxesConstraint c )
{
ps_.AddAxesConstraint( c );
}
/// <summary>
/// Whether or not the title will be scaled according to size of the plot
/// surface.
/// </summary>
[
Browsable(true),
Bindable(true)
]
public bool AutoScaleTitle
{
get
{
return ps_.AutoScaleTitle;
}
set
{
ps_.AutoScaleTitle = value;
}
}
/// <summary>
/// When plots are added to the plot surface, the axes they are attached to
/// are immediately modified to reflect data of the plot. If
/// AutoScaleAutoGeneratedAxes is true when a plot is added, the axes will
/// be turned in to auto scaling ones if they are not already [tick marks,
/// tick text and label size scaled to size of plot surface]. If false,
/// axes will not be autoscaling.
/// </summary>
[
Browsable(true),
Bindable(true),
]
public bool AutoScaleAutoGeneratedAxes
{
get
{
return ps_.AutoScaleAutoGeneratedAxes;
}
set
{
ps_.AutoScaleAutoGeneratedAxes = value;
}
}
/// <summary>
/// Sets the title to be drawn using a solid brush of this color.
/// </summary>
[
Browsable(true),
Bindable(true)
]
public Color TitleColor
{
set
{
ps_.TitleColor = value;
}
}
/// <summary>
/// The brush used for drawing the title.
/// </summary>
[
Browsable(false)
]
public Brush TitleBrush
{
get
{
return ps_.TitleBrush;
}
set
{
ps_.TitleBrush = value;
}
}
/// <summary>
/// Remove a drawable object from the plot surface.
/// </summary>
/// <param name="p">the drawable to remove</param>
/// <param name="updateAxes">whether or not to update the axes after removing the idrawable.</param>
public void Remove( IDrawable p, bool updateAxes )
{
ps_.Remove( p, updateAxes );
}
/// <summary>
/// Gets an array list containing all drawables currently added to the PlotSurface2D.
/// </summary>
[
Browsable(false)
]
public ArrayList Drawables
{
get
{
return ps_.Drawables;
}
}
}
}
}