/* * NPlot - A charting library for .NET * * Bitmap.PlotSurface2D.cs * Copyright (C) 2003-2006 Matt Howlett and others. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of NPlot nor the names of its contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER OR CONTRIBUTORS 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.Collections; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; namespace NPlot { namespace Bitmap { /// /// Wrapper around NPlot.PlotSurface2D that provides extra functionality /// specific to drawing to Bitmaps. /// public class PlotSurface2D : IPlotSurface2D { private readonly NPlot.PlotSurface2D ps_; private System.Drawing.Bitmap b_; private object backColor_; /// /// Constructor. /// /// width of the bitmap. /// height of the bitmap. public PlotSurface2D(int width, int height) { b_ = new System.Drawing.Bitmap(width, height); ps_ = new NPlot.PlotSurface2D(); } /// /// Constructor. /// /// The Bitmap where the plot is to be rendered. public PlotSurface2D(System.Drawing.Bitmap b) { b_ = b; ps_ = new NPlot.PlotSurface2D(); } /// /// The bitmap width /// public int Width { get { return b_.Width; } } /// /// The bitmap height /// public int Height { get { return b_.Height; } } /// /// The bitmap to use as the drawing surface. /// public System.Drawing.Bitmap Bitmap { get { return b_; } set { b_ = value; } } /// /// The bitmap background color outside the bounds of the plot surface. /// public Color BackColor { set { backColor_ = value; } } /// /// Clears the plot. /// public void Clear() { ps_.Clear(); } /// /// Adds a drawable object to the plot surface. If the object is an IPlot, /// the PlotSurface2D axes will also be updated. /// /// The IDrawable object to add to the plot surface. public void Add(IDrawable p) { ps_.Add(p); } /// /// 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. /// /// the IDrawable object to add to the plot surface /// the x-axis to add the plot against. /// the y-axis to add the plot against. public void Add(IDrawable p, NPlot.PlotSurface2D.XAxisPosition xp, NPlot.PlotSurface2D.YAxisPosition yp) { ps_.Add(p, xp, yp); } /// /// Adds a drawable object to the plot surface. If the object is an IPlot, /// the PlotSurface2D axes will also be updated. /// /// The IDrawable object to add to the plot surface. /// The z-ordering when drawing (objects with lower numbers are drawn first) public void Add(IDrawable p, int zOrder) { ps_.Add(p, zOrder); } /// /// 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. /// /// the IDrawable object to add to the plot surface /// the x-axis to add the plot against. /// the y-axis to add the plot against. /// The z-ordering when drawing (objects with lower numbers are drawn first) public void Add(IDrawable p, NPlot.PlotSurface2D.XAxisPosition xp, NPlot.PlotSurface2D.YAxisPosition yp, int zOrder) { ps_.Add(p, xp, yp, zOrder); } /// /// The plot surface title. /// public string Title { get { return ps_.Title; } set { ps_.Title = value; } } /// /// The plot title font. /// public Font TitleFont { get { return ps_.TitleFont; } set { ps_.TitleFont = value; } } /// /// 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. /// public int Padding { get { return ps_.Padding; } set { ps_.Padding = value; } } /// /// The bottom abscissa axis. /// public Axis XAxis1 { get { return ps_.XAxis1; } set { ps_.XAxis1 = value; } } /// /// The left ordinate axis. /// public Axis YAxis1 { get { return ps_.YAxis1; } set { ps_.YAxis1 = value; } } /// /// The top abscissa axis. /// public Axis XAxis2 { get { return ps_.XAxis2; } set { ps_.XAxis2 = value; } } /// /// The right ordinate axis. /// public Axis YAxis2 { get { return ps_.YAxis2; } set { ps_.YAxis2 = value; } } /// /// Gets or Sets the legend to use with this plot surface. /// public Legend Legend { get { return ps_.Legend; } set { ps_.Legend = value; } } /// /// Gets or Sets the legend z-order. /// public int LegendZOrder { get { return ps_.LegendZOrder; } set { ps_.LegendZOrder = value; } } /// /// A color used to paint the plot background. Mutually exclusive with PlotBackImage and PlotBackBrush /// public Color PlotBackColor { set { ps_.PlotBackColor = value; } } /// /// An imaged used to paint the plot background. Mutually exclusive with PlotBackColor and PlotBackBrush /// public System.Drawing.Bitmap PlotBackImage { set { ps_.PlotBackImage = value; } } /// /// A Rectangle brush used to paint the plot background. Mutually exclusive with PlotBackColor and PlotBackBrush /// public IRectangleBrush PlotBackBrush { set { ps_.PlotBackBrush = value; } } /// /// Smoothing mode to use when drawing plots. /// public SmoothingMode SmoothingMode { get { return ps_.SmoothingMode; } set { ps_.SmoothingMode = value; } } /// /// Add an axis constraint to the plot surface. Axis constraints can /// specify relative world-pixel scalings, absolute axis positions etc. /// /// The axis constraint to add. public void AddAxesConstraint(AxesConstraint c) { ps_.AddAxesConstraint(c); } /// /// Whether or not the title will be scaled according to size of the plot /// surface. /// public bool AutoScaleTitle { get { return ps_.AutoScaleTitle; } set { ps_.AutoScaleTitle = value; } } /// /// 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. /// public bool AutoScaleAutoGeneratedAxes { get { return ps_.AutoScaleAutoGeneratedAxes; } set { ps_.AutoScaleAutoGeneratedAxes = value; } } /// /// Sets the title to be drawn using a solid brush of this color. /// public Color TitleColor { set { ps_.TitleColor = value; } } /// /// The brush used for drawing the title. /// public Brush TitleBrush { get { return ps_.TitleBrush; } set { ps_.TitleBrush = value; } } /// /// Remove a drawable object from the plot surface. /// /// the drawable to remove /// whether or not to update the axes after removing the idrawable. public void Remove(IDrawable p, bool updateAxes) { ps_.Remove(p, updateAxes); } /// /// Gets an array list containing all drawables currently added to the PlotSurface2D. /// public ArrayList Drawables { get { return ps_.Drawables; } } /// /// Renders the plot. /// /// The graphics surface. /// The rectangle storing the bounds for rendering. public void Draw(Graphics g, Rectangle bounds) { ps_.Draw(g, bounds); } /// /// Renders the bitmap to a MemoryStream. Useful for returning the bitmap from /// an ASP.NET page. /// /// The MemoryStream object. public MemoryStream ToStream(ImageFormat imageFormat) { MemoryStream stream = new MemoryStream(); ps_.Draw(Graphics.FromImage(Bitmap), new Rectangle(0, 0, b_.Width, b_.Height)); Bitmap.Save(stream, imageFormat); return stream; } /// /// Refreshes (draws) the plot. /// public void Refresh() { if (backColor_ != null) { Graphics g = Graphics.FromImage(b_); g.FillRectangle((new Pen((Color) backColor_)).Brush, 0, 0, b_.Width, b_.Height); } ps_.Draw(Graphics.FromImage(b_), new Rectangle(0, 0, b_.Width, b_.Height)); } } } }