12 Replies - 630 Views - Last Post: 26 October 2010 - 09:25 AM Rate Topic: -----

#1 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Problem with Graph Tool

Posted 26 October 2010 - 06:01 AM

All,

I am playing with a graph users control, and am having some trouble (at the start no less). My code is below, and when I run my form with the graph control, the XAxis is not drawn out.

Any suggestions?

Thanks,

Ed.

My Control Class:
public enum GraphType { TwoAxis, ThreeAxis }

        #region Properties for graph.
        [Description("Number of axes for the graph.  Must be 2 or 3"),
         Category("GraphSettings"),
         Browsable(true)]
        public GraphType NumberOfAxes { get; set; }

        [Description("The number of divisions in the X-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public int XAxisDivisions { get; set; }

        [Description("The data type to be used on the X-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public CscGraph.ValidAxisType XAxisType { get; set; }

        [Description("The number of divisions in the Y-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public int YAxisDivisions { get; set; }

        [Description("The data type to be used on the Y-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public CscGraph.ValidAxisType YAxisType { get; set; }

        [Description("The number of divisions in the Z-Axis. Only used with Three Axis Graphs."),
         Category("GraphSettings"),
         DefaultValue(10),
         Browsable(true)]
        public int ZAxisDivisions { get; set; }

        [Description("The data type to be used on the Z-Axis. Only used with Three Axis Graphs."),
         Category("GraphSettings"),
         Browsable(true)]
        public CscGraph.ValidAxisType ZAxisType { get; set; }

        #endregion

        public CscGraphPane()
        {
            InitializeComponent();
            
        }

        public void AddCurve(CscGraph.CscPointList pointList) {
            
        }

        private void DrawXAxis()
        {
            CscGraph.Axis X = new CscGraph.Axis();
            X.MaxValue = 100;
            X.MinValue = 0;
            X.NumberOfSections = 10;

            Graphics xAxisBase = this.CreateGraphics();
            xAxisBase.DrawLine(new Pen(Color.Black), new Point(10, 10), new Point(this.Width - 10, 10));

            this.Refresh();
        }

        public class SetAxes
        {
            public CscGraph.Axis X { get; set; }
            public CscGraph.Axis Y { get; set; }
            public CscGraph.Axis Z { get; set; }

            public SetAxes() {
                X = new CscGraph.Axis();
                Y = new CscGraph.Axis();
            }

            public SetAxes(int numberOfAxes) {
                if (numberOfAxes == 3) { 
                    X = new CscGraph.Axis();
                    Y = new CscGraph.Axis();
                    Z = new CscGraph.Axis();
                }
                else {
                        X = new CscGraph.Axis();
                        Y = new CscGraph.Axis();
                }


            }
        }

        private void CscGraphPane_Load(object sender, EventArgs e)
        {
            DrawXAxis();
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with Graph Tool

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 06:22 AM

Comment out line 62
I bet you're drawing your x axis, then you are refreshing the control - which causes it to execute its On_Paint() method and re-paint itself blank.
Was This Post Helpful? 0
  • +
  • -

#3 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Problem with Graph Tool

Posted 26 October 2010 - 07:17 AM

tlhIn'toq,

I tried commenting out that line, and it still doesn't work. Is there anything I need to do on the form implementing the user control to have it update? Right now, I have added the control, and set the properties as appropriate.

Also, Do I need to add a pane or anything to the User Control where I am building the graph?
Thank,

Ed.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 07:34 AM

The only time you call DrawXaxis() is when the UserControl loads.
After that, I don't see any time it is used/called.

private void DrawXAxis()
        {
            CscGraph.Axis X = new CscGraph.Axis()
            X.MaxValue = 100;
            X.MinValue = 0;
            X.NumberOfSections = 10;
            Graphics xAxisBase = this.CreateGraphics();
            
            xAxisBase.DrawLine(new Pen(Color.Black), new Point(10, 10), new Point(this.Width - 10, 10));
            //this.Refresh();
        }



Quote

Also, Do I need to add a pane or anything to the User Control where I am building the graph?
No. The UC is your drawing surface. That's fine. Though I do have a tendancy to place panels on most of my controls with the autoscroll set to true. That's just in case the control at some point becomes large (WindowMaximized)... then later gets resized smaller so the user needs to be able to scroll around. For now, just work it as is until it is working right. Make it more complicated after it is in a known-working condition.

Everything other than line 9 goes to waste at this point: It's never used and doesn't affect the 10 pixel line being drawn. I assume you realize this and that right now you just want to see the 10px line get drawn to prove the method works.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 07:47 AM

I just realized you create a pen, but never give it a width.
private void DrawXAxis()
        {
            //CscGraph.Axis X = new CscGraph.Axis()
            //X.MaxValue = 100;
            //X.MinValue = 0;
            //X.NumberOfSections = 10;

            Graphics xAxisBase = this.CreateGraphics();     
            Pen myPen = new Pen(Color.Black);
            myPen.Width = 5.0f; // Set the pen to a width of 5 pixels.
            xAxisBase.DrawLine(myPen), new Point(10, 10), new Point(this.Width - 10, 10));
            //this.Refresh();
        }



This post has been edited by tlhIn'toq: 26 October 2010 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 08:03 AM

After a little bit of experimenting I realized you can't do this just in the on_load. There is no surface to draw on yet.
If you call the drawing function after the control exists, then you can draw on it.
That's why most drawing functions take place (or are called from the on_paint method)

In the attached the drawing doesn't take in the onload. More likely it does take place, then the control's on_paint finishes and paints over your drawing.

Attached Image

When the [Draw] button is clicked the drawings return.
When the control is covered and revealed again the on_paint function is automatically called to redraw the areas that were obscured. So the control goes back to looking blank.

The morale of the story is... you should call your custom drawing functions from On_Paint()

public override void OnPaint()
{
    //do your drawing here
}

Was This Post Helpful? 2
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 08:17 AM

I smell a tutorial, why don't you write one. Probably a common question.
Was This Post Helpful? 0
  • +
  • -

#8 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Problem with Graph Tool

Posted 26 October 2010 - 08:20 AM

Thanks, placing the call to DrawXAxis in the override to OnPaint works! Thanks for all the help tlhIn'toq.

Now to add the pane (or will that be pain!)

Ed.

My new Code:
    public partial class CscGraphPane : UserControl
    {
        public enum GraphType { TwoAxis, ThreeAxis }

        #region Properties for graph.
        [Description("Number of axes for the graph.  Must be 2 or 3"),
         Category("GraphSettings"),
         Browsable(true)]
        public GraphType NumberOfAxes { get; set; }

        [Description("The number of divisions in the X-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public int XAxisDivisions { get; set; }

        [Description("The data type to be used on the X-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public CscGraph.ValidAxisType XAxisType { get; set; }

        [Description("The number of divisions in the Y-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public int YAxisDivisions { get; set; }

        [Description("The data type to be used on the Y-Axis."),
         Category("GraphSettings"),
         Browsable(true)]
        public CscGraph.ValidAxisType YAxisType { get; set; }

        [Description("The number of divisions in the Z-Axis. Only used with Three Axis Graphs."),
         Category("GraphSettings"),
         DefaultValue(10),
         Browsable(true)]
        public int ZAxisDivisions { get; set; }

        [Description("The data type to be used on the Z-Axis. Only used with Three Axis Graphs."),
         Category("GraphSettings"),
         Browsable(true)]
        public CscGraph.ValidAxisType ZAxisType { get; set; }

        #endregion

        public CscGraphPane()
        {
            InitializeComponent();
            
        }

        public void AddCurve(CscGraph.CscPointList pointList) {
            
        }

        private void DrawXAxis()
        {
            CscGraph.Axis X = new CscGraph.Axis();
            X.MaxValue = 100;
            X.MinValue = 0;
            X.NumberOfSections = 10;
            Pen myPen = new Pen(Color.Black);
            myPen.Width = 5.0f;
            Graphics xAxisBase = this.CreateGraphics();
            xAxisBase.DrawLine(myPen, new Point(10, 10), new Point(this.Width - 10, 10));

            // this.Refresh();
        }

        public class SetAxes
        {
            public CscGraph.Axis X { get; set; }
            public CscGraph.Axis Y { get; set; }
            public CscGraph.Axis Z { get; set; }

            public SetAxes() {
                X = new CscGraph.Axis();
                Y = new CscGraph.Axis();
            }

            public SetAxes(int numberOfAxes) {
                if (numberOfAxes == 3) { 
                    X = new CscGraph.Axis();
                    Y = new CscGraph.Axis();
                    Z = new CscGraph.Axis();
                }
                else {
                        X = new CscGraph.Axis();
                        Y = new CscGraph.Axis();
                }


            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawXAxis();
        }

        
    }

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 08:40 AM

View PostSergio Tapia, on 26 October 2010 - 07:17 AM, said:

I smell a tutorial, why don't you write one. Probably a common question.


I'll put it on my list. Right now I'm up against a totally unreasonable mid-November deadline. (2 months to do 4 months worth of work)

But I'm taking December off from work (combination of vacation and comp-time).
It will be nice to relax and be able to concentrate on personal stuff. Like my photography, personal coding projects and tutorials.

I've actually wanted to do a nice tutorial on custom user controls, like an analog gauge or dial. That would of course involve custom drawing, limiting the events and properties shown in the designer to just the pertinent ones, etc.

View Posterburrell, on 26 October 2010 - 07:20 AM, said:

Thanks, placing the call to DrawXAxis in the override to OnPaint works! Thanks for all the help tlhIn'toq.

Now to add the pane (or will that be pain!)

Ed.


I meant to compliment you on your use of regions and property attributes. Such a small percentage of even experienced coders use them and I think they are just great. If you are going to do 90% of it, why not through in the other 10% and make it really nice and complete like that? Right?
Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 08:57 AM

Eh! Pump the brakes! I used to like using regions in my code when I was in college, but I don't really like using them anymore. They make the code less readable! :P If I find my class needing regions it's a good sign of code smell and I should be taking some lines OUT! :D
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 09:17 AM

View PostSergio Tapia, on 26 October 2010 - 07:57 AM, said:

Eh! Pump the brakes! I used to like using regions in my code when I was in college, but I don't really like using them anymore. They make the code less readable! :P If I find my class needing regions it's a good sign of code smell and I should be taking some lines OUT! :D


I guess that's where it comes into a case of style and such. I think they make it more readable. Certainly more organized. They tell your co-workers where they need to add/change something.
I love being able to type ctrl+M, ctrl+O and have the entire code page roll up into collapsed regions.

Attached Image

I find it very clean and fast to roll everything up, then open only the method I need.

It's nice to say that code needs to be taken out... but that just isn't always the case. Sometimes its just about keeping it organized. In my photo processing steps I like grouping the steps

#region Rotate
#region Resize
#region Add Caption
#region Black & white
#region Add watermark
#region Add to audit trail

Again, when you roll up all the code with ctrl+M, ctrl+O (for the whole page) or ctrl+M, ctrl+M (for the selected portion) you can get to the part you want quickly
  • Expand methods
  • Expand ProcessImage()
  • Expand #region Add Caption

And they are handy for using Find. I can quickly Find "Add watermark" and be taken right to the code block for it.
Was This Post Helpful? 0
  • +
  • -

#12 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Problem with Graph Tool

Posted 26 October 2010 - 09:23 AM

One more question...How do I clear the panel before it repaints?

Thanks,

Ed.

Also, I have to agree with tlhIn'toq when it comes to regions and such. I use them to make my code more readable. For eample, in the graphing tool, I use the region to section off the properties for the user control. This just makes it easier for me to find things when I need to go back to them.

Ed.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Problem with Graph Tool

Posted 26 October 2010 - 09:25 AM

View Posterburrell, on 26 October 2010 - 08:21 AM, said:

One more question...How do I clear the panel before it repaints?

Thanks,

Ed.


Think of your drawing surface as a painting canvas. You can't take paint off of a canvas once its on, right?
So you have to paint over it and start again.

Paint the entire area with Color.FromKnownColor(KnownColor.Control);
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1