Select different series

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Paola
Newbie
Newbie
Posts: 11
Joined: Thu Mar 19, 2015 12:00 am

Select different series

Post by Paola » Thu May 21, 2015 3:52 pm



Hi
In my chart I have about 100 series with only One point. Each serie represents a place with different attributes(e.g. area, anual produced commodities, curve of population growth..). The user wants to select multiple series with the mouse. Selected series will then be retrieved and some additional information will be calculated, and shown in a report.

I've seen the example in http://www.teechart.net/support/viewtopic.php?t=5385.
but I'm working with different series, I need to show markers in my selected points and know Exactly the selected series in order to calculate my averages and curves.

Could you help me? any advice would be really appreciated.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Select different series

Post by Christopher » Fri May 22, 2015 2:33 pm

Hello!

I've written a little class which derives from TeeChart's Tool class:

Code: Select all

  public class SelectionEventArgs : EventArgs
  {
    public SelectionEventArgs(List<Series> series)
    {
      SelectedSeries = new List<Series>();
      SelectedSeries.AddRange(series);

    }
    public List<Series> SelectedSeries { get; set; }
  }

  public class SelectMultiSeries : Tool
  {
    private Keys pressedKey;
    public event EventHandler<SelectionEventArgs> PresentSelection;
    public event EventHandler<SelectionEventArgs> FinishedSelection;

    public SelectMultiSeries(Chart chart) : base(chart)
    {
      StartKey = Keys.F1;
      EndKey = Keys.F2;
      SelectedSeries = new List<Series>();
      HandleSize = 2;
    }

    public SelectMultiSeries() : this(null)
    {

    }

    public int HandleSize { get; set; }
    public Keys StartKey { get; set; }
    public Keys EndKey { get; set; }

    protected virtual void OnPresentSelection(SelectionEventArgs e)
    {
      if (PresentSelection != null)
      {
        PresentSelection(this, e);
      }
    }

    protected virtual void OnFinishedSelection(SelectionEventArgs e)
    {
      if (FinishedSelection != null)
      {
        FinishedSelection(this, e);
      }
    }

    public List<Series> SelectedSeries { get; set; }

    protected override void KeyEvent(KeyEventArgs e)
    {
      pressedKey = e.KeyCode;
      if (pressedKey == EndKey)
      {
        OnFinishedSelection(new SelectionEventArgs(this.SelectedSeries));
        SelectedSeries.Clear();
        Invalidate();
      }
    }

    private void DrawSeriesHandles(Series tmpSeries)
    {
      int t, X, Y;
      int tmpStep, tmpLast, tmpCount;

      Graphics3D g = Chart.Graphics3D;

      tmpCount = tmpSeries.LastVisibleIndex - tmpSeries.FirstVisibleIndex;
      if (tmpCount == 0) tmpCount = tmpSeries.Count;
      if (tmpCount > 20) tmpStep = tmpCount / 20;
      else tmpStep = 1;
      t = tmpSeries.FirstVisibleIndex;
      if (t == -1) t = 0;
      tmpLast = tmpSeries.LastVisibleIndex;
      if (tmpLast == -1) tmpLast = tmpSeries.Count - 1;
      while (t <= tmpLast)
      {
        tmpSeries.CalcSelectionPos(t, out X, out Y);
        g.Rectangle(RectFromPoint(X, Y), tmpSeries.MiddleZ);
        t += tmpStep;
      }
    }

    private Rectangle RectFromPoint(int X, int Y)
    {
      return Utils.FromLTRB(X - HandleSize, Y - HandleSize, X + HandleSize, Y + HandleSize);
    }

    protected override void ChartEvent(EventArgs e)
    {
      if (e is AfterDrawEventArgs)
      {
        foreach (Series s in SelectedSeries)
        {
          DrawSeriesHandles(s);
        }
      }
      else if (e is BeforeDrawEventArgs)
      {
        Chart.Parent.GetControl().Focus();
      }
    }

    protected override void MouseEvent(MouseEventKinds kind, MouseEventArgs e, ref Cursor c)
    {
      switch (kind)
      {
        case MouseEventKinds.Down:
          if (pressedKey == StartKey)
          {
            for (int i = 0; i < Chart.Series.Count; i++)
            {
              if (Chart.Series[i].Clicked(e.Location) != -1)
              {
                SelectedSeries.Add(Chart.Series[i]);
              }
            }
            OnPresentSelection(new SelectionEventArgs(this.SelectedSeries));
            Invalidate();
          }
          break;
      }
    }
  }
}
The idea here is that you press F1 to start the selection then F2 to finish it. To run this little tool, you can use:

Code: Select all

    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;

      Random rnd = new Random();

      for (int i = 0; i < 10; i++)
      {
        tChart1.Series.Add(typeof(Points));
        tChart1.Series[i].Add(rnd.NextDouble(), rnd.NextDouble());
      }

      SelectMultiSeries tool = new SelectMultiSeries(tChart1.Chart);
      tool.PresentSelection += Tool_PresentSelection;
      tool.FinishedSelection += Tool_FinishedSelection;
    }

    private void Tool_FinishedSelection(object sender, SelectionEventArgs e)
    {
      string names = "";
      foreach (Series s in e.SelectedSeries)
      {
        names += s.ToString() + " ";
      }
      tChart1.Header.Text = "TeeChart";
      MessageBox.Show(names);
    }

    private void Tool_PresentSelection(object sender, SelectionEventArgs e)
    {
      string names = "";
      foreach (Series s in e.SelectedSeries)
      {
        names += s.ToString() + " ";
      }
      tChart1.Header.Text = names;
    }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Paola
Newbie
Newbie
Posts: 11
Joined: Thu Mar 19, 2015 12:00 am

Re: Select different series

Post by Paola » Fri May 22, 2015 2:43 pm

thank you!

Post Reply