/*
* NPlot - A charting library for .NET
*
* VerticalLine.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 vertical line on a plot surface.
///
public class VerticalLine : IPlot
{
private string label_ = "";
private Pen pen_ = new Pen(Color.Black);
private int pixelIndent_;
private float scale_ = 1.0f;
private double value_;
///
/// Constructor
///
/// abscissa (X) value of line.
public VerticalLine(double abscissaValue)
{
value_ = abscissaValue;
}
///
/// Constructor
///
/// abscissa (X) value of line.
/// draw the line using this color.
public VerticalLine(double abscissaValue, Color color)
{
value_ = abscissaValue;
pen_ = new Pen(color);
}
///
/// Constructor
///
/// abscissa (X) value of line.
/// Pen to use to draw the line.
public VerticalLine(double abscissaValue, Pen pen)
{
value_ = abscissaValue;
pen_ = pen;
}
///
/// abscissa (X) value to draw horizontal line at.
///
public double AbscissaValue
{
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 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 an x-axis that is suitable for drawing this plot.
///
/// A suitable x-axis.
public Axis SuggestXAxis()
{
return new LinearAxis(value_, value_);
}
///
/// Returns null indicating that y extremities of the line are variable.
///
/// null
public Axis SuggestYAxis()
{
return null;
}
///
/// Writes text data describing the vertical 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.X + region.Width || value_ < region.X)
{
if (onlyInRegion)
{
return;
}
}
sb.Append("Label: ");
sb.Append(Label);
sb.Append("\r\n");
sb.Append(value_.ToString());
sb.Append("\r\n");
}
///
/// Draws the vertical 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 yMin = yAxis.PhysicalMin.Y;
int yMax = yAxis.PhysicalMax.Y;
yMin -= pixelIndent_;
yMax += pixelIndent_;
float length = Math.Abs(yMax - yMin);
float lengthDiff = length - length*scale_;
float indentAmount = lengthDiff/2;
yMin -= (int) indentAmount;
yMax += (int) indentAmount;
int xPos = (int) xAxis.WorldToPhysical(value_, false).X;
g.DrawLine(pen_, new Point(xPos, yMin), new Point(xPos, yMax));
// todo: clip and proper logic for flipped axis min max.
}
}
}