Skip to content

Instantly share code, notes, and snippets.

@MareMare
Last active April 19, 2022 05:48
Show Gist options
  • Save MareMare/27160d80beb51dcadd1a5e6251f8e67f to your computer and use it in GitHub Desktop.
Save MareMare/27160d80beb51dcadd1a5e6251f8e67f to your computer and use it in GitHub Desktop.
ScottPlot: How to remove list of text labels
using ScottPlot;
using ScottPlot.Plottable;
namespace ScottPlot_Issue1784;
public partial class Form1 : Form
{
private readonly List<IPlottable> _texts = new ();
public Form1()
{
InitializeComponent();
this.buttonToAddTexts.Click += ButtonToAddTexts_Click;
this.buttonToRemoveTexts.Click += ButtonToRemoveTexts_Click;
var plot = this.formsPlot1.Plot;
// display some interesting data in the background
plot.AddSignal(DataGen.Sin(51), label: "sin");
plot.AddSignal(DataGen.Cos(51), label: "cos");
// add a legend to the corner
var legend = plot.Legend();
legend.FontName = "comic sans ms";
legend.FontSize = 18;
legend.FontBold = true;
legend.FontColor = Color.DarkBlue;
this.formsPlot1.Refresh();
}
private void AddTexts()
{
var plot = this.formsPlot1.Plot;
// add text with custom fonts
_texts.Add(plot.AddText("very graph", 25, .8, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 24, Color = Color.Blue, Bold = true }));
_texts.Add(plot.AddText("so data", 0, 0, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 42, Color = Color.Magenta, Bold = true }));
_texts.Add(plot.AddText("many documentation", 3, -.6, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 18, Color = Color.DarkBlue, Bold = true }));
_texts.Add(plot.AddText("wow.", 10, .6, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 36, Color = Color.Green, Bold = true }));
_texts.Add(plot.AddText("NuGet", 32, 0, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 24, Color = Color.Gold, Bold = true }));
this.formsPlot1.Refresh();
}
private void RemoveTexts()
{
var plot = this.formsPlot1.Plot;
// remove texts
_texts.ForEach(plot.Remove);
_texts.Clear();
this.formsPlot1.Refresh();
}
private void ButtonToAddTexts_Click(object? sender, EventArgs e) => AddTexts();
private void ButtonToRemoveTexts_Click(object? sender, EventArgs e) => RemoveTexts();
}

ScottPlot/ScottPlot#1784(comment)

First, store the text added with Plot.AddText() at the level of the control. Next, when you want to remove the text, you can use the Plot.Remove() method.

private readonly List<IPlottable> _texts = new ();

private void AddTexts()
{
    var plot = this.formsPlot1.Plot;

    // add text with custom fonts
    _texts.Add(plot.AddText("very graph", 25, .8, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 24, Color = Color.Blue, Bold = true }));
    _texts.Add(plot.AddText("so data", 0, 0, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 42, Color = Color.Magenta, Bold = true }));
    _texts.Add(plot.AddText("many documentation", 3, -.6, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 18, Color = Color.DarkBlue, Bold = true }));
    _texts.Add(plot.AddText("wow.", 10, .6, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 36, Color = Color.Green, Bold = true }));
    _texts.Add(plot.AddText("NuGet", 32, 0, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 24, Color = Color.Gold, Bold = true }));

    this.formsPlot1.Refresh();
}

private void RemoveTexts()
{
    var plot = this.formsPlot1.Plot;

    // remove texts
    _texts.ForEach(plot.Remove);
    _texts.Clear();

    this.formsPlot1.Refresh();
}
Example Code.(Winforms)
using ScottPlot;
using ScottPlot.Plottable;

namespace ScottPlot_Issue1784;

public partial class Form1 : Form
{
    private readonly List<IPlottable> _texts = new ();

    public Form1()
    {
        InitializeComponent();
        this.buttonToAddTexts.Click += ButtonToAddTexts_Click;
        this.buttonToRemoveTexts.Click += ButtonToRemoveTexts_Click;

        var plot = this.formsPlot1.Plot;

        // display some interesting data in the background
        plot.AddSignal(DataGen.Sin(51), label: "sin");
        plot.AddSignal(DataGen.Cos(51), label: "cos");

        // add a legend to the corner
        var legend = plot.Legend();
        legend.FontName = "comic sans ms";
        legend.FontSize = 18;
        legend.FontBold = true;
        legend.FontColor = Color.DarkBlue;

        this.formsPlot1.Refresh();
    }

    private void AddTexts()
    {
        var plot = this.formsPlot1.Plot;

        // add text with custom fonts
        _texts.Add(plot.AddText("very graph", 25, .8, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 24, Color = Color.Blue, Bold = true }));
        _texts.Add(plot.AddText("so data", 0, 0, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 42, Color = Color.Magenta, Bold = true }));
        _texts.Add(plot.AddText("many documentation", 3, -.6, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 18, Color = Color.DarkBlue, Bold = true }));
        _texts.Add(plot.AddText("wow.", 10, .6, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 36, Color = Color.Green, Bold = true }));
        _texts.Add(plot.AddText("NuGet", 32, 0, new ScottPlot.Drawing.Font() { Name = "comic sans ms", Size = 24, Color = Color.Gold, Bold = true }));

        this.formsPlot1.Refresh();
    }

    private void RemoveTexts()
    {
        var plot = this.formsPlot1.Plot;

        // remove texts
        _texts.ForEach(plot.Remove);
        _texts.Clear();

        this.formsPlot1.Refresh();
    }

    private void ButtonToAddTexts_Click(object? sender, EventArgs e) => AddTexts();

    private void ButtonToRemoveTexts_Click(object? sender, EventArgs e) => RemoveTexts();
}

Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment