Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start work on line chart #760

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Source/MF.Charts.slnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"solution": {
"path": "Meadow.Foundation.sln",
"projects": [
"..\\..\\MQTTnet\\Source\\MQTTnet\\MQTTnet.csproj",
"..\\..\\Meadow.Contracts\\Source\\Meadow.Contracts\\Meadow.Contracts.csproj",
"..\\..\\Meadow.Core\\source\\Meadow.Core\\Meadow.Core.csproj",
"..\\..\\Meadow.Core\\source\\implementations\\f7\\Meadow.F7\\Meadow.F7.csproj",
"..\\..\\Meadow.Core\\source\\implementations\\windows\\Meadow.Windows\\Meadow.Windows.csproj",
"..\\..\\Meadow.Logging\\Source\\Meadow.Logging\\lib\\Meadow.Logging.csproj",
"..\\..\\Meadow.Modbus\\src\\Meadow.Modbus\\Meadow.Modbus.csproj",
"..\\..\\Meadow.Units\\Source\\Meadow.Units\\Meadow.Units.csproj",
"Meadow.Foundation.Core\\Meadow.Foundation.Core.csproj",
"Meadow.Foundation.Libraries_and_Frameworks\\Graphics.MicroGraphics\\Driver\\Graphics.MicroGraphics.csproj",
"Meadow.Foundation.Libraries_and_Frameworks\\Graphics.MicroLayout\\Driver\\Graphics.MicroLayout.csproj"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;

namespace Meadow.Foundation.Graphics.MicroLayout;

/// <summary>
/// Extension methods for Chart objects
/// </summary>
public static class ChartExtensions
{
/// <summary>
/// Converts a 2-dimensional array of doubles to a LineCHartSeries
/// </summary>
/// <param name="xyData">the data in X,Y pairs</param>
public static LineChartSeries ToLineChartSeries(this double[,] xyData)
{
if (xyData.Rank != 2) throw new ArgumentException("Expected a 2-dimensional array");

var series = new LineChartSeries
{
LineStroke = 4,
LineColor = Color.Green,
ShowLines = true,
PointSize = 5,
PointColor = Color.Yellow,
ShowPoints = true
};

var points = xyData.GetLength(0);

for (var i = 0; i < points; i++)
{
series.Points.Add(new LineSeriesPoint(xyData[i, 0], xyData[i, 1]));
}

return series;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
using System.Linq;

namespace Meadow.Foundation.Graphics.MicroLayout;

/// <summary>
/// An X/Y Line chart
/// </summary>
public class DisplayLineChart : ThemedDisplayControl
{
/// <summary>
/// The default color for axis lines
/// </summary>
public static Color DefaultAxisColor = Color.Gray;
/// <summary>
/// The default color for axis labels
/// </summary>
public static Color DefaultAxisLabelColor = Color.White;
/// <summary>
/// The default chart background color
/// </summary>
public static Color DefaultBackgroundColor = Color.Black;

private const int DefaultMargin = 5;
private const int DefaultAxisStroke = 4;

private IFont _axisFont = default!;

/// <summary>
/// When true, Y-value origin (zero) is always displayed, otherwise the Y axis is scaled based on the data range.
/// </summary>
public bool AlwaysShowYOrigin { get; set; } = true;
/// <summary>
/// The IFont used to for displaying axis labels
/// </summary>
public IFont? AxisFont { get; set; }
/// <summary>
/// The chart's background color
/// </summary>
public Color BackgroundColor { get; set; } = DefaultBackgroundColor;
/// <summary>
/// The color used to draw the chart axes lines
/// </summary>
public Color AxisColor { get; set; } = DefaultAxisColor;
/// <summary>
/// The color used to draw the chart axes labels
/// </summary>
public Color AxisLabelColor { get; set; } = DefaultAxisLabelColor;
/// <summary>
/// The width of the axes lines
/// </summary>
public int AxisStroke { get; set; } = DefaultAxisStroke;

// public bool ShowXAxisLabels { get; set; }
/// <summary>
/// When true, Y-axis labels will be shown
/// </summary>
public bool ShowYAxisLabels { get; set; }
/// <summary>
/// The collection of data series to plot
/// </summary>
public LineChartSeriesCollection Series { get; set; } = new();

private double VerticalScale { get; set; } // pixels per Y units
private double YMinimumValue { get; set; } // Y units at bottom of chart
private double YMaximumValue { get; set; } // Y units at top of chart
private double XAxisYIntercept { get; set; }
private int XAxisScaledPosition { get; set; }
private double HorizontalScale { get; set; } // pixels per X units
private double HorizontalMinimum { get; set; } // X units at left of chart
private int ChartAreaHeight { get; set; }
private int ChartAreaWidth { get; set; }
private int ChartAreaLeft { get; set; }
private int ChartAreaTop { get; set; }
private int ChartAreaBottom { get; set; }

/// <summary>
/// Creates a DisplayLineChart instance
/// </summary>
/// <param name="left">The control's left position</param>
/// <param name="top">The control's top position</param>
/// <param name="width">The control's width</param>
/// <param name="height">The control's height</param>
public DisplayLineChart(int left, int top, int width, int height)
: base(left, top, width, height)
{
}

/// <inheritdoc/>
public override void ApplyTheme(DisplayTheme theme)
{
}

/// <inheritdoc/>
protected override void OnDraw(MicroGraphics graphics)
{
graphics.DrawRectangle(Left, Top, Width, Height, BackgroundColor, true);

ChartAreaTop = Top + DefaultMargin;
ChartAreaBottom = Bottom - DefaultMargin;

// determine overall min/max
var minX = Series.Min(s => s.Points.MinX);
var maxX = Series.Max(s => s.Points.MaxX);


if (AlwaysShowYOrigin)
{
var min = Series.Min(s => s.Points.MinY);
var max = Series.Max(s => s.Points.MaxY);

if (min > 0)
{
YMinimumValue = 0;
YMaximumValue = max;
}
else if (max < 0)
{
YMinimumValue = min;
YMaximumValue = 0;
}
else
{
YMinimumValue = min;
YMaximumValue = max;
}
}
else
{
YMinimumValue = Series.Min(s => s.Points.MinY);
YMaximumValue = Series.Max(s => s.Points.MaxY);
}

ChartAreaHeight = this.Height - (2 * DefaultMargin) - (DefaultAxisStroke / 2);
VerticalScale = ChartAreaHeight / (YMaximumValue - YMinimumValue); // pixels per vertical unit

DrawXAxis(graphics, YMinimumValue, YMaximumValue);
DrawYAxis(graphics);

foreach (var series in Series)
{
DrawSeries(graphics, series);
}

DrawAxisLabels(graphics);

graphics.Show();
}

private void DrawAxisLabels(MicroGraphics graphics)
{
var font = GetAxisFont();

if (ShowYAxisLabels)
{
// axis label
if (XAxisYIntercept != YMinimumValue)
{
graphics.DrawText(
x: DefaultMargin,
y: XAxisScaledPosition - (font.Height / 2), // centered on tick
color: AxisLabelColor,
text: XAxisYIntercept.ToString("0.0"),
font: font);
}

// max label
graphics.DrawText(
x: DefaultMargin,
y: ChartAreaTop + font.Height,
color: AxisLabelColor,
text: YMaximumValue.ToString("0.0"),
font: font);

// min label
graphics.DrawText(
x: DefaultMargin,
y: ChartAreaBottom - font.Height,
color: AxisLabelColor,
text: YMinimumValue.ToString("0.0"),
font: font);
}
}

private void DrawXAxis(MicroGraphics graphics, double minY, double maxY)
{
if (minY < 0 && maxY > 0)
{
// axis is at 0
XAxisYIntercept = 0;

XAxisScaledPosition = Bottom - DefaultMargin - DefaultAxisStroke + (int)(minY * VerticalScale);
}
else
{

// axis at min Y
XAxisYIntercept = YMinimumValue;

XAxisScaledPosition = Bottom - DefaultMargin - DefaultAxisStroke;
}

// for now it's a fixed line at the bottom
graphics.Stroke = DefaultAxisStroke;
graphics.DrawLine(
Left + DefaultMargin,
XAxisScaledPosition,
Right - DefaultMargin,
XAxisScaledPosition,
AxisColor);
}

private IFont GetAxisFont()
{
if (AxisFont == null)
{
_axisFont = new Font8x16();
}
else
{
_axisFont = AxisFont;
}

return _axisFont;
}

private void DrawYAxis(MicroGraphics graphics)
{
var leftMargin = DefaultMargin + AxisStroke;

if (ShowYAxisLabels)
{
// TODO: this needs to be label-based
leftMargin += GetAxisFont().Width * 4;
}

// TODO: deal with chart with negative values

ChartAreaLeft = leftMargin;
ChartAreaWidth = Width - ChartAreaLeft - DefaultMargin;

// for now it's a fixed line at the left
graphics.Stroke = DefaultAxisStroke;
graphics.DrawLine(
ChartAreaLeft,
Top + DefaultMargin,
ChartAreaLeft,
Bottom - DefaultMargin,
AxisColor);
}

private void DrawSeries(MicroGraphics graphics, LineChartSeries series)
{
var minX = series.Points.MinX;
var minY = series.Points.MinY;
var xRange = series.Points.MaxX - minX;
var yRange = series.Points.MaxY; // - minY; // assuming axis at 0 right now

LineSeriesPoint lastPoint = new LineSeriesPoint();
var first = true;

graphics.Stroke = series.LineStroke;

foreach (var point in series.Points)
{
var scaledX = ChartAreaLeft + (int)(point.X / xRange * ChartAreaWidth) + DefaultMargin;
var scaledY = Bottom - DefaultMargin - (DefaultAxisStroke / 2) - (int)((point.Y - YMinimumValue) * VerticalScale);

if (series.ShowLines)
{
if (first)
{
first = false;
}
else
{
graphics.DrawLine(
(int)lastPoint.X,
(int)lastPoint.Y,
scaledX,
scaledY,
series.LineColor);
}

lastPoint.X = scaledX;
lastPoint.Y = scaledY;
}

if (series.ShowPoints)
{
graphics.DrawCircle(scaledX, scaledY, series.PointSize, series.PointColor, true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Meadow.Foundation.Graphics.MicroLayout;

/// <summary>
/// A series for a DisplayLineChart
/// </summary>
public class LineChartSeries
{
/// <summary>
/// Gets or sets whether lines are displayed between points
/// </summary>
public bool ShowLines { get; set; }
/// <summary>
/// Gets or sets the color of lines between points
/// </summary>
public Color LineColor { get; set; }
/// <summary>
/// Gets or sets the width of lines between points
/// </summary>
public int LineStroke { get; set; }

/// <summary>
/// Gets or sets whether markers are displayed for points
/// </summary>
public bool ShowPoints { get; set; }
/// <summary>
/// Gets or sets the color of markers displayed for points
/// </summary>
public Color PointColor { get; set; }
/// <summary>
/// Gets or sets the size of markers displayed for points
/// </summary>
public int PointSize { get; set; }

/// <summary>
/// Gets or sets the points in the series
/// </summary>
public LineSeriesPointCollection Points { get; set; } = new();
}
Loading
Loading