Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Draw.TextFixed() Multiple Instances
Collapse
X
-
Thanks Chelsea, much appreciated.Originally posted by NinjaTrader_ChelseaB View Post
Do you know if there is sample code on how to translate the "top right of the chart" as used by the Draw.TextFixed() into pixel coordinates (x,y) so I can use them in the OnRender() method for the text placement?
Andrew
Comment
-
Hello Chelsea,Originally posted by NinjaTrader_ChelseaB View PostHello Andrew,
The top right would be chartPanel.Width minus the width you want for the text as the X and 0 (or whatever margin you want at the top) as the Y.
https://ninjatrader.com/support/help...chartpanel.htm
Wanted to circle back and give you a shout out for your assistance with my enquiry regarding the textboxes using the OnRender method!
After some hard work and trial & error finally got the desired out come I was seeking.
The pic below is of four different screen shots (meshed together) showing the indicator updating the two textboxes in the top right of the chart and filling the rectangles depending on the state of the charts in the 5 & 15-min timeframes. (The indicator is used on either a 1, 2 or 3 minute chart.)
Have also shared the code segment from the OnRender method that I implemented that draws the two textboxes.
Kind Regards
Andrew
Code:protected override void OnRender (ChartControl chartControl, ChartScale chartScale) { base.OnRender(chartControl, chartScale); NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier", 14) { Size = 18, Bold = true }; //Create two the textbox format objects, one for each of the two text boxes that will be drawn // SharpDX.DirectWrite.TextFormat textBoxFormat5Min = myFont.ToDirectWriteTextFormat(); SharpDX.DirectWrite.TextFormat textBoxFormat15Min = myFont.ToDirectWriteTextFormat(); //Create three textbox layouts 2 layouts will be created purely so I can determine the length of the two potential strings that will be displayed in the textboxes. // Once the longest string length is determined, it will be used in the coordinates to establish the dimmensions of the textbox & therefore when rendering the text box will not change size/shape as the strings alternate SharpDX.DirectWrite.TextLayout textBoxLayOut5Min = new SharpDX.DirectWrite.TextLayout (NinjaTrader.Core.Globals.DirectWriteFactory, Text5Min, textBoxFormat5Min, float.MaxValue, textBoxFormat5Min.FontSize); SharpDX.DirectWrite.TextLayout textBoxLayOut5MinGreen = new SharpDX.DirectWrite.TextLayout (NinjaTrader.Core.Globals.DirectWriteFactory, Text5MinAlignGreen, textBoxFormat5Min, float.MaxValue, textBoxFormat5Min.FontSize); SharpDX.DirectWrite.TextLayout textBoxLayOut5MinRed = new SharpDX.DirectWrite.TextLayout (NinjaTrader.Core.Globals.DirectWriteFactory, Text5MinAlignRed, textBoxFormat5Min, float.MaxValue, textBoxFormat5Min.FontSize); //Same as the above objects however this is for the second textbox will be rendered underneath the 1st text box. SharpDX.DirectWrite.TextLayout textBoxLayOut15Min = new SharpDX.DirectWrite.TextLayout (NinjaTrader.Core.Globals.DirectWriteFactory, Text15Min, textBoxFormat15Min, float.MaxValue, textBoxFormat15Min.FontSize ); SharpDX.DirectWrite.TextLayout textBoxLayOut15MinGreen = new SharpDX.DirectWrite.TextLayout (NinjaTrader.Core.Globals.DirectWriteFactory, Text15MinAlignGreen, textBoxFormat15Min, float.MaxValue, textBoxFormat15Min.FontSize ); SharpDX.DirectWrite.TextLayout textBoxLayOut15MinRed = new SharpDX.DirectWrite.TextLayout (NinjaTrader.Core.Globals.DirectWriteFactory, Text15MinAlignRed, textBoxFormat15Min, float.MaxValue, textBoxFormat15Min.FontSize ); //Using the two extra textlayout objects for each of the two textboxes //Identif for each textbox which will be the longest to render & use that value float MaxStringLength1 = textBoxLayOut5MinGreen.Metrics.Width > textBoxLayOut15MinGreen.Metrics.Width ? textBoxLayOut5MinGreen.Metrics.Width : textBoxLayOut15MinGreen.Metrics.Width; float MaxStringLength2 = textBoxLayOut5MinRed.Metrics.Width > textBoxLayOut15MinRed.Metrics.Width ? textBoxLayOut5MinRed.Metrics.Width : textBoxLayOut15MinRed.Metrics.Width; MaxTextBoxStringSize = MaxStringLength1 > MaxStringLength2 ? MaxStringLength1 : MaxStringLength2; //Essentially to draw a text box that has text inside it and will be filled with a color and have a a border //It will functionally be a smaller rectangle inside a large rectangle. //The smaller (inner) rectangle will be for the text, the larger (outter) rectangle will be filled and have a border //Coordinates for the 5-Minute timeframe Text to appear inside the colored rectangle textBox5Min_XCoord = textBoxBorder5Min_XCoord + 5; textBox5Min_YCoord = textBoxBorder5Min_YCoord + 10; textBox5Min_Width = textBoxLayOut5Min.Metrics.Width; textBox5Min_Height = textBoxLayOut5Min.Metrics.Height; //Coordinates for the 15-Minute timeframe Textbox to appear inside the colored rectangle //The coordinates are offset from the 5-Minute timeframe text box which will be ABOVE this box on the chart in the top right hand corner textBox15Min_XCoord = textBoxBorder5Min_XCoord + 5; textBox15Min_YCoord = textBoxBorder15Min_YCoord + 10; textBox15Min_Width = textBoxLayOut15Min.Metrics.Width; textBox15Min_Height = textBoxLayOut15Min.Metrics.Height; //Coordiantes for the Rectangle that will be filled and encompass the 5-min timeframe text box textBoxBorder5Min_XCoord = ChartPanel.W - MaxTextBoxStringSize - 25; textBoxBorder5Min_YCoord = 20; textBoxBorder5Min_Width = MaxTextBoxStringSize + 20; textBoxBorder5Min_Height = textBoxLayOut5Min.Metrics.Height + 20; //Coordiantes for the Rectangle that will be filled and encompass the 15-min timeframe text box and will be draw in the top right of the chart underneath the 5-min timeframe text box textBoxBorder15Min_XCoord = textBoxBorder5Min_XCoord; textBoxBorder15Min_YCoord = textBoxBorder5Min_YCoord + textBoxBorder5Min_Height + 10; textBoxBorder15Min_Width = MaxTextBoxStringSize + 20;; textBoxBorder15Min_Height = textBoxBorder5Min_Height; //Setup the rectangle object for the inner rectangle that contains the text SharpDX.RectangleF textBox5Min = new SharpDX.RectangleF(textBox5Min_XCoord , textBox5Min_YCoord, textBox5Min_Width, textBox5Min_Height); SharpDX.RectangleF textBox15Min = new SharpDX.RectangleF(textBox15Min_XCoord , textBox15Min_YCoord, textBox15Min_Width, textBox15Min_Height); //Setup the rectangle object for the outter rectangle that will encompass the textbox, color filled and have a border SharpDX.RectangleF textBoxBorder5Min = new SharpDX.RectangleF(textBoxBorder5Min_XCoord , textBoxBorder5Min_YCoord, textBoxBorder5Min_Width, textBoxBorder5Min_Height); SharpDX.RectangleF textBoxBorder15Min = new SharpDX.RectangleF(textBoxBorder15Min_XCoord , textBoxBorder15Min_YCoord, textBoxBorder15Min_Width, textBoxBorder15Min_Height); //Draw the outter rectangle RenderTarget.DrawRectangle(textBoxBorder5Min, textBox5minBrush.ToDxBrush(RenderTarget), 2); RenderTarget.DrawRectangle(textBoxBorder15Min, textBox15minBrush.ToDxBrush(RenderTarget), 2 ); //Filled the outter rectange with the timeframe alignment colour RenderTarget.FillRectangle(textBoxBorder5Min, FiveMinuteColor.ToDxBrush(RenderTarget)); RenderTarget.FillRectangle(textBoxBorder15Min, FifteenMinuteColour.ToDxBrush(RenderTarget)); //Draw the text to be laced inside each of the color filled rectangles RenderTarget.DrawText(Text5Min, textBoxFormat5Min, textBox5Min, textBox5minBrush.ToDxBrush(RenderTarget)); RenderTarget.DrawText(Text15Min, textBoxFormat15Min, textBox15Min, textBox15minBrush.ToDxBrush(RenderTarget)); textBoxFormat5Min.Dispose(); textBoxFormat15Min.Dispose(); textBoxLayOut5Min.Dispose(); textBoxLayOut5MinGreen.Dispose(); textBoxLayOut5MinRed.Dispose(); textBoxLayOut15Min.Dispose(); textBoxLayOut15MinGreen.Dispose(); textBoxLayOut15MinRed.Dispose(); }
Last edited by andrew-NT888; 04-11-2024, 05:51 AM.
- Likes 1
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
603 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
349 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
104 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
560 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
560 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment