question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting the value(object/index/properties) of the PlotVLine I just dragged?

See original GitHub issue

How do I get the value(object/index/properties) of the PlotVLine I just dragged, or am presently dragging in either realtime for MouseDown, or on MouseUp?

It would be nice to know what it’s exact location for the xAxis (yet again). I can loop through all of the PlotLines and get the values, but knowing that actual line and addressing it directly would be better.

I have added the following to my code, to know when the mouse is up on the control, but, it doesn’t give me much at all.

  public Form1()
  {
        InitializeComponent();

        this.formsPlot1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.formsPlot1_MouseUp);
}

and

private void formsPlot1_MouseUp(object sender, MouseEventArgs e)
{
      Console.WriteLine("MouseUp");
      Console.WriteLine(sender.ToString());
      Console.WriteLine(e.ToString());

     // This would be ideal, though it will fail at this present time.
      var X = sender.PlotVLine.X;

    // ... update values based on X etc 
}

This next bit, I realise this may be the wrong, or not the original intended way of doing this, but I couldn’t find any other way… it seems to work for individual elements for a short search, but not at all good for real-time. Basically, it you need to find the index of a known ‘label’.

formsPlot1.plt.PlotVLine(label: "green", lineWidth: 3, x: position, draggable: true, dragLimitLower: 0, dragLimitUpper: 100000);                            
String plotname = "";

int plottablescount = formsPlot1.plt.GetSettings(false).plottables.Count; 
for (int i = 0; i < plottablescount; i++)
{
                                
Console.WriteLine(formsPlot1.plt.GetSettings(false).plottables[i].ToString());      //  PlottableVLine (green) at X=516587
Console.WriteLine(formsPlot1.plt.GetSettings(false).plottables[i].GetLegendItems());//  ScottPlot.Config.LegendItem[]
Console.WriteLine(formsPlot1.plt.GetSettings(false).plottables[i].GetLimits());     //  x1=516587.000, x2=516587.000, y1=NaN, y2=NaN
Console.WriteLine(formsPlot1.plt.GetSettings(false).plottables[i].GetType());       //  ScottPlot.PlottableVLine
Console.WriteLine(formsPlot1.plt.GetSettings(false).plottables[i].GetHashCode());   //  2383799
var stringlabel = formsPlot1.plt.GetSettings(false).plottables[i].ToString();       //  PlottableVLine (green) at X=516587
                                
Console.WriteLine(Regex.Match(stringlabel, @"\(([^)]*)\)").Groups[1].Value); // This will extract the name of the moveable plotline

plotname = Regex.Match(stringlabel, @"\(([^)]*)\)").Groups[1].Value;

if (plotname == "green")
{
    // do stuff as the 'green' label was found
}

}

Thanks again.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
swhardencommented, Sep 13, 2020

Hey @vrdriver, sorry it took so long for me to get to this but I’m really happy that @StendProg was able to provide an excellent and thorough answer.

To summarize the conclusion about how to continuously read the position of dragged axis lines, the best way is to save the object PlotVLine() returns and access its position property whenever you want to know where the line is. Whether reading this property is triggered by a timer or some type of mouse move event is up to you.

Regarding the rendering system, ScottPlot uses System.Drawing to draw a plot as a Bitmap. User controls are just thin wrappers that watch the mouse and request next Bitmap whenever something changes. The appearance of animation comes from ScottPlot being pretty fast at drawing this way, but all interactivity is achieved by creating new Bitmaps. On one hand this makes it really easy to use and it’s supported on many systems, but on the other hand adding advanced interactivity requires writing your own control.

I second @StendProg’s recommendation to use PlotSignalCost() for plotting things like audio data. It is so fast, even with millions of samples, that refreshing the whole screen takes very little time.

I think this answers all the questions raised in this thread so I’ll close this issue, but feel free to open it again if more questions pop up! --Scott

1reaction
vrdrivercommented, Sep 12, 2020

@StendProg . Thanks for helping with guiding me the right direction. It has helped a lot.

As I have multiple vplots, I’ve redirected the entire event, which keeps that real-time thing happening and I’m then able to get all of them separately and detect changes.

PlottableVLine vLine_cue;
PlottableVLine vLine_overlap;
PlottableVLine vLine_intro;
// .. etc

public Form1()
{
    InitializeComponent();
    this.formsPlot1.MouseDragPlottable += (s, e) => { formsPlot1_MouseDragPlottable(s, e); };
} 

private void formsPlot1_MouseDragPlottable(object s, EventArgs e)
{ 
    // This is for detecting the plot lines that have been changed in ScottPlot
 

    // check if each link has changed
    if (plot_Meta_Cue_sample != vLine_cue.position)
    { 
        textBox_vLine_cue.Text = vLine_cue.position.ToString(); // with added formating later
        plot_Meta_Cue_sample = vLine_cue.position;
        button_Save.BackColor = Color.LightPink;
    }
 
    // repeat the above statement for the rest of them... 
    if (plot_Meta_Overlap_sample != vLine_cue.position)
    { 
        textBox_vLine_overlap.Text = vLine_overlap.position.ToString(); // with added formating later
        plot_Meta_Overlap_sample = vLine_overlap.position;
        button_Save.BackColor = Color.LightPink;
    }
 
    // etc
 
}



private void Form1_Load(object sender, EventArgs e)
{ 

    double position = 100;

    vLine_cue = formsPlot1.plt.PlotVLine(label: "vLine_cue", lineWidth: 3, x: position, draggable: true, dragLimitLower: 0, dragLimitUpper:   100000);
    vLine_overlap = formsPlot1.plt.PlotVLine(label: "vLine_overlap", lineWidth: 3, x: position+10, draggable: true, dragLimitLower: 0, dragLimitUpper: 100000);
    vLine_intro = formsPlot1.plt.PlotVLine(label: "vLine_intro", lineWidth: 3, x: position+20, draggable: true, dragLimitLower: 0, dragLimitUpper: 100000);
}

I’ve included this code for others to discover if required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the value(object/index/properties) of the PlotVLine I ...
How do I get the value(object/index/properties) of the PlotVLine I just dragged, or am presently dragging in either realtime for MouseDown, ...
Read more >
How to add data to a Drag object?
I have bunch of food items as Drag objects, and a grocery cart to drop them in. Each item has a bunch of...
Read more >
mplcursors – Interactive data selection cursors for Matplotlib ...
A left click on a line (a point, for plots where the data points are not connected) creates a draggable annotation there. Only...
Read more >
How to Add Drag and Drop in React with React Beautiful ...
We use the source. index value to find our item from our new array and remove it using the splice method. That result...
Read more >
How To Create Drag and Drop Elements with Vanilla ...
In this tutorial, we will build a drag-and-drop example using the HTML Drag and Drop API with vanilla JavaScript to use the event...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found