/* * NPlot - A charting library for .NET * * HorizontalLine.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; using System.Drawing; using System.Text; namespace NPlot { /// /// Encapsulates functionality for drawing a horizontal line on a plot surface. /// public class HorizontalLine : IPlot { private string label_ = ""; private Pen pen_ = new Pen(Color.Black); private int pixelIndent_; private float scale_ = 1.0f; private double value_; /// /// Constructor /// /// ordinate (Y) value of line. public HorizontalLine(double ordinateValue) { value_ = ordinateValue; } /// /// Constructor /// /// ordinate (Y) value of line. /// draw the line using this color. public HorizontalLine(double ordinateValue, Color color) { value_ = ordinateValue; pen_ = new Pen(color); } /// /// Constructor /// /// ordinate (Y) value of line. /// Pen to use to draw the line. public HorizontalLine(double ordinateValue, Pen pen) { value_ = ordinateValue; pen_ = pen; } /// /// ordinate (Y) value to draw horizontal line at. /// public double OrdinateValue { get { return value_; } set { value_ = value; } } /// /// Pen to use to draw the horizontal line. /// public Pen Pen { get { return pen_; } set { pen_ = value; } } /// /// Each end of the line is indented by this many pixels. /// public int PixelIndent { get { return pixelIndent_; } set { pixelIndent_ = value; } } /// /// The line length is multiplied by this amount. Default /// corresponds to a value of 1.0. /// public float LengthScale { get { return scale_; } set { scale_ = value; } } /// /// Draws a representation of the horizontal line 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 void DrawInLegend(Graphics g, Rectangle startEnd) { g.DrawLine(pen_, startEnd.Left, (startEnd.Top + startEnd.Bottom)/2, startEnd.Right, (startEnd.Top + startEnd.Bottom)/2); } /// /// A label to associate with the plot - used in the legend. /// public string Label { get { return label_; } set { label_ = value; } } /// /// Whether or not to include an entry for this plot in the legend if it exists. /// public bool ShowInLegend { get; set; } /// /// Returns null indicating that x extremities of the line are variable. /// /// null public Axis SuggestXAxis() { return null; } /// /// Returns a y-axis that is suitable for drawing this plot. /// /// A suitable y-axis. public Axis SuggestYAxis() { return new LinearAxis(value_, value_); } /// /// Writes text data describing the horizontal line object to the supplied string builder. It is /// possible to specify that the data will be written only if the line is in the specified /// region. /// /// the StringBuilder object to write to. /// a region used if onlyInRegion is true. /// If true, data will be written only if the line is in the specified region. public void WriteData(StringBuilder sb, RectangleD region, bool onlyInRegion) { // return if line is not in plot region and if (value_ > region.Y + region.Height || value_ < region.Y) { if (onlyInRegion) { return; } } sb.Append("Label: "); sb.Append(Label); sb.Append("\r\n"); sb.Append(value_.ToString()); sb.Append("\r\n"); } /// /// Draws the horizontal 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) { int xMin = xAxis.PhysicalMin.X; int xMax = xAxis.PhysicalMax.X; xMin += pixelIndent_; xMax -= pixelIndent_; float length = Math.Abs(xMax - xMin); float lengthDiff = length - length*scale_; float indentAmount = lengthDiff/2; xMin += (int) indentAmount; xMax -= (int) indentAmount; int yPos = (int) yAxis.WorldToPhysical(value_, false).Y; g.DrawLine(pen_, new Point(xMin, yPos), new Point(xMax, yPos)); // todo: clip and proper logic for flipped axis min max. } } }