Web.PlotSurface2D.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 Web.PlotSurface2d.cs
00005 Copyright (C) 2003 
00006 Matt Howlett, Paolo Pierini
00007 
00008 Redistribution and use of NPlot or parts there-of in source and
00009 binary forms, with or without modification, are permitted provided
00010 that the following conditions are met:
00011 
00012 1. Re-distributions in source form must retain at the head of each
00013    source file the above copyright notice, this list of conditions
00014    and the following disclaimer.
00015 
00016 2. Any product ("the product") that makes use NPlot or parts 
00017    there-of must either:
00018   
00019     (a) allow any user of the product to obtain a complete machine-
00020         readable copy of the corresponding source code for the 
00021         product and the version of NPlot used for a charge no more
00022         than your cost of physically performing source distribution,
00023             on a medium customarily used for software interchange, or:
00024 
00025     (b) reproduce the following text in the documentation, about 
00026         box or other materials intended to be read by human users
00027         of the product that is provided to every human user of the
00028         product: 
00029    
00030               "This product includes software developed as 
00031               part of the NPlot library project available 
00032               from: http://www.nplot.com/" 
00033 
00034         The words "This product" may optionally be replace with 
00035         the actual name of the product.
00036 
00037 ------------------------------------------------------------------------
00038 
00039 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00040 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00041 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00042 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00043 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00044 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00045 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00046 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00047 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00048 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00049 
00050 */
00051 
00052 using System;
00053 using System.Drawing;
00054 using System.Drawing.Imaging;
00055 using System.Web.UI;
00056 using System.Web.UI.WebControls;
00057 using System.ComponentModel;
00058 using System.IO;
00059 using System.Collections;
00060 using System.Text;
00061 
00062 namespace NPlot
00063 {
00064 
00065         namespace Web
00066         {
00067 
00078                 [
00079                 DefaultProperty("Title"), 
00080                 ToolboxData("<{0}:PlotSurface2D runat=server></{0}:PlotSurface2D>"),
00081                 Designer(typeof(NPlot.Web.Design.PlotSurface2D))
00082                 ]
00083                 public class PlotSurface2D : System.Web.UI.WebControls.WebControl, IPlotSurface2D 
00084                 {
00085 
00086                         private NPlot.PlotSurface2D ps_ = new NPlot.PlotSurface2D();
00087 
00088 
00092                         public PlotSurface2D() : 
00093                                 base()
00094                         {
00095                         }
00096 
00097 
00101                         private string plotUrl;
00102 
00103 
00107                         private string prefix()
00108                         {
00109                                 string toReturn = "__PlotSurface2D_";
00110                                 toReturn += this.ClientID;
00111                                 toReturn += "_";
00112                                 toReturn += this.Page.ToString();
00113                                 toReturn += "_";
00114                                 return toReturn;
00115                         }
00116 
00117 
00121                         public void Clear()
00122                         {
00123                                 ps_.Clear();
00124                         }
00125 
00126 
00132                         public void Add( IDrawable p )
00133                         {
00134                                 ps_.Add( p );
00135                         }
00136 
00137 
00145                         public void Add( IDrawable p, NPlot.PlotSurface2D.XAxisPosition xp, NPlot.PlotSurface2D.YAxisPosition yp )
00146                         {
00147                                 ps_.Add( p, xp, yp );
00148                         }
00149 
00150 
00157                         public void Add( IDrawable p, int zOrder )
00158                         {
00159                                 ps_.Add( p, zOrder );
00160                         }
00161 
00162 
00171                         public void Add( IDrawable p, NPlot.PlotSurface2D.XAxisPosition xp,
00172                                 NPlot.PlotSurface2D.YAxisPosition yp, int zOrder )
00173                         {
00174                                 ps_.Add( p, xp, yp , zOrder);
00175                         }
00176 
00180                         [
00181                         Browsable(true),
00182                         Bindable(true)
00183                         ]
00184                         public string Title
00185                         {
00186                                 get 
00187                                 {
00188                                         return ps_.Title;
00189                                 }
00190                                 set 
00191                                 {
00192                                         ps_.Title = value;
00193                                 }
00194                         }
00195 
00196 
00200                         [
00201                         Browsable(true)
00202                         ]
00203                         public System.Drawing.Font TitleFont 
00204                         {
00205                                 get 
00206                                 {
00207                                         return ps_.TitleFont;
00208                                 }
00209                                 set 
00210                                 {
00211                                         ps_.TitleFont = value;
00212                                 }
00213                         }
00214 
00215 
00220                         [
00221                         Browsable(true),
00222                         Category("Data"),
00223                         Bindable(true)
00224                         ]
00225                         public int Padding
00226                         {
00227                                 get
00228                                 {
00229                                         return ps_.Padding;
00230                                 }
00231                                 set
00232                                 {
00233                                         ps_.Padding = value;
00234                                 }
00235                         }
00236 
00237 
00241                         [
00242                         Browsable(false),
00243                         Bindable(false)
00244                         ]
00245                         public Axis XAxis1
00246                         {
00247                                 get
00248                                 {
00249                                         return ps_.XAxis1;
00250                                 }
00251                                 set
00252                                 {
00253                                         ps_.XAxis1 = value;
00254                                 }
00255                         }
00256 
00257 
00261                         [
00262                         Browsable(false),
00263                         Bindable(false)
00264                         ]
00265                         public Axis YAxis1
00266                         {
00267                                 get
00268                                 {
00269                                         return ps_.YAxis1;
00270                                 }
00271                                 set
00272                                 {
00273                                         ps_.YAxis1 = value;
00274                                 }
00275                         }
00276 
00277 
00281                         [
00282                         Browsable(false),
00283                         Bindable(false)
00284                         ]
00285                         public Axis XAxis2
00286                         {
00287                                 get
00288                                 {
00289                                         return ps_.XAxis2;
00290                                 }
00291                                 set
00292                                 {
00293                                         ps_.XAxis2 = value;
00294                                 }
00295                         }
00296 
00297 
00301                         [
00302                         Browsable(false),
00303                         Bindable(false)
00304                         ]
00305                         public Axis YAxis2
00306                         {
00307                                 get
00308                                 {
00309                                         return ps_.YAxis2;
00310                                 }
00311                                 set
00312                                 {
00313                                         ps_.YAxis2 = value;
00314                                 }
00315                         }
00316 
00317 
00321                         [
00322                         Bindable(true),
00323                         Browsable(true)
00324                         ]
00325                         public System.Drawing.Color PlotBackColor
00326                         {
00327                                 set
00328                                 {
00329                                         ps_.PlotBackColor = value;
00330                                 }
00331                         }
00332 
00333 
00337                         public System.Drawing.Bitmap PlotBackImage
00338                         {
00339                                 set
00340                                 {
00341                                         ps_.PlotBackImage = value;
00342                                 }
00343                         }
00344 
00345 
00349                         public IRectangleBrush PlotBackBrush
00350                         {
00351                                 set
00352                                 {
00353                                         ps_.PlotBackBrush = value;
00354                                 }
00355                         }
00356 
00357 
00361                         [
00362                         Bindable(false),
00363                         Browsable(false)
00364                         ]
00365                         public NPlot.Legend Legend
00366                         {
00367                                 get
00368                                 {
00369                                         return ps_.Legend;
00370                                 }
00371                                 set
00372                                 {
00373                                         ps_.Legend = value;
00374                                 }
00375                         }
00376 
00377 
00381                         [
00382                         Bindable(true),
00383                         Browsable(true)
00384                         ]
00385                         public int LegendZOrder
00386                         {
00387                                 get
00388                                 {
00389                                         return ps_.LegendZOrder;
00390                                 }
00391                                 set
00392                                 {
00393                                         ps_.LegendZOrder = value;
00394                                 }
00395                         }
00396         
00397 
00401                         [
00402                         Bindable(true),
00403                         Browsable(true)
00404                         ]
00405                         public System.Drawing.Drawing2D.SmoothingMode SmoothingMode 
00406                         { 
00407                                 get
00408                                 {
00409                                         return ps_.SmoothingMode;
00410                                 }
00411                                 set
00412                                 {
00413                                         ps_.SmoothingMode = value;
00414                                 }
00415                         }
00416 
00417 
00421                         [
00422                         Bindable(true),
00423                         Browsable(true)
00424                         ]
00425                         public override Color BackColor
00426                         {
00427                                 set
00428                                 {
00429                                         backColor_ = value;
00430                                 }
00431                         }
00432                         object backColor_ = null;
00433                         
00434 
00445                         private String buildPlotURL()
00446                         {
00447                                 StringBuilder urlParams = new StringBuilder();
00448 
00449                                 foreach (string getParamName in Context.Request.QueryString.AllKeys)
00450                                 {
00451                                         urlParams.Append(getParamName + "=" +
00452                                                 Context.Server.UrlEncode(Context.Request.QueryString[getParamName]) + "&");
00453                                 }
00454 
00455                                 return Context.Request.Url.AbsolutePath +
00456                                         (urlParams.Length > 0  ?
00457                                         "?" + urlParams.Append("PlotSurface2D_" + this.ClientID + "=1").ToString() :
00458                                         "?PlotSurface2D_" + this.ClientID + "=1");
00459                         }
00460 
00461 
00466                         protected override void OnInit(EventArgs e)
00467                         {
00468                                 System.Web.HttpRequest request = Context.Request;
00469                                 System.Web.HttpResponse response = Context.Response;
00470                                 if (request.Params["PlotSurface2D_" + this.ClientID] != null) 
00471                                 {
00472                                         // retrieve the bitmap and display
00473                                         response.Clear();
00474                                         try
00475                                         {
00476                                                 response.ContentType = "Image/Png"; 
00477                                                 System.Drawing.Bitmap bmp = (System.Drawing.Bitmap) Context.Session[prefix()+"PNG"];
00478                         
00479                         // don't ask why, but if I write directly to the response
00480                                                 // I have a GDI+ error, if I first write to a MemoryStream and 
00481                                                 // then to the response.OutputStream I don't get an error.
00482                                                 System.IO.MemoryStream s = new System.IO.MemoryStream();
00483                                                 bmp.Save( s, System.Drawing.Imaging.ImageFormat.Png);
00484                                                 s.WriteTo(response.OutputStream);
00485                                                 Context.Session.Remove(prefix()+"PNG");
00486                                         }
00487                                         catch (Exception ex)
00488                                         {
00489                                                 response.ContentType = "Text/HTML";
00490                                                 response.Write( ex.Message );
00491                                         }
00492                                         finally
00493                                         {
00494                                                 response.Flush();
00495                                                 response.End(); 
00496                                         }
00497                                 }
00498                                 
00499                                 this.plotUrl = this.buildPlotURL();
00500                                 base.OnInit (e);
00501                         }
00502 
00503         
00508                         protected override void Render(HtmlTextWriter output)
00509                         {
00510 
00511                                 // first of all render the bitmap;
00512                                 System.Drawing.Bitmap b = new System.Drawing.Bitmap( (int)this.Width.Value, (int)this.Height.Value );
00513                                 if (backColor_!=null)
00514                                 {
00515                                         Graphics g = Graphics.FromImage( b );
00516                                         g.FillRectangle( (new Pen( (Color)this.backColor_)).Brush,0,0,b.Width,b.Height );
00517                                 }
00518                                 ps_.Draw( Graphics.FromImage(b), new System.Drawing.Rectangle(0,0,b.Width,b.Height) );
00519 
00520                                 // then store in context memory. 
00521                                 Context.Session[prefix()+"PNG"] = b;
00522 
00523                                 // now render html.
00524                                 if (this.BorderStyle == BorderStyle.None)
00525                                 {
00526                                         output.AddAttribute("border","0");
00527                                 }
00528                                 else
00529                                 {
00530                                         output.AddAttribute("border",this.BorderWidth.ToString());
00531                                         output.AddAttribute("borderColor",this.BorderColor.ToKnownColor().ToString());
00532                                 }
00533                                 output.AddAttribute("cellSpacing","0");
00534                                 output.AddAttribute("cellPadding","0");
00535                                 output.RenderBeginTag("table");
00536                                 output.RenderBeginTag("tr");
00537                                 output.AddAttribute("vAlign","center");
00538                                 output.AddAttribute("align","middle");
00539                                 output.RenderBeginTag("td");
00540                                 output.RenderBeginTag("P");
00541                                 output.AddAttribute("src",this.plotUrl);
00542                                 output.AddAttribute("alt",this.ToolTip);
00543                                 output.RenderBeginTag("img");
00544                                 output.RenderEndTag();
00545                                 output.RenderEndTag();
00546                                 output.RenderEndTag();
00547                                 output.RenderEndTag();
00548                                 output.RenderEndTag();
00549                                 output.Flush();
00550                         }
00551 
00557                         public void AddAxesConstraint( AxesConstraint c )
00558                         {
00559                                 ps_.AddAxesConstraint( c );
00560                         }
00561 
00562 
00567                         [
00568                         Browsable(true),
00569                         Bindable(true)
00570                         ]
00571                         public bool AutoScaleTitle
00572                         {
00573                                 get
00574                                 {
00575                                         return ps_.AutoScaleTitle;
00576                                 }
00577                                 set
00578                                 {
00579                                         ps_.AutoScaleTitle = value;
00580                                 }
00581                         }
00582 
00583 
00592                         [
00593                         Browsable(true),
00594                         Bindable(true),
00595                         ]
00596                         public bool AutoScaleAutoGeneratedAxes
00597                         {
00598                                 get
00599                                 {
00600                                         return ps_.AutoScaleAutoGeneratedAxes;
00601                                 }
00602                                 set
00603                                 {
00604                                         ps_.AutoScaleAutoGeneratedAxes = value;
00605                                 }
00606                         }
00607 
00608 
00612                         [
00613                         Browsable(true),
00614                         Bindable(true)
00615                         ]
00616                         public Color TitleColor
00617                         {
00618                                 set
00619                                 {
00620                                         ps_.TitleColor = value;
00621                                 }
00622                         }
00623 
00624 
00628                         [
00629                         Browsable(false)
00630                         ]
00631                         public Brush TitleBrush
00632                         {
00633                                 get
00634                                 {
00635                                         return ps_.TitleBrush;
00636                                 }
00637                                 set
00638                                 {
00639                                         ps_.TitleBrush = value;
00640                                 }
00641                         }
00642 
00648                         public void Remove( IDrawable p, bool updateAxes ) 
00649                         {
00650                                 ps_.Remove( p, updateAxes );
00651                         }
00652 
00653 
00657                         [
00658                         Browsable(false)
00659                         ]
00660                         public ArrayList Drawables
00661                         {
00662                                 get
00663                                 {
00664                                         return ps_.Drawables;
00665                                 }
00666                         }
00667 
00668                 }
00669         }
00670 
00671 }

Generated on Sat Nov 5 01:04:06 2005 for NPlot by  doxygen 1.4.5