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

Fix and improve MicroLayout Linechart control #932

Merged
merged 3 commits into from
Mar 20, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected override void OnDraw(MicroGraphics graphics)
{
graphics.DrawRectangle(Left, Top, Width, Height, BackgroundColor, true);

ChartAreaTop = Top + DefaultMargin;
ChartAreaTop = Top + DefaultMargin * 2 - AxisStroke;
ChartAreaBottom = Bottom - DefaultMargin;

// determine overall min/max
Expand Down Expand Up @@ -136,7 +136,7 @@ protected override void OnDraw(MicroGraphics graphics)
YMaximumValue = (ymax > 0) ? ymax * 1.1 : ymax * 0.9;
}

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

DrawYAxis(graphics);
Expand Down Expand Up @@ -172,7 +172,7 @@ private void DrawAxisLabels(MicroGraphics graphics)
// max label
graphics.DrawText(
x: Left + DefaultMargin + ParentOffsetX,
y: ChartAreaTop + font.Height + ParentOffsetY,
y: ChartAreaTop - font.Height + DefaultMargin,
color: AxisLabelColor,
text: YMaximumValue.ToString("0.0"),
font: font);
Expand All @@ -194,31 +194,30 @@ private void DrawXAxis(MicroGraphics graphics, double minY, double maxY)
// axis is at 0
XAxisYIntercept = 0;

XAxisScaledPosition = Bottom - DefaultMargin - DefaultAxisStroke + (int)(minY * VerticalScale);
XAxisScaledPosition = Bottom - DefaultMargin + (int)(minY * VerticalScale);
}
else
{
// axis at min Y
XAxisYIntercept = YMinimumValue;

XAxisScaledPosition = Bottom - DefaultMargin - DefaultAxisStroke;
XAxisScaledPosition = ChartAreaBottom - DefaultMargin * 2 + AxisStroke * 2;
}

// for now it's a fixed line at the bottom
graphics.Stroke = DefaultAxisStroke;
graphics.DrawLine(
ChartAreaLeft + ParentOffsetX,
XAxisScaledPosition + ParentOffsetY,
Right - DefaultMargin,
graphics.Stroke = AxisStroke;
graphics.DrawHorizontalLine(
ChartAreaLeft, //ChartAreaLeft - AxisStroke + ParentOffsetX,
adrianstevens marked this conversation as resolved.
Show resolved Hide resolved
XAxisScaledPosition,
ChartAreaWidth,
AxisColor);
}

private IFont GetAxisFont()
{
if (AxisFont == null)
{
_axisFont = new Font8x16();
_axisFont = new Font6x8();
}
else
{
Expand All @@ -239,17 +238,15 @@ private void DrawYAxis(MicroGraphics graphics)
}

// TODO: deal with chart with negative values

ChartAreaLeft = Left + leftMargin;
ChartAreaWidth = Width - ChartAreaLeft - DefaultMargin - DefaultAxisStroke * 2;
ChartAreaLeft = Left + leftMargin + AxisStroke;
ChartAreaWidth = Right - ChartAreaLeft - DefaultMargin - AxisStroke;

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

Expand All @@ -260,15 +257,15 @@ private void DrawSeries(MicroGraphics graphics, LineChartSeries series)
var xRange = series.Points.MaxX - minX;
var yRange = series.Points.MaxY; // - minY; // assuming axis at 0 right now

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

graphics.Stroke = series.LineStroke;

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

if (series.ShowLines)
{
Expand All @@ -292,8 +289,13 @@ private void DrawSeries(MicroGraphics graphics, LineChartSeries series)

if (series.ShowPoints)
{
graphics.DrawCircle(scaledX + ParentOffsetX, scaledY + ParentOffsetY, series.PointSize, series.PointColor, true);
graphics.DrawCircle(
scaledX + ParentOffsetX,
scaledY + ParentOffsetY,
series.PointSize,
series.PointColor,
true);
}
}
}
}
}
Loading