Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set Color of Brush to Match Chart Background

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Set Color of Brush to Match Chart Background

    Just a quick one that has me stumped...

    How do I set a brush color to the color of the chart background?

    #2
    Hello,

    You can retrieve the background of the chart using ChartControl.Properties:

    ChartControl.Properties.ChartBackground

    You can locate the other properties in the help guide here: http://ninjatrader.com/support/helpG...b=chartcontrol

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse, but I already have that.

      How do I then set the color of a brush to that color property?

      Comment


        #4
        Hello,

        The ChartBackground is a brush already, you would just need to assign it to whatever variable you have currently.

        Code:
        Brush myBrushVar;
        
        myBrushVar = ChartControl.Properties.ChartBackground;
        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Of course it is, I knew that

          Think I was expecting a more complex solution.

          Thanks.

          Comment


            #6
            Where is the best place to put these two lines of code.

            The only place that

            myBrushVar = ChartControl.Properties.ChartBackground;

            seems to work is within OnRender.

            As an aside, how do I get the code snippet to show in a grey box in forum posts?

            Comment


              #7
              Hello,

              Thank you for the questions.

              Are you getting an exception or something while you try the code somewhere else?

              ChartControl in general can be null or not, while the sample syntax is correct you still may need to check if the object is null or not before using it.

              If you are getting exceptions in the output or log, you may need to utilize :

              if(ChartControl != null)
              {
              myBrushVar = ChartControl.Properties.ChartBackground;
              }

              If this is not the case, could you tell me what errors or other problems you are running into regarding using the syntax anywhere other than OnRender?

              Regarding the forum question, if you click the Go Advanced button while replying, you will get the full editor with Syntax button. The bb code for the syntax block is : [ CODE ] some code here [/ CODE], without the spaces.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse

                Previously, when I had
                Code:
                (State == State.Configure)
                {
                backgroundBrush = ChartControl.Properties.ChartBackground;
                }
                I got an exception. Now it's working fine.
                Is it OK to leave it here or is it better suited elsewhere?

                Thanks for the bb code.

                Comment


                  #9
                  Hello,

                  What you have would in general be OK,

                  Code:
                  (State == State.Configure)
                  {
                      backgroundBrush = ChartControl.Properties.ChartBackground;
                  }
                  But, ChartControl can be null which is an exception if you try to access Properties on the null object, I would suggest any time using ChartControl to wrap it in a null check just in case.

                  Code:
                  (State == State.Configure)
                  {
                          if(ChartControl != null)
                          {
                                  backgroundBrush = ChartControl.Properties.ChartBackground;
                          }
                  }
                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Great stuff, thanks.

                    Comment


                      #11
                      Originally posted by NinjaTrader_Jesse View Post
                      But, ChartControl can be null which is an exception if you try to access Properties on the null object, I would suggest any time using ChartControl to wrap it in a null check just in case.

                      Code:
                      (State == State.Configure)
                      {
                              if(ChartControl != null)
                              {
                                      backgroundBrush = ChartControl.Properties.ChartBackground;
                              }
                      }
                      I agree with the null check (anywhere), although as you mentioned, I've run across chartcontrol being null in state.config more than once, so setting your brush there might not be the best solution.. If for some reason chartcontrol is null there, your brush never gets set.. I would probably tend to throw that in state.historical, or dataloaded, as I've yet to experience chartcontrol being null in either of those 2 states..

                      Question for you Jesse..

                      How might I go about grabbing the background of the chart from an AddOn in the OnWindowCreated?
                      Guess more to the point, how might I find and declare the ChartControl of the window/chart?


                      -=Edge=-
                      NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

                      Comment


                        #12
                        Originally posted by -=Edge=- View Post
                        I've run across chartcontrol being null in state.config more than once, so setting your brush there might not be the best solution.. If for some reason chartcontrol is null there, your brush never gets set.. I would probably tend to throw that in state.historical, or dataloaded, as I've yet to experience chartcontrol being null in either of those 2 states..
                        Good point made, this would be suggested rather than Configure due to ChartControl being null at certain points, generally DataLoaded or Historical are safe for items like this.

                        regarding getting the background from an addon, here is a snippet of the OnWindowCreated to do that:

                        Code:
                        protected override void OnWindowCreated(Window window)
                        {
                        	Chart chart = window as Chart;
                        	window.Dispatcher.InvokeAsync((() =>
                        	{
                                    if (chart != null)
                                    {
                                        ChartControl myCC = chart.ActiveChartControl;
                                        myCC.Properties.ChartBackground = Brushes.Red;
                                    }
                            }));
                        }
                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Jesse View Post
                          Good point made, this would be suggested rather than Configure due to ChartControl being null at certain points, generally DataLoaded or Historical are safe for items like this.
                          Although I use that null check in most all states, I actually refrain from using it in the State.Config for just that reason..

                          Originally posted by NinjaTrader_Jesse View Post
                          regarding getting the background from an addon, here is a snippet of the OnWindowCreated to do that:
                          doh! .. I'm not sure how I missed that, but Thank You!


                          Couple more questions with regards to your sample.. I haven't been using a dispatcher in create, destroy, restore, or save.. I haven't experienced any problems not doing so, but guess the question is.. Should I be using that? ..

                          Also I've notice in most of the samples I've seen, none of them are calling the base method after any custom coding... base.OnWindowSaved(window, element); Isn't or Is this recommended in all of those states as well?


                          -=Edge=-
                          NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

                          Comment


                            #14
                            Originally posted by -=Edge=- View Post
                            Couple more questions with regards to your sample.. I haven't been using a dispatcher in create, destroy, restore, or save.. I haven't experienced any problems not doing so, but guess the question is.. Should I be using that? ..
                            This really kind of depends if you get a threading exception or not, in this case I added it for good measure, but if there is not any exception being generated in any situation you test in, I would say it is likely safe to remove it. During the beta this has been a confusing topic due to the nature of the program, some early samples required a lot of dispatching while later samples may no longer require dispatching where they did previously. I believe this would be a good point to mention to Product management for more specifics on locations this is required.


                            Originally posted by -=Edge=- View Post
                            Also I've notice in most of the samples I've seen, none of them are calling the base method after any custom coding... base.OnWindowSaved(window, element); Isn't or Is this recommended in all of those states as well?
                            This also depends on if there is base logic happening, OnRender from an indicator is a good example, without calling the base you have no plots rendered. By not calling the base in the addon, this seems to have no repercussion from what I can see. I do see a good learning point in this question and will consult PM on this topic to see if there can be a list of what absolutely needs base to be called and what does not.

                            Thank you for bringing these items up.


                            I look forward to being of further assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Thanks for that Edge.

                              Have moved it to State.DataLoaded

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              40 views
                              0 likes
                              Last Post alifarahani  
                              Started by Waxavi, Today, 02:10 AM
                              1 response
                              18 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X