Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

OnRender - How to right justify text (TextAlignment.Right)?

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

    OnRender - How to right justify text (TextAlignment.Right)?

    Hey guys,

    I'm trying to right justify text with "RenderTarget.DrawTextLayout".

    In OBU, I could do like this:
    TextFixed MyDrawText = Draw.TextFixed(...);
    MyDrawText.Alignment = TextAlignment.Right;

    In OR, the code below just gives me the options:
    Center
    Leading
    Trailing

    Code:
    textFormat1.TextAlignment = SharpDX.DirectWrite.TextAlignment.
    Does anyone know how to right justify text in OnRender?​

    #2
    I was just looking at this ...

    Try studying the OnRender code in the Watermark indicator.

    Comment


      #3
      hey bltdavid

      I've read a lot of your posts. Nice to see you in one of my posts.

      I looked at the "Watermark" indicator you mentioned, but it seems to me that the code reproduces in OR the "TextPosition" property of OBU drawings.

      Code:
      TextPosition.
      BottomLeft
      BottomRight
      Center
      TopLeft
      TopRight
      I am trying to reproduce the "TextAlignment" property.

      Code:
      TextAlignment.
      Center
      Justify
      Left
      Right;
      More specifically "TextAlignment.Right".​

      Comment


        #4
        Think about what you're trying to do. You want to render the text at a specific position minus the width of the text you are rendering, so that the right side of the text will be at that position. So, if you know the position you want to be at the right, you just need to measure the width of the text and subtract that from the position to know where to render the text. You can do that without anything fancy in the text layout.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Hey, QuantKey_Bruce

          I don't really see how doing additions and subtractions with "width" the text will change the justification.

          I made two gifs that show the difference between "TextPosition" and "TextAlignment".

          "TextPosition":

          Click image for larger version

Name:	ex2.gif
Views:	118
Size:	143.3 KB
ID:	1244109

          "TextAlignment":

          Click image for larger version

