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

How to draw text above some level correctly

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

    How to draw text above some level correctly

    Hello, I need to draw some horizontal line and then the text above it in OnRender method

    for drawing line I use
    Code:
    RenderTarget.DrawLine(new Vector2(x1, y), new Vector2(x2, y), lineBrush, lineWidth);
    Then I use this code to draw text above the line

    Code:
    SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont();
    using (var textFormat = wpfFont.ToDirectWriteTextFormat())
    {
       using (TextLayout textLayout = new TextLayout(
          Globals.DirectWriteFactory,
          MyText,
          textFormat, 1, textFormat.FontSize))
        {
           RenderTarget.DrawTextLayout(new Vector2(x1, y), textLayout, textBrush, DrawTextOptions.NoSnap);
         }
    }
    The text appears under the line because my y coordinate = top of the text rectangle by default. How can I tell to DrawTextLayout routine that my y coordinate is a bottom of the text rectangle?

    #2
    Hello rfsettling,

    Thanks for your inquiry.

    The solution would be to measure the font height from the TextLayout and then to use that value to offset the DrawTextLayout()'s y-coordinate.

    I've attached some sample code and a working example that uses it.

    Code:
    private int CheckFontHeight(SimpleFont font)
    {
    	int Height = 0;
    	
    	SharpDX.DirectWrite.TextFormat textFormat = font.ToDirectWriteTextFormat();
    	SharpDX.DirectWrite.TextLayout textLayout =
    		new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
    		"", textFormat, ChartPanel.X + ChartPanel.W,
    		textFormat.FontSize);
    
      	Height =  (int)textLayout.Metrics.Height;
    
    	textLayout.Dispose();
    	textFormat.Dispose();
    	
    	return Height;
    }
    If you have any additional questions, please don't hesitate to write back.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello, Jim

      I hoped there is a vertical alignment setting which I just could not find. Having vertical alignment would make the life easier...

      Anyway, thanks!

      Comment


        #4
        thank Jim, this gave me an idea

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by proptrade13, Today, 11:06 AM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by quantismo, 04-17-2024, 05:13 PM
        4 responses
        31 views
        0 likes
        Last Post quantismo  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        32 views
        0 likes
        Last Post love2code2trade  
        Started by cls71, Today, 04:45 AM
        2 responses
        10 views
        0 likes
        Last Post eDanny
        by eDanny
         
        Started by kulwinder73, Today, 10:31 AM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Erick  
        Working...
        X