using System.Collections.ObjectModel;
namespace Terminal.Gui;
#nullable enable
/// Describes a series of data that can be rendered into a >
public interface ISeries
{
///
/// Draws the section of a series into the view
///
///
/// Graph series is to be drawn onto
/// Visible area of the graph in Console Screen units (excluding margins)
/// Visible area of the graph in Graph space units
void DrawSeries (GraphView graph, Rectangle drawBounds, RectangleF graphBounds);
}
/// Series composed of any number of discrete data points
public class ScatterSeries : ISeries
{
///
/// The color and character that will be rendered in the console when there are point(s) in the corresponding
/// graph space. Defaults to uncolored 'dot'
///
public GraphCellToRender Fill { get; set; } = new (Glyphs.Dot);
/// Collection of each discrete point in the series
///
public List Points { get; set; } = new ();
/// Draws all points directly onto the graph
public void DrawSeries (GraphView graph, Rectangle drawBounds, RectangleF graphBounds)
{
if (Fill.Color.HasValue)
{
graph.SetAttribute (Fill.Color.Value);
}
foreach (PointF p in Points.Where (p => graphBounds.Contains (p)))
{
Point screenPoint = graph.GraphSpaceToScreen (p);
graph.AddRune (screenPoint.X, screenPoint.Y, Fill.Rune);
}
}
}
/// Collection of in which bars are clustered by category
public class MultiBarSeries : ISeries
{
private readonly BarSeries [] subSeries;
/// Creates a new series of clustered bars.
/// Each category has this many bars
/// How far apart to put each category (in graph space)
///
/// How much spacing between bars in a category (should be less than /
/// )
///
///
/// Array of colors that define bar color in each category. Length must match
///
///
public MultiBarSeries (int numberOfBarsPerCategory, float barsEvery, float spacing, Attribute []? colors = null)
{
subSeries = new BarSeries [numberOfBarsPerCategory];
if (colors is { } && colors.Length != numberOfBarsPerCategory)
{
throw new ArgumentException (
"Number of colors must match the number of bars",
nameof (numberOfBarsPerCategory)
);
}
for (var i = 0; i < numberOfBarsPerCategory; i++)
{
subSeries [i] = new BarSeries ();
subSeries [i].BarEvery = barsEvery;
subSeries [i].Offset = i * spacing;
// Only draw labels for the first bar in each category
subSeries [i].DrawLabels = i == 0;
if (colors is { })
{
subSeries [i].OverrideBarColor = colors [i];
}
}
Spacing = spacing;
}
///
/// The number of units of graph space between bars. Should be less than
///
public float Spacing { get; }
///
/// Sub collections. Each series contains the bars for a different category. Thus SubSeries[0].Bars[0] is the
/// first bar on the axis and SubSeries[1].Bars[0] is the second etc.
///
public IReadOnlyCollection SubSeries => new ReadOnlyCollection (subSeries);
/// Draws all
///
///
///
public void DrawSeries (GraphView graph, Rectangle drawBounds, RectangleF graphBounds)
{
foreach (BarSeries bar in subSeries)
{
bar.DrawSeries (graph, drawBounds, graphBounds);
}
}
/// Adds a new cluster of bars
///
///
/// Values for each bar in category, must match the number of bars per category
public void AddBars (string label, Rune fill, params float [] values)
{
if (values.Length != subSeries.Length)
{
throw new ArgumentException (
"Number of values must match the number of bars per category",
nameof (values)
);
}
for (var i = 0; i < values.Length; i++)
{
subSeries [i]
.Bars.Add (
new BarSeriesBar (
label,
new GraphCellToRender (fill),
values [i]
)
);
}
}
}
/// Series of bars positioned at regular intervals
public class BarSeries : ISeries
{
///
/// Determines the spacing of bars along the axis. Defaults to 1 i.e. every 1 unit of graph space a bar is
/// rendered. Note that you should also consider when changing this.
///
public float BarEvery { get; set; } = 1;
/// Ordered collection of graph bars to position along axis
public List Bars { get; set; } = new ();
/// True to draw along the axis under the bar. Defaults to true.
public bool DrawLabels { get; set; } = true;
///
/// The number of units of graph space along the axis before rendering the first bar (and subsequent bars - see
/// ). Defaults to 0
///
public float Offset { get; set; }
/// Direction bars protrude from the corresponding axis. Defaults to vertical
public Orientation Orientation { get; set; } = Orientation.Vertical;
/// Overrides the with a fixed color
public Attribute? OverrideBarColor { get; set; }
/// Draws bars that are currently in the
///
/// Screen area of the graph excluding margins
/// Graph space area that should be drawn into
public virtual void DrawSeries (GraphView graph, Rectangle drawBounds, RectangleF graphBounds)
{
for (var i = 0; i < Bars.Count; i++)
{
float xStart = Orientation == Orientation.Horizontal ? 0 : Offset + (i + 1) * BarEvery;
float yStart = Orientation == Orientation.Horizontal ? Offset + (i + 1) * BarEvery : 0;
float endX = Orientation == Orientation.Horizontal ? Bars [i].Value : xStart;
float endY = Orientation == Orientation.Horizontal ? yStart : Bars [i].Value;
// translate to screen positions
Point screenStart = graph.GraphSpaceToScreen (new PointF (xStart, yStart));
Point screenEnd = graph.GraphSpaceToScreen (new PointF (endX, endY));
// Start the bar from wherever the axis is
if (Orientation == Orientation.Horizontal)
{
screenStart.X = graph.AxisY.GetAxisXPosition (graph);
// don't draw bar off the right of the control
screenEnd.X = Math.Min (graph.Viewport.Width - 1, screenEnd.X);
// if bar is off the screen
if (screenStart.Y < 0 || screenStart.Y > drawBounds.Height - graph.MarginBottom)
{
continue;
}
}
else
{
// Start the axis
screenStart.Y = graph.AxisX.GetAxisYPosition (graph);
// don't draw bar up above top of control
screenEnd.Y = Math.Max (0, screenEnd.Y);
// if bar is off the screen
if (screenStart.X < graph.MarginLeft || screenStart.X > graph.MarginLeft + drawBounds.Width - 1)
{
continue;
}
}
// draw the bar unless it has no height
if (Bars [i].Value != 0)
{
DrawBarLine (graph, screenStart, screenEnd, Bars [i]);
}
// If we are drawing labels and the bar has one
if (DrawLabels && !string.IsNullOrWhiteSpace (Bars [i].Text))
{
// Add the label to the relevant axis
if (Orientation == Orientation.Horizontal)
{
graph.AxisY.DrawAxisLabel (graph, screenStart.Y, Bars [i].Text);
}
else if (Orientation == Orientation.Vertical)
{
graph.AxisX.DrawAxisLabel (graph, screenStart.X, Bars [i].Text);
}
}
}
}
/// Applies any color overriding
///
///
protected virtual GraphCellToRender AdjustColor (GraphCellToRender graphCellToRender)
{
if (OverrideBarColor.HasValue)
{
graphCellToRender.Color = OverrideBarColor;
}
return graphCellToRender;
}
/// Override to do custom drawing of the bar e.g. to apply varying color or changing the fill symbol mid bar.
///
/// Screen position of the start of the bar
/// Screen position of the end of the bar
/// The Bar that occupies this space and is being drawn
protected virtual void DrawBarLine (GraphView graph, Point start, Point end, BarSeriesBar beingDrawn)
{
GraphCellToRender adjusted = AdjustColor (beingDrawn.Fill);
if (adjusted.Color.HasValue)
{
graph.SetAttribute (adjusted.Color.Value);
}
graph.DrawLine (start, end, adjusted.Rune);
graph.SetDriverColorToGraphColor ();
}
}