Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

change font size according to bar width

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

    change font size according to bar width

    is that possible?

    Best regards
    Thomas

    #2
    Hello Thomas,

    Thanks for your post.

    Are you referring to changing the font size of the text of a drawing object, such as Draw.Text(), so that the font size is equal to the bar width?

    If so, this could be possible by using ChartControl.BarWidth to get the width of the bars and save that value to a double variable.

    double barWidth = ChartControl.BarWidth;

    Then, you could use the Draw.Text() syntax that allows you to specify a SimpleFont argument. You would use the barWidth variable when specifying the Size of your SimpleFont.

    Draw.Text(NinjaScriptBase owner, string tag, bool isAutoScale, string text, int barsAgo, double y, int yPixelOffset, Brush textBrush, SimpleFont font, TextAlignment alignment, Brush outlineBrush, Brush areaBrush, int areaOpacity)

    The code might look something like this:

    NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = barWidth, Bold = true };

    Draw.Text(this, "myTag", false, "Hi There!", 0, Low[0], 5, Brushes.Pink, myFont, TextAlignment.Center, Brushes.Transparent, null, 1);


    See the help guide documentation below for more information and sample code.

    BarWidth: https://ninjatrader.com/support/help...l_barwidth.htm
    Draw.Text: https://ninjatrader.com/support/help.../draw_text.htm
    SimpleFont: https://ninjatrader.com/support/help...font_class.htm

    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      many thanks, found the error

      my code was:
      if (ChartControl.BarWidth==2) myFontW.Size = 20;
      if (ChartControl.BarWidth==3) myFontW.Size = 30;
      if (ChartControl.BarWidth==4) myFontW.Size = 40;

      but should have been:
      if (ChartControl.BarWidth>1.9) myFontW.Size = 20;
      if (ChartControl.BarWidth>2.9) myFontW.Size = 30;
      if (ChartControl.BarWidth>3.9) myFontW.Size = 40;

      Comment


        #4
        Hi Brandon,
        I also wanted to adjust the font text size dynamically with the change in bar width. Tried your suggestion
        HTML Code:
        double barWidth = ChartControl.BarWidth;
        NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = barWidth, Bold = true };
        but keep getting the following error : An object reference is required for the non-static field, method, or property 'NinjaTrader.Gui.Chart.ChartControl.BarWidth.get"

        I am not sure what the issue is. Request assistance.

        Comment


          #5
          Hello kashter,

          Thanks for your note.

          Are you calling this code within the OnBarUpdate() method of an indicator?

          Please see the attached example indicator demonstrating how to set the font size of the Draw.Text() method to use the same value as the chart's BarWidth.

          Let me know if I may assist further.
          Attached Files
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #6
            Thank you Brandon, much appreciated.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            633 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            364 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            567 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X