Is there a way to display text on a black rectangle on chart where the rectangle is the exact width of the text it's under?

Created at 09 Jun 2022, 08:24
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
FI

firemyst

Joined 26.03.2019

Is there a way to display text on a black rectangle on chart where the rectangle is the exact width of the text it's under?
09 Jun 2022, 08:24


HI all:

So I'm writing text to the chart, but find it difficult to find a text color that nicely contrasts with all the other colors on the chart (eg, candle colors, indicator colors, etc) that all text is always easily readable.

What I'd like to do is set the text on a black (or other color) rectangle so that the text is crystal clear sitting on a solid background.

What I'm having issues with is getting the length of the text being written so I know how long to make the rectangles they're sitting on.

And then adjusting the length of those rectangles (or whatever) as the text changes (gets shorter or longer as info changes)

Is there a way to get the length of the text as it's drawn on the chart so that I can then make the corresponding rectangle the corresponding width so it fits precisely underneath the text without having to stretch across the whole chart?

I'm not set on it being a rectangle - I'm open to other options that will give me a solid background and hide bars/indicators/everything else underneath it.

Thank you


@firemyst
Replies

amusleh
09 Jun 2022, 09:44

Hi,

You can't do this with Chart objects, but you can use controls:

using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot2 : Robot
    {
        protected override void OnStart()
        {
            var textBlock = new TextBlock
            {
                Text = "Text",
                BackgroundColor = Color.Red,
                ForegroundColor = Color.White,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Padding = 5
            };

            Chart.AddControl(textBlock);
        }

        protected override void OnStop()
        {
        }
    }
}

The problem with controls is that they are static, so you can't get the same behavior like a ChartText object.


@amusleh