How do I set a brush color to the color of the chart background?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Set Color of Brush to Match Chart Background
Collapse
X
-
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.
-
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.
Comment
-
Hi Jesse
Previously, when I had
I got an exception. Now it's working fine.Code:(State == State.Configure) { backgroundBrush = ChartControl.Properties.ChartBackground; }
Is it OK to leave it here or is it better suited elsewhere?
Thanks for the bb code.
Comment
-
Hello,
What you have would in general be OK,
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) { backgroundBrush = ChartControl.Properties.ChartBackground; }
I look forward to being of further assistance.Code:(State == State.Configure) { if(ChartControl != null) { backgroundBrush = ChartControl.Properties.ChartBackground; } }
Comment
-
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..Originally posted by NinjaTrader_Jesse View PostBut, 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; } }
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?
Comment
-
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.Originally posted by -=Edge=- View PostI'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..
regarding getting the background from an addon, here is a snippet of the OnWindowCreated to do that:
I look forward to being of further assistance.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; } })); }
Comment
-
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 PostGood 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.
doh!Originally posted by NinjaTrader_Jesse View Postregarding getting the background from an addon, here is a snippet of the OnWindowCreated to do that:
.. 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?
Comment
-
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 PostCouple 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 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.Originally posted by -=Edge=- View PostAlso 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?
Thank you for bringing these items up.
I look forward to being of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
662 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
376 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
110 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
575 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
580 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|


Comment