Programmatic Drawlines

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
rossmc
Newbie
Newbie
Posts: 57
Joined: Wed Apr 08, 2009 12:00 am

Programmatic Drawlines

Post by rossmc » Tue Jan 26, 2010 7:30 am

Hi

I have my own set of classes that persist my chart so I can recreate it later. For the drawline tool, I save the properties of each line in the .Lines collection (StartPos, EndPos, and Pen information).

When I reload a chart, I add a drawline tool and then manually add drawlineitems. The lines are drawn back on the chart but if I mouse over I get an error; 'Object reference not set to an instance of an object'?

How do I programatically add lines to a drawline tool?

ANSWER: When creating the drawlineitem I need to pass the drawline to it's constructor; ie. line = New Steema.TeeChart.Tools.DrawLineItem(dl) where dl is the drawline tool.

Something else though. Is it possible to add/access the event handlers of a drawlineitem. I want to know when the user clicks on a drawlineitem?

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Programmatic Drawlines

Post by Sandra » Tue Jan 26, 2010 10:33 am

HHello rossmc,

I made a simple project that I think you can use for your application. Please see next code works correctly in your application:

Code: Select all

 public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }
        Steema.TeeChart.Styles.Line line1;
        Steema.TeeChart.Tools.DrawLine drawLine1;
        Steema.TeeChart.Tools.DrawLineItem I;
        private void InitializeChart()
        {
            line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
            line1.FillSampleValues();
            drawLine1 = new Steema.TeeChart.Tools.DrawLine(tChart1.Chart);
            drawLine1.Pen.Color = Color.Blue;
            drawLine1.Pen.Width = 2;
            I=new Steema.TeeChart.Tools.DrawLineItem(drawLine1);
            double tmp = line1.YValues.Range / 5.0;
            I.StartPos = new Steema.TeeChart.Drawing.PointDouble(5, line1.YValues.Maximum - tmp);
            I.EndPos = new Steema.TeeChart.Drawing.PointDouble(15, line1.YValues.Minimum + tmp);
            drawLine1.EnableDraw = false;
            drawLine1.DragLine += new Steema.TeeChart.Tools.DrawLineEventHandler(drawLine1_DragLine);
        }

        void drawLine1_DragLine(Steema.TeeChart.Tools.DrawLine sender)
        {
            I=drawLine1.Selected;
            tChart1.Header.Text = "Start: (x: "+ I.StartPos.X.ToString("0.00")+ ", y: " + I.StartPos.Y.ToString("0.00")+")";
            tChart1.SubHeader.Visible = true;
            tChart1.SubHeader.Text = "End: (x: " + I.EndPos.X.ToString("0.00") + ", y: " + I.EndPos.Y.ToString("0.00") + ")";
        }
If your problem still appears, please you could send us a simple project because we can reproduce the problem here and we try to solve it.

I hope will help.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

rossmc
Newbie
Newbie
Posts: 57
Joined: Wed Apr 08, 2009 12:00 am

Re: Programmatic Drawlines

Post by rossmc » Tue Jan 26, 2010 12:54 pm

Thanks for that code, it's very helpful. Is there a way I can trap a right mouse button click on a drawlineitem now? No events fire (that I can see) when I right click a drawlineitem?

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Programmatic Drawlines

Post by Sandra » Wed Jan 27, 2010 10:39 am

Hello rossmc,

You have to options:

First: You could change button with the drawline dragging to right use next code.

Code: Select all

drawLine1.Button = System.Windows.Forms.MouseButtons.Right; 
Second: If you want drawline tool dragging both left and right, I recommend use MouseDown event or MouseClick event as next code. Please check next code works correctly in your application.

Code: Select all

       
 void tChart1_MouseDown(object sender, MouseEventArgs e)
{
      if (e.Button == MouseButtons.Right)
     {
           drawLine1.Button = System.Windows.Forms.MouseButtons.Right; 
           Steema.TeeChart.Tools.DrawLineItem I1 = drawLine1.Selected;
           tChart1.Header.Text = "Start: (x: " + I.StartPos.X.ToString("0.00") + ", y: " + I.StartPos.Y.ToString("0.00") + ")";
           tChart1.SubHeader.Visible = true;
           tChart1.SubHeader.Text = "End: (x: " + I.EndPos.X.ToString("0.00") + ", y: " + I.EndPos.Y.ToString("0.00") + ")";
       }
       else { drawLine1.Button = System.Windows.Forms.MouseButtons.Left; }
}
I hope will helps.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

rossmc
Newbie
Newbie
Posts: 57
Joined: Wed Apr 08, 2009 12:00 am

Re: Programmatic Drawlines

Post by rossmc » Thu Jan 28, 2010 12:04 pm

I suppose I should have been more specific. What I really want is:

Left click on drawline selects the line (as per normal operation)
Right-click on drawline shows a drawline specific context-sensitive menu.

So really what I am looking for is a way to trap a right-click on a drawline.

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Programmatic Drawlines

Post by Sandra » Fri Jan 29, 2010 9:16 am

Hello rossmc,

If you want to click the line using button right, you should only be use MouseDown Event, or MouseDown and MouseClick event. I recommend use a similar code as next:

Code: Select all

  public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }
        Steema.TeeChart.Styles.Line line1;
        Steema.TeeChart.Tools.DrawLine drawLine1;
        Steema.TeeChart.Tools.DrawLineItem I;
        private void InitializeChart()
        {
            line1 = new Steema.TeeChart.Styles.Line(tChart1.Chart);
            line1.FillSampleValues();
            drawLine1 = new Steema.TeeChart.Tools.DrawLine(tChart1.Chart);
            drawLine1.Pen.Color = Color.Blue;
            drawLine1.Pen.Width = 2;
            I=new Steema.TeeChart.Tools.DrawLineItem(drawLine1);
            double tmp = line1.YValues.Range / 5.0;
            I.StartPos = new Steema.TeeChart.Drawing.PointDouble(5, line1.YValues.Maximum - tmp);
            I.EndPos = new Steema.TeeChart.Drawing.PointDouble(15, line1.YValues.Minimum + tmp);
            drawLine1.EnableDraw = false;
            drawLine1.DragLine += new Steema.TeeChart.Tools.DrawLineEventHandler(drawLine1_DragLine);
            tChart1.AllowScroll += new Steema.TeeChart.TChart.AllowScrollEventHandler(tChart1_AllowScroll);
            tChart1.MouseDown += new MouseEventHandler(tChart1_MouseDown);
        }

        void tChart1_AllowScroll(Steema.TeeChart.Axis a, Steema.TeeChart.TChart.AllowScrollEventArgs e)
        {
            e.AllowScroll = false;
        }

        void tChart1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
               Text = "End: (x: " + I.EndPos.X.ToString("0.00") + ", y: " + I.EndPos.Y.ToString("0.00") + ")";

            }
        
        }
        void drawLine1_DragLine(Steema.TeeChart.Tools.DrawLine sender)
        {
                I = drawLine1.Selected;
                tChart1.Header.Text = "Start: (x: " + I.StartPos.X.ToString("0.00") + ", y: " + I.StartPos.Y.ToString("0.00") + ")";
                tChart1.SubHeader.Visible = true;
                tChart1.SubHeader.Text = "End: (x: " + I.EndPos.X.ToString("0.00") + ", y: " + I.EndPos.Y.ToString("0.00") + ")";
        }
    }
I hope will helps.

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply