Update maximum cell width and height to be dynamic according to text size

This commit is contained in:
moshferatu 2023-11-12 06:01:11 -08:00
parent e9b5002acb
commit f27075712b

View File

@ -45,6 +45,8 @@ namespace NinjaTrader.NinjaScript.Indicators
HeaderColor = Brushes.Yellow;
HeaderTextColor = Brushes.Black;
RSIValuesTextColor = Brushes.White;
HeaderFont = new SimpleFont("Arial", 12);
RSIValuesFont = new SimpleFont("Arial", 12);
}
else if (State == State.Configure)
{
@ -111,35 +113,53 @@ namespace NinjaTrader.NinjaScript.Indicators
{
base.OnRender(chartControl, chartScale);
// Calculate maximum column width based on time frame text.
int maxColWidth = 0;
foreach (var timeFrame in TimeFrames)
int maxHeaderHeight = 0;
int maxRowHeight = 0;
using (TextFormat headerTextFormat = HeaderFont.ToDirectWriteTextFormat(),
rsiTextFormat = RSIValuesFont.ToDirectWriteTextFormat())
{
var size = MeasureString(timeFrame, new TextFormat(Core.Globals.DirectWriteFactory, "Arial", 12));
maxColWidth = Math.Max(maxColWidth, size.Width);
// Calculate maximum header text size
foreach (var timeFrame in TimeFrames)
{
var headerSize = MeasureString(timeFrame, headerTextFormat);
maxColWidth = Math.Max(maxColWidth, headerSize.Width);
maxHeaderHeight = Math.Max(maxHeaderHeight, headerSize.Height);
}
// Calculate maximum RSI value text size
foreach (var rsiValue in RSIValues)
{
var rsiText = rsiValue.ToString("F2");
var rsiSize = MeasureString(rsiText, rsiTextFormat);
maxColWidth = Math.Max(maxColWidth, rsiSize.Width);
maxRowHeight = Math.Max(maxRowHeight, rsiSize.Height);
}
}
maxColWidth += 40; // Adding padding of 40 pixels (20 on each side).
int headerHeight = maxHeaderHeight + 10; // Additional padding for header.
int rowHeight = maxRowHeight + 10; // Additional padding for rows.
int x = ChartPanel.W - (maxColWidth * TimeFrames.Length) - 20; // Adjust starting position due to padding.
int y = 10;
int rowHeight = 20;
int headerHeight = 20;
int y = 0;
// Draw header row.
foreach (var timeFrame in TimeFrames)
using (TextFormat headerTextFormat = HeaderFont.ToDirectWriteTextFormat())
{
RectangleF headerRect = new RectangleF(x, y, maxColWidth, headerHeight);
RenderTarget.FillRectangle(headerRect, HeaderColor.ToDxBrush(RenderTarget));
foreach (var timeFrame in TimeFrames)
{
RectangleF headerRect = new RectangleF(x, y, maxColWidth, headerHeight);
RenderTarget.FillRectangle(headerRect, HeaderColor.ToDxBrush(RenderTarget));
var textFormat = new TextFormat(Core.Globals.DirectWriteFactory, "Arial", FontWeight.Normal, FontStyle.Normal, 12);
var size = MeasureString(timeFrame, headerTextFormat);
int textX = x + (maxColWidth - size.Width) / 2; // Center the text horizontally.
int textY = y + (headerHeight - size.Height) / 2; // Center the text vertically.
var size = MeasureString(timeFrame, textFormat);
int textX = x + (maxColWidth - size.Width) / 2; // Center the text horizontally.
int textY = y + (headerHeight - size.Height) / 2; // Center the text vertically.
DrawText(timeFrame, textX, textY, HeaderTextColor, textFormat, headerHeight);
x += maxColWidth;
DrawText(timeFrame, textX, textY, HeaderTextColor, headerTextFormat, headerHeight);
x += maxColWidth;
}
}
// Reset x coordinate for RSI values.
@ -147,21 +167,24 @@ namespace NinjaTrader.NinjaScript.Indicators
y += headerHeight;
// Draw RSI value row.
for (int i = 0; i < RSIValues.Length; i++)
using (TextFormat rsiTextFormat = RSIValuesFont.ToDirectWriteTextFormat())
{
var rsiText = RSIValues[i].ToString("F2"); // Format RSI value to two decimal places.
var size = MeasureString(rsiText, new TextFormat(Core.Globals.DirectWriteFactory, "Arial", 12));
int textX = x + (maxColWidth - size.Width) / 2; // Center the text horizontally.
int textY = y + (rowHeight - size.Height) / 2; // Center the text vertically.
for (int i = 0; i < RSIValues.Length; i++)
{
var rsiText = RSIValues[i].ToString("F2"); // Format RSI value to two decimal places.
var size = MeasureString(rsiText, rsiTextFormat);
int textX = x + (maxColWidth - size.Width) / 2; // Center the text horizontally.
int textY = y + (rowHeight - size.Height) / 2; // Center the text vertically.
Brush bgColor = BetweenThresholdsColor;
if (RSIValues[i] > UpperThreshold) bgColor = AboveThresholdColor;
else if (RSIValues[i] < LowerThreshold) bgColor = BelowThresholdColor;
Brush bgColor = BetweenThresholdsColor;
if (RSIValues[i] > UpperThreshold) bgColor = AboveThresholdColor;
else if (RSIValues[i] < LowerThreshold) bgColor = BelowThresholdColor;
RectangleF rect = new RectangleF(x, y, maxColWidth, rowHeight);
RenderTarget.FillRectangle(rect, bgColor.ToDxBrush(RenderTarget));
DrawText(rsiText, textX, textY, RSIValuesTextColor, new TextFormat(Core.Globals.DirectWriteFactory, "Arial", 12), rowHeight);
x += maxColWidth;
RectangleF rect = new RectangleF(x, y, maxColWidth, rowHeight);
RenderTarget.FillRectangle(rect, bgColor.ToDxBrush(RenderTarget));
DrawText(rsiText, textX, textY, RSIValuesTextColor, rsiTextFormat, rowHeight);
x += maxColWidth;
}
}
}
@ -209,33 +232,41 @@ namespace NinjaTrader.NinjaScript.Indicators
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Above Threshold", Order = 5, GroupName = "RSI Dashboard")]
[Display(Name = "Above Threshold", GroupName = "RSI Dashboard", Order = 5)]
public Brush AboveThresholdColor { get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Below Threshold", Order = 6, GroupName = "RSI Dashboard")]
[Display(Name = "Below Threshold", GroupName = "RSI Dashboard", Order = 6)]
public Brush BelowThresholdColor { get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Between Thresholds", Order = 7, GroupName = "RSI Dashboard")]
[Display(Name = "Between Thresholds", GroupName = "RSI Dashboard", Order = 7)]
public Brush BetweenThresholdsColor { get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Header Row", Order = 8, GroupName = "RSI Dashboard")]
[Display(Name = "Header Row", GroupName = "RSI Dashboard", Order = 8)]
public Brush HeaderColor { get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "Header Text", Order = 9, GroupName = "RSI Dashboard")]
[Display(Name = "Header Text", GroupName = "RSI Dashboard", Order = 9)]
public Brush HeaderTextColor { get; set; }
[NinjaScriptProperty]
[XmlIgnore]
[Display(Name = "RSI Values", Order = 10, GroupName = "RSI Dashboard")]
[Display(Name = "RSI Values", GroupName = "RSI Dashboard", Order = 10)]
public Brush RSIValuesTextColor { get; set; }
[NinjaScriptProperty]
[Display(Name = "Header Font", GroupName = "RSI Dashboard", Order = 11)]
public SimpleFont HeaderFont { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Values Font", GroupName = "RSI Dashboard", Order = 12)]
public SimpleFont RSIValuesFont { get; set; }
#endregion
}
}
@ -247,18 +278,18 @@ namespace NinjaTrader.NinjaScript.Indicators
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private RSIDashboard[] cacheRSIDashboard;
public RSIDashboard RSIDashboard(int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor)
public RSIDashboard RSIDashboard(int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor, SimpleFont headerFont, SimpleFont rSIValuesFont)
{
return RSIDashboard(Input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor);
return RSIDashboard(Input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor, headerFont, rSIValuesFont);
}
public RSIDashboard RSIDashboard(ISeries<double> input, int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor)
public RSIDashboard RSIDashboard(ISeries<double> input, int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor, SimpleFont headerFont, SimpleFont rSIValuesFont)
{
if (cacheRSIDashboard != null)
for (int idx = 0; idx < cacheRSIDashboard.Length; idx++)
if (cacheRSIDashboard[idx] != null && cacheRSIDashboard[idx].RSIPeriod == rSIPeriod && cacheRSIDashboard[idx].RSISmoothing == rSISmoothing && cacheRSIDashboard[idx].UpperThreshold == upperThreshold && cacheRSIDashboard[idx].LowerThreshold == lowerThreshold && cacheRSIDashboard[idx].AboveThresholdColor == aboveThresholdColor && cacheRSIDashboard[idx].BelowThresholdColor == belowThresholdColor && cacheRSIDashboard[idx].BetweenThresholdsColor == betweenThresholdsColor && cacheRSIDashboard[idx].HeaderColor == headerColor && cacheRSIDashboard[idx].HeaderTextColor == headerTextColor && cacheRSIDashboard[idx].RSIValuesTextColor == rSIValuesTextColor && cacheRSIDashboard[idx].EqualsInput(input))
if (cacheRSIDashboard[idx] != null && cacheRSIDashboard[idx].RSIPeriod == rSIPeriod && cacheRSIDashboard[idx].RSISmoothing == rSISmoothing && cacheRSIDashboard[idx].UpperThreshold == upperThreshold && cacheRSIDashboard[idx].LowerThreshold == lowerThreshold && cacheRSIDashboard[idx].AboveThresholdColor == aboveThresholdColor && cacheRSIDashboard[idx].BelowThresholdColor == belowThresholdColor && cacheRSIDashboard[idx].BetweenThresholdsColor == betweenThresholdsColor && cacheRSIDashboard[idx].HeaderColor == headerColor && cacheRSIDashboard[idx].HeaderTextColor == headerTextColor && cacheRSIDashboard[idx].RSIValuesTextColor == rSIValuesTextColor && cacheRSIDashboard[idx].HeaderFont == headerFont && cacheRSIDashboard[idx].RSIValuesFont == rSIValuesFont && cacheRSIDashboard[idx].EqualsInput(input))
return cacheRSIDashboard[idx];
return CacheIndicator<RSIDashboard>(new RSIDashboard(){ RSIPeriod = rSIPeriod, RSISmoothing = rSISmoothing, UpperThreshold = upperThreshold, LowerThreshold = lowerThreshold, AboveThresholdColor = aboveThresholdColor, BelowThresholdColor = belowThresholdColor, BetweenThresholdsColor = betweenThresholdsColor, HeaderColor = headerColor, HeaderTextColor = headerTextColor, RSIValuesTextColor = rSIValuesTextColor }, input, ref cacheRSIDashboard);
return CacheIndicator<RSIDashboard>(new RSIDashboard(){ RSIPeriod = rSIPeriod, RSISmoothing = rSISmoothing, UpperThreshold = upperThreshold, LowerThreshold = lowerThreshold, AboveThresholdColor = aboveThresholdColor, BelowThresholdColor = belowThresholdColor, BetweenThresholdsColor = betweenThresholdsColor, HeaderColor = headerColor, HeaderTextColor = headerTextColor, RSIValuesTextColor = rSIValuesTextColor, HeaderFont = headerFont, RSIValuesFont = rSIValuesFont }, input, ref cacheRSIDashboard);
}
}
}
@ -267,14 +298,14 @@ namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.RSIDashboard RSIDashboard(int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor)
public Indicators.RSIDashboard RSIDashboard(int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor, SimpleFont headerFont, SimpleFont rSIValuesFont)
{
return indicator.RSIDashboard(Input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor);
return indicator.RSIDashboard(Input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor, headerFont, rSIValuesFont);
}
public Indicators.RSIDashboard RSIDashboard(ISeries<double> input , int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor)
public Indicators.RSIDashboard RSIDashboard(ISeries<double> input , int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor, SimpleFont headerFont, SimpleFont rSIValuesFont)
{
return indicator.RSIDashboard(input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor);
return indicator.RSIDashboard(input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor, headerFont, rSIValuesFont);
}
}
}
@ -283,14 +314,14 @@ namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.RSIDashboard RSIDashboard(int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor)
public Indicators.RSIDashboard RSIDashboard(int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor, SimpleFont headerFont, SimpleFont rSIValuesFont)
{
return indicator.RSIDashboard(Input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor);
return indicator.RSIDashboard(Input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor, headerFont, rSIValuesFont);
}
public Indicators.RSIDashboard RSIDashboard(ISeries<double> input , int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor)
public Indicators.RSIDashboard RSIDashboard(ISeries<double> input , int rSIPeriod, int rSISmoothing, double upperThreshold, double lowerThreshold, Brush aboveThresholdColor, Brush belowThresholdColor, Brush betweenThresholdsColor, Brush headerColor, Brush headerTextColor, Brush rSIValuesTextColor, SimpleFont headerFont, SimpleFont rSIValuesFont)
{
return indicator.RSIDashboard(input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor);
return indicator.RSIDashboard(input, rSIPeriod, rSISmoothing, upperThreshold, lowerThreshold, aboveThresholdColor, belowThresholdColor, betweenThresholdsColor, headerColor, headerTextColor, rSIValuesTextColor, headerFont, rSIValuesFont);
}
}
}