Skip to content

RenderLegend() exception if no labeled plots #1257

@swharden

Description

@swharden

This works:

formsPlot1.Plot.AddSignal(DataGen.Sin(51), label: "sin");
formsPlot1.Plot.AddSignal(DataGen.Cos(51), label: "cos");
formsPlot1.Render();
pictureBox1.Image = formsPlot1.Plot.RenderLegend();

This throws an exception:

formsPlot1.Plot.AddSignal(DataGen.Sin(51));
formsPlot1.Plot.AddSignal(DataGen.Cos(51));
formsPlot1.Render();
pictureBox1.Image = formsPlot1.Plot.RenderLegend();
System.ArgumentException
  HResult=0x80070057
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
   at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
   at ScottPlot.Renderable.Legend.GetBitmap(Boolean lowQuality, Double scale) in C:\Users\scott\Documents\GitHub\ScottPlot\src\ScottPlot\Renderable\Legend.cs:line 68
   at ScottPlot.Plot.RenderLegend(Boolean lowQuality, Double scale) in C:\Users\scott\Documents\GitHub\ScottPlot\src\ScottPlot\Plot\Plot.Render.cs:line 197
   at WinFormsFrameworkApp.Form1..ctor() in C:\Users\scott\Documents\GitHub\ScottPlot\src\sandbox\WinFormsFrameworkApp\Form1.cs:line 18
   at WinFormsFrameworkApp.Program.Main() in C:\Users\scott\Documents\GitHub\ScottPlot\src\sandbox\WinFormsFrameworkApp\Program.cs:line 19

Bitmap bmp = new(width, height, PixelFormat.Format32bppPArgb);

Fix this bug by returning a null if there are no items in the legend

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