Skip to content

RadialGaugePlot: System.Drawing throws OutOfMemoryException when drawing small arcs #1474

@arthurits

Description

@arthurits

Bug Report

Issue: an "OutOfMemoryException" is fired whenever the SweepAngle property in any of the gauges's is equal or below 0.01.

image

Explanation: according to this post it seems that DrawArc doesn't handle very well the combination of pen width and small sweep angles.

gfx.DrawArc(
pen,
(center.X - radius),
(center.Y - radius),
(radius * 2),
(radius * 2),
(float)StartAngle,
(float)SweepAngle);

Reproducing: using these values, the DrawArc function fails for sweep angles equal of below 0.01. However, using less values (less gauges, which result in a wider pen width does modify the 0.01 threshold limit.

var plt = new ScottPlot.Plot(600, 400);
double[] values = { 505, 590, 0.005, 0.005, 0.0005, 0.005, 0.005, 0.0005 };
plt.AddRadialGauge(values);

Solution: either check for values lower than 0.01 when creating the plot's RadialGauge instances

RadialGauge gauge = new()
{
MaximumSizeAngle = MaximumAngle,
StartAngle = startAngles[index],
SweepAngle = sweepAngles[index],
Color = Colors[index],
BackgroundColor = Color.FromArgb(backgroundAlpha, Colors[index]),
Width = gaugeWidthPx,
CircularBackground = CircularBackground,
Clockwise = Clockwise,
BackStartAngle = StartingAngleBackGauges,
StartCap = StartCap,
EndCap = EndCap,
Mode = GaugeMode,
Font = Font,
FontSizeFraction = FontSizeFraction,
Label = Levels[index].ToString(LevelTextFormat),
LabelPositionFraction = LabelPositionFraction,
ShowLabels = ShowLevels,
};

SweepAngle = sweepAngles[index] <= 0.01 ? 0 :  sweepAngles[index],

or make the checking when calling DrawArc inside the RenderGaugeForeground function (it might be wiser to do the same inside RenderBackground):

gfx.DrawArc(
                pen,
                (center.X - radius),
                (center.Y - radius),
                (radius * 2),
                (radius * 2),
                (float)StartAngle,
                SweepAngle <= 0.01 ? 0 : (float)SweepAngle);

These suggestions are GDI+ dependant, meaning that they might not be needed whenever ScottPlot is ported to other graphics library.

System Details

  • ScottPlot Version: 4.1.27
  • Operating System: Windows 11
  • Application Type: WinForms
  • .NET Version: .NET 6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BUGunexpected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions