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.