Name:	ex1.gif
Views:	138
Size:	68.8 KB
ID:	1244108

          * Note that the second line starts from right to left which is equivalent to "TextAlignment.Right".​

          Comment


            #6
            If you are just rendering one line of text, you can measure its width, subtract that width from the right-side X coordinate, and render it there. It will be "right-justified" to the left of that point.

            If you are rendering more than one line of text, then you have at least two choices - you can render the lines one at a time just like I described above, or you can use a TextLayout to justify within the rectangle you are rendering to and use the override that uses a TextLayout instead of the simpler one. The rectangle still has to be wide enough to contain all of the lines, so unless you use some huge rectangle "just in case" you'll be measuring it either way.
            Last edited by QuantKey_Bruce; 04-01-2023, 02:55 PM.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              what if you have 2 or 3 lines?
              Are you going to call "RenderTarget.DrawTextLayout" for each line?​

              Comment


                #8
                Originally posted by rafaelcoisa View Post
                what if you have 2 or 3 lines?
                Are you going to call "RenderTarget.DrawTextLayout" for each line?​
                rafaelcoisa No, you can do it in one render operation with something similar to this example which you'll need to adapt to your specific situation and which is not optimized for speed but for clarity and explicitness:

                Code:
                #region Using declarations
                using System.Linq;
                using System.Windows;
                using System.Windows.Media;
                using NinjaTrader.Gui;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Gui.Tools;
                using System.Windows.Documents;
                #endregion
                
                //This namespace holds Indicators in this folder and is required. Do not change it.
                namespace NinjaTrader.NinjaScript.Indicators
                {
                    public class NT8RightJustifyTextExample : Indicator
                    {
                        private SimpleFont MyTextSimpleFont = new SimpleFont("System", 14) { Bold = true, Italic = true };
                
                        private string MyTextString =
                            "This is on the first line" + System.Environment.NewLine +
                            "And this is on the second line" + System.Environment.NewLine +
                            "While this is the third line";
                
                        private Brush MyTextBrush = Brushes.LightBlue;
                
                        protected override void OnStateChange()
                        {
                            if (State == State.SetDefaults)
                            {
                                Name = "NinjaTrader 8 Right-Justify Text Example";
                                Description = "This example right justifies three lines of text at the right margin of the chart vertically centered on price";
                
                                IsOverlay = true;
                            }
                        }
                
                        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
                        {
                            base.OnRender(chartControl, chartScale);
                
                            if (BarsArray == null) return;
                            int BarsArrayCount = BarsArray[0].Count;
                            if (BarsArrayCount <= 0) return;
                
                            double LastPrice = BarsArray[0].GetClose(BarsArrayCount - 1);
                            int LastPricePixelY = chartScale.GetYByValue(LastPrice);
                
                            FormattedText MyFormattedText = new FormattedText(
                                MyTextString,
                                System.Globalization.CultureInfo.CurrentCulture,
                                FlowDirection.LeftToRight,
                                MyTextSimpleFont.Typeface,
                                MyTextSimpleFont.Size,
                                MyTextBrush,
                                VisualTreeHelper.GetDpi(chartControl).PixelsPerDip);
                
                            float MyFormattedTextWidth = (float)MyFormattedText.Width;
                            float MyFormattedTextHeight = (float)MyFormattedText.Height;
                
                            using (SharpDX.Direct2D1.Brush MyTextBrushDX = MyTextBrush.ToDxBrush(RenderTarget))
                            using (SharpDX.DirectWrite.TextFormat MyTextFormat = new SharpDX.DirectWrite.TextFormat(
                                Core.Globals.DirectWriteFactory,
                                MyTextSimpleFont.Family.ToString(),
                                (MyTextSimpleFont.Bold ? SharpDX.DirectWrite.FontWeight.Bold : SharpDX.DirectWrite.FontWeight.Normal),
                                (MyTextSimpleFont.Italic ? SharpDX.DirectWrite.FontStyle.Italic : SharpDX.DirectWrite.FontStyle.Normal),
                                SharpDX.DirectWrite.FontStretch.Normal,
                                (float)MyTextSimpleFont.Size)
                                    { TextAlignment = SharpDX.DirectWrite.TextAlignment.Trailing })
                            {
                                RenderTarget.DrawText(
                                    MyTextString,
                                    MyTextFormat,
                                    new SharpDX.RectangleF(
                                        chartControl.CanvasRight - MyFormattedTextWidth,
                                        LastPricePixelY - (MyFormattedTextHeight / 2f),
                                        MyFormattedTextWidth,
                                        MyFormattedTextHeight),
                                    MyTextBrushDX);
                            }
                        }
                    }
                }
                Click image for larger version  Name:	image.png Views:	0 Size:	30.1 KB ID:	1244143

                Do you see now how I am subtracting the width of the text to get where to render it?​
                Last edited by QuantKey_Bruce; 04-02-2023, 06:33 AM.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  I edited the post above with a more explicit example of how to do this.
                  Bruce DeVault
                  QuantKey Trading Vendor Services
                  NinjaTrader Ecosystem Vendor - QuantKey

                  Comment


                    #10
                    Hey, QuantKey_Bruce
                    Thanks for your time man.​​

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by GLFX005, Today, 03:23 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post GLFX005
                    by GLFX005
                     
                    Started by XXtrader, Yesterday, 11:30 PM
                    2 responses
                    11 views
                    0 likes
                    Last Post XXtrader  
                    Started by Waxavi, Today, 02:10 AM
                    0 responses
                    6 views
                    0 likes
                    Last Post Waxavi
                    by Waxavi
                     
                    Started by TradeForge, Today, 02:09 AM
                    0 responses
                    11 views
                    0 likes
                    Last Post TradeForge  
                    Started by Waxavi, Today, 02:00 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post Waxavi
                    by Waxavi
                     
                    Working...
                    X