SHARE
TWEET

OxyPlot clicking bug

a guest Sep 27th, 2012 2 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ===== MainPage.xaml =====
  2.  
  3. <UserControl x:Class="SilverlightApplication3.MainPage"
  4.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  5.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.     xmlns:oxy="clr-namespace:OxyPlot.Silverlight;assembly=OxyPlot.Silverlight"
  9.     mc:Ignorable="d"
  10.     d:DesignHeight="300" d:DesignWidth="400">
  11.  
  12.     <Grid x:Name="LayoutRoot" Background="White">
  13.         <oxy:Plot x:Name="FFTplotter"/>
  14.     </Grid>
  15. </UserControl>
  16.  
  17. ===== MainPage.xaml.cs =====
  18.  
  19. using System;
  20. using OxyPlot;
  21. using System.Collections.ObjectModel;
  22. using System.Collections.Specialized;
  23. using System.ComponentModel;
  24. using System.Windows.Controls;
  25. using System.Collections.Generic;
  26.  
  27. namespace SilverlightApplication3
  28. {
  29.     public partial class MainPage : UserControl, INotifyPropertyChanged
  30.     {
  31.         public MainPage()
  32.         {
  33.             InitializeComponent();
  34.             FFTplotter.Model = new PlotModel();
  35.             SetPlotData();
  36.  
  37.             SelectedValues = new ObservableCollection<int>();
  38.  
  39.             FFTplotter.Model.MouseDown += MouseDown;
  40.             FFTplotter.Model.MouseUp += MouseUp;
  41.             FFTplotter.Model.MouseMove += MouseMoved;
  42.         }
  43.  
  44.         private void SetPlotData()
  45.         {
  46.             // Set the type of lineseries and the kind of visualization
  47.             var ls = new LineSeries();
  48.  
  49.             var ps = new List<DataPoint>();
  50.             Random random = new Random();
  51.             var min = 0;
  52.             var max = 100;
  53.  
  54.             for (int x = 0; x < 1000; x++)
  55.             {
  56.                 var dataPoint = 1;
  57.                 ps.Add(new DataPoint(x, random.Next(min, max)));
  58.             }
  59.  
  60.             ls.ItemsSource = ps;
  61.  
  62.             FFTplotter.Model.Series.Clear();
  63.             FFTplotter.Model.Series.Add(ls);
  64.             FFTplotter.Model.InvalidatePlot(true);
  65.         }
  66.  
  67.         private ObservableCollection<int> _selectedFrequencies;
  68.         public ObservableCollection<int> SelectedValues
  69.         {
  70.             get { return _selectedFrequencies; }
  71.             set
  72.             {
  73.                 if (value == null) return;
  74.                 _selectedFrequencies = value;
  75.                 NotifyPropertyChanged("SelectedFrequencies");
  76.             }
  77.         }
  78.  
  79.         private bool _mousedown;
  80.         private void MouseDown(object sender, OxyMouseEventArgs e)
  81.         {
  82.             _mousedown = true;
  83.             e.Handled = true;
  84.         }
  85.  
  86.         private void MouseUp(object sender, OxyMouseEventArgs e)
  87.         {
  88.             _mousedown = false;
  89.             RenderSelectedValue();
  90.             e.Handled = true;
  91.         }
  92.  
  93.         private void MouseMoved(object sender, OxyMouseEventArgs e)
  94.         {
  95.             if (_mousedown)
  96.             {
  97.                 e.Handled = true;
  98.             }
  99.         }
  100.  
  101.         private ObservableCollection<Annotation> _selectedFrequencylines;
  102.         private static bool RenderingLines;
  103.         private void RenderSelectedValue()
  104.         {
  105.             if (!RenderingLines)
  106.             {
  107.                 RenderingLines = true;
  108.  
  109.                 if (_selectedFrequencylines == null) _selectedFrequencylines = new ObservableCollection<Annotation>();
  110.  
  111.                 _selectedFrequencylines.Add(new LineAnnotation
  112.                 {
  113.                     Type = LineAnnotationType.Vertical,
  114.                     X = 500,
  115.                     Color = OxyColors.Gray,
  116.                     LineStyle = LineStyle.Solid,
  117.                 });
  118.  
  119.                 // And plot all the added lines!
  120.                 foreach (var a in _selectedFrequencylines)
  121.                 {
  122.                     // TODO: here silverlight crashes if you quicly click (with lmb) at two different locations in the plot.
  123.                     FFTplotter.Model.Annotations.Add(a);
  124.                 }
  125.                 FFTplotter.Model.InvalidatePlot(false);
  126.  
  127.                 RenderingLines = false;
  128.             }
  129.             else
  130.             {
  131.             }
  132.         }
  133.  
  134.         #region INotifyPropertyChanged Members
  135.  
  136.         public event PropertyChangedEventHandler PropertyChanged;
  137.  
  138.         /// <summary>
  139.         ///   Generic NotifyPropertyChanged function
  140.         /// </summary>
  141.         /// <param name="propertyname"> Name of the property that has changed </param>
  142.         private void NotifyPropertyChanged(string propertyname)
  143.         {
  144.             if (PropertyChanged != null)
  145.             {
  146.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
  147.             }
  148.         }
  149.         #endregion
  150.     }
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top