/* * NPlot - A charting library for .NET * * BarPlot.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.Drawing; using System.Text; namespace NPlot { /// /// Draws /// public class BarPlot : BasePlot, IPlot, IDrawable { private float barWidth_ = 8; private Pen borderPen_ = new Pen(Color.Black); private IRectangleBrush rectangleBrush_ = new RectangleBrushes.Solid(Color.LightGray); /// /// Gets or sets the data, or column name for the ordinate [y] axis. /// public object OrdinateDataTop { get; set; } /// /// Gets or sets the data, or column name for the ordinate [y] axis. /// public object OrdinateDataBottom { get; set; } /// /// Gets or sets the data, or column name for the abscissa [x] axis. /// public object AbscissaData { get; set; } /// /// The pen used to draw the plot /// public Pen BorderPen { get { return borderPen_; } set { borderPen_ = value; } } /// /// The color of the pen used to draw lines in this plot. /// public Color BorderColor { set { if (borderPen_ != null) { borderPen_.Color = value; } else { borderPen_ = new Pen(value); } } get { return borderPen_.Color; } } /// /// Set/Get the fill brush /// public IRectangleBrush FillBrush { get { return rectangleBrush_; } set { rectangleBrush_ = value; } } /// /// Set/Get the width of the bar in physical pixels. /// public float BarWidth { get { return barWidth_; } set { barWidth_ = value; } } /// /// Draws the line plot on a GDI+ surface against the provided x and y axes. /// /// The GDI+ surface on which to draw. /// The X-Axis to draw against. /// The Y-Axis to draw against. public void Draw(Graphics g, PhysicalAxis xAxis, PhysicalAxis yAxis) { SequenceAdapter dataTop = new SequenceAdapter(DataSource, DataMember, OrdinateDataTop, AbscissaData); SequenceAdapter dataBottom = new SequenceAdapter(DataSource, DataMember, OrdinateDataBottom, AbscissaData); ITransform2D t = Transform2D.GetTransformer(xAxis, yAxis); for (int i = 0; i < dataTop.Count; ++i) { PointF physicalBottom = t.Transform(dataBottom[i]); PointF physicalTop = t.Transform(dataTop[i]); if (physicalBottom != physicalTop) { Rectangle r = new Rectangle((int) (physicalBottom.X - BarWidth/2), (int) physicalTop.Y, (int) BarWidth, (int) (physicalBottom.Y - physicalTop.Y)); g.FillRectangle(rectangleBrush_.Get(r), r); g.DrawRectangle(borderPen_, r); } } } /// /// Returns an x-axis that is suitable for drawing this plot. /// /// A suitable x-axis. public Axis SuggestXAxis() { SequenceAdapter dataBottom_ = new SequenceAdapter(DataSource, DataMember, OrdinateDataBottom, AbscissaData); SequenceAdapter dataTop_ = new SequenceAdapter(DataSource, DataMember, OrdinateDataTop, AbscissaData); Axis axis = dataTop_.SuggestXAxis(); axis.LUB(dataBottom_.SuggestXAxis()); return axis; } /// /// Returns a y-axis that is suitable for drawing this plot. /// /// A suitable y-axis. public Axis SuggestYAxis() { SequenceAdapter dataBottom_ = new SequenceAdapter(DataSource, DataMember, OrdinateDataBottom, AbscissaData); SequenceAdapter dataTop_ = new SequenceAdapter(DataSource, DataMember, OrdinateDataTop, AbscissaData); Axis axis = dataTop_.SuggestYAxis(); axis.LUB(dataBottom_.SuggestYAxis()); return axis; } /// /// Draws a representation of this plot in the legend. /// /// The graphics surface on which to draw. /// A rectangle specifying the bounds of the area in the legend set aside for drawing. public virtual void DrawInLegend(Graphics g, Rectangle startEnd) { int smallerHeight = (int) (startEnd.Height*0.5f); int heightToRemove = (int) (startEnd.Height*0.5f); Rectangle newRectangle = new Rectangle(startEnd.Left, startEnd.Top + smallerHeight/2, startEnd.Width, smallerHeight); g.FillRectangle(rectangleBrush_.Get(newRectangle), newRectangle); g.DrawRectangle(borderPen_, newRectangle); } /// /// Write data associated with the plot as text. /// /// the string builder to write to. /// Only write out data in this region if onlyInRegion is true. /// If true, only data in region is written, else all data is written. /// TODO: not implemented. public void WriteData(StringBuilder sb, RectangleD region, bool onlyInRegion) { sb.Append("Write data not implemented yet for BarPlot\r\n"); } } }