Skip to content

Commit

Permalink
remove paint argument from IDrawable..Measure
Browse files Browse the repository at this point in the history
This makes the measure method easier to consume, also
it forces labels to have a paint before the measure method is called.
  • Loading branch information
beto-rodriguez committed Nov 30, 2024
1 parent 0f18bf6 commit 834f80a
Show file tree
Hide file tree
Showing 30 changed files with 98 additions and 67 deletions.
34 changes: 20 additions & 14 deletions src/LiveChartsCore/CoreAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ public void InvalidateCrosshair(Chart chart, LvcPoint pointerPosition)
_crosshairLabel.Padding = CrosshairPadding ?? _padding;
_crosshairLabel.X = x;
_crosshairLabel.Y = y;
_crosshairLabel.Paint = CrosshairLabelsPaint;

var r = (float)_labelsRotation;
var hasRotation = Math.Abs(r) > 0.01f;
Expand Down Expand Up @@ -844,10 +845,11 @@ public LvcSize GetNameLabelSize(Chart chart)
RotateTransform = Orientation == AxisOrientation.X
? 0
: InLineNamePlacement ? 0 : -90,
Padding = NamePadding
Padding = NamePadding,
Paint = NamePaint
};

return textGeometry.Measure(NamePaint);
return textGeometry.Measure();
}

/// <inheritdoc cref="IPlane.GetPossibleSize(Chart)"/>
Expand Down Expand Up @@ -883,9 +885,10 @@ public virtual LvcSize GetPossibleSize(Chart chart)
Text = TryGetLabelOrLogError(labeler, i),
TextSize = ts,
RotateTransform = r,
Padding = _padding
Padding = _padding,
Paint = LabelsPaint
};
var m = textGeometry.Measure(LabelsPaint);
var m = textGeometry.Measure();
if (m.Width > w) w = m.Width;
if (m.Height > h) h = m.Height;
}
Expand Down Expand Up @@ -1028,10 +1031,11 @@ private LvcSize GetPossibleMaxLabelSize()
Text = labeler(i),
TextSize = (float)_textSize,
RotateTransform = (float)LabelsRotation,
Padding = _padding
Padding = _padding,
Paint = LabelsPaint
};

var m = textGeometry.Measure(LabelsPaint);
var m = textGeometry.Measure();

maxLabelSize = new LvcSize(
maxLabelSize.Width > m.Width ? maxLabelSize.Width : m.Width,
Expand Down Expand Up @@ -1067,6 +1071,7 @@ private void DrawName(
_nameGeometry.Padding = NamePadding;
_nameGeometry.Text = Name ?? string.Empty;
_nameGeometry.TextSize = (float)_nameTextSize;
_nameGeometry.Paint = NamePaint;

if (_orientation == AxisOrientation.X)
{
Expand Down Expand Up @@ -1309,8 +1314,8 @@ private void UpdateLabel(
if (hasRotation && _labelsPaint is not null)
{
var notRotatedSize =
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text }
.Measure(_labelsPaint);
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text, Paint = _labelsPaint }
.Measure();

var rhx = Math.Cos((90 - actualRotatation) * toRadians) * notRotatedSize.Height;
x += (float)Math.Abs(rhx * 0.5f);
Expand All @@ -1324,8 +1329,8 @@ private void UpdateLabel(
if (hasRotation && _labelsPaint is not null)
{
var notRotatedSize =
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text }
.Measure(_labelsPaint);
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text, Paint = _labelsPaint }
.Measure();

var rhx = Math.Cos((90 - actualRotatation) * toRadians) * notRotatedSize.Height;
x -= (float)Math.Abs(rhx * 0.5f);
Expand All @@ -1351,8 +1356,8 @@ private void UpdateLabel(
if (hasRotation && _labelsPaint is not null)
{
var notRotatedSize =
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text }
.Measure(_labelsPaint);
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text, Paint = _labelsPaint }
.Measure();

var rhx = Math.Sin((90 - actualRotatation) * toRadians) * notRotatedSize.Height;
y += (float)Math.Abs(rhx * 0.5f);
Expand All @@ -1375,8 +1380,8 @@ private void UpdateLabel(
if (hasRotation && _labelsPaint is not null)
{
var notRotatedSize =
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text }
.Measure(_labelsPaint);
new TTextGeometry { TextSize = (float)_textSize, Padding = _padding, Text = text, Paint = _labelsPaint }
.Measure();

var rhx = Math.Sin((90 - actualRotatation) * toRadians) * notRotatedSize.Height;
y -= (float)Math.Abs(rhx * 0.5f);
Expand All @@ -1401,6 +1406,7 @@ private void UpdateLabel(
label.Padding = _padding;
label.X = x;
label.Y = y;
label.Paint = _labelsPaint;

if (hasRotation) label.RotateTransform = actualRotatation;

Expand Down
4 changes: 3 additions & 1 deletion src/LiveChartsCore/CoreBoxSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
var m = label.Measure(DataLabelsPaint);
label.Paint = DataLabelsPaint;

var m = label.Measure();
var labelPosition = GetLabelPosition(
x, high, helper.uw, Math.Abs(low - high), m, DataLabelsPosition,
SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CoreColumnSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,13 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var m = label.Measure(DataLabelsPaint);
var m = label.Measure();
var labelPosition = GetLabelPosition(
x, cy, helper.uw, b, m,
DataLabelsPosition, SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CoreFinancialSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,13 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var m = label.Measure(DataLabelsPaint);
var m = label.Measure();
var labelPosition = GetLabelPosition(
x, high, uw, Math.Abs(low - high), m, DataLabelsPosition,
SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CoreHeatSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,15 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var labelPosition = GetLabelPosition(
secondary - uws * 0.5f + p.Left, primary - uwp * 0.5f + p.Top, uws - p.Left - p.Right, uwp - p.Top - p.Bottom,
label.Measure(DataLabelsPaint), DataLabelsPosition, SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
label.Measure(), DataLabelsPosition, SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
label.X = labelPosition.X;
label.Y = labelPosition.Y;
}
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CoreLineSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,13 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(data.TargetPoint));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var m = label.Measure(DataLabelsPaint);
var m = label.Measure();
var labelPosition = GetLabelPosition(
x - hgs, y - hgs, gs, gs, m, DataLabelsPosition,
SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CorePieSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ public override void Invalidate(Chart chart)
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.RotateTransform = actualRotation;
label.Paint = DataLabelsPaint;

AlignLabel(label, (float)start, initialRotation, sweep);

Expand All @@ -429,7 +430,7 @@ public override void Invalidate(Chart chart)
(stackedOuterRadius + relativeOuterRadius * 2) * 0.5f,
(float)(start + initialRotation),
(float)sweep,
label.Measure(DataLabelsPaint),
label.Measure(),
DataLabelsPosition);

label.X = labelPosition.X;
Expand Down
10 changes: 6 additions & 4 deletions src/LiveChartsCore/CorePolarAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,11 @@ public LvcSize GetNameLabelSize(Chart chart)
{
Text = Name ?? string.Empty,
TextSize = (float)NameTextSize,
Padding = _labelsPadding
Padding = _labelsPadding,
Paint = NamePaint
};

return textGeometry.Measure(NamePaint);
return textGeometry.Measure();
}

/// <inheritdoc cref="IPlane.GetPossibleSize(Chart)"/>
Expand Down Expand Up @@ -489,9 +490,10 @@ public virtual LvcSize GetPossibleSize(Chart chart)
Text = labeler(i),
TextSize = ts,
RotateTransform = r + (_orientation == PolarAxisOrientation.Angle ? scaler.GetAngle(i) - 90 : 0),
Padding = _labelsPadding
Padding = _labelsPadding,
Paint = LabelsPaint
};
var m = textGeometry.Measure(LabelsPaint);
var m = textGeometry.Measure();

var h = (float)Math.Sqrt(Math.Pow(m.Width * 0.5, 2) + Math.Pow(m.Height * 0.5, 2));
if (h > totalH) totalH = h;
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CorePolarLineSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ public override void Invalidate(Chart chart)
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.RotateTransform = actualRotation;
label.Paint = DataLabelsPaint;

var rad = Math.Sqrt(Math.Pow(cp.X - scaler.CenterX, 2) + Math.Pow(cp.Y - scaler.CenterY, 2));

Expand All @@ -435,7 +436,7 @@ public override void Invalidate(Chart chart)

var labelPosition = GetLabelPolarPosition(
scaler.CenterX, scaler.CenterY, (float)rad, scaler.GetAngle(coordinate.SecondaryValue),
label.Measure(DataLabelsPaint), (float)GeometrySize, DataLabelsPosition);
label.Measure(), (float)GeometrySize, DataLabelsPosition);

label.X = labelPosition.X;
label.Y = labelPosition.Y;
Expand Down
7 changes: 4 additions & 3 deletions src/LiveChartsCore/CoreRowSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,16 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var m = label.Measure(DataLabelsPaint);
var m = label.Measure();
var labelPosition = GetLabelPosition(
cx, y, b, helper.uw, label.Measure(DataLabelsPaint),
DataLabelsPosition, SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
cx, y, b, helper.uw, m, DataLabelsPosition, SeriesProperties,
coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
if (DataLabelsTranslate is not null) label.TranslateTransform =
new LvcPoint(m.Width * DataLabelsTranslate.Value.X, m.Height * DataLabelsTranslate.Value.Y);

Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CoreScatterSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,13 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var m = label.Measure(DataLabelsPaint);
var m = label.Measure();
var labelPosition = GetLabelPosition(
x - hgs, y - hgs, gs, gs, m, DataLabelsPosition,
SeriesProperties, coordinate.PrimaryValue > 0, drawLocation, drawMarginSize);
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/CoreStepLineSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,13 @@ public override void Invalidate(Chart chart)
label.Text = DataLabelsFormatter(new ChartPoint<TModel, TVisual, TLabel>(point));
label.TextSize = dls;
label.Padding = DataLabelsPadding;
label.Paint = DataLabelsPaint;

if (isFirstDraw)
label.CompleteTransition(
nameof(label.TextSize), nameof(label.X), nameof(label.Y), nameof(label.RotateTransform));

var m = label.Measure(DataLabelsPaint);
var m = label.Measure();
var labelPosition = GetLabelPosition(
x - hgs, y - hgs, gs, gs, m, DataLabelsPosition,
SeriesProperties, coordinate.PrimaryValue > Pivot, drawLocation, drawMarginSize);
Expand Down
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreBoxGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public float Median
set => _medProperty.SetMovement(value, this);
}

/// <inheritdoc cref="CoreGeometry.Measure(Paint)" />
public override LvcSize Measure(Paint paintTasks) =>
/// <inheritdoc cref="CoreGeometry.Measure()" />
public override LvcSize Measure() =>
new(Width, Math.Abs(Min - Y));
}
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreCandlestickGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public float Low
set => _lProperty.SetMovement(value, this);
}

/// <inheritdoc cref="CoreGeometry.Measure(Paint)" />
public override LvcSize Measure(Paint paintTasks) =>
/// <inheritdoc cref="CoreGeometry.Measure()" />
public override LvcSize Measure() =>
new(Width, Math.Abs(Low - Y));
}
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ public Paint? Fill
}
}

/// <inheritdoc cref="IDrawable.Measure(Paint)"/>
public abstract LvcSize Measure(Paint drawableTask);
/// <inheritdoc cref="IDrawable.Measure()"/>
public abstract LvcSize Measure();
}
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreLabelGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,6 @@ public Paint? Paint
Paint? IDrawable.Stroke { get; set; }
Paint? IDrawable.Fill { get; set; }

/// <inheritdoc cref="IDrawable.Measure(Paint)"/>
public abstract LvcSize Measure(Paint drawableTask);
/// <inheritdoc cref="IDrawable.Measure()"/>
public abstract LvcSize Measure();
}
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreLineGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public float Y1
set => _y1.SetMovement(value, this);
}

/// <inheritdoc cref="CoreGeometry.Measure(Paint)" />
public override LvcSize Measure(Paint drawable) =>
/// <inheritdoc cref="CoreGeometry.Measure()" />
public override LvcSize Measure() =>
new(Math.Abs(X1 - X), Math.Abs(Y1 - Y));
}
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreNeedleGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public float Width
set => _wProperty.SetMovement(value, this);
}

/// <inheritdoc cref="CoreGeometry.Measure(Paint)"/>
public override LvcSize Measure(Paint paintTasks) =>
/// <inheritdoc cref="IDrawable.Measure()"/>
public override LvcSize Measure() =>
new(Width, Radius);
}

4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreSizedGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public float Height
set => _heightProperty.SetMovement(value, this);
}

/// <inheritdoc cref="CoreGeometry.Measure(Paint)" />
public override LvcSize Measure(Paint paint) =>
/// <inheritdoc cref="CoreGeometry.Measure()" />
public override LvcSize Measure() =>
new(Width, Height);
}
4 changes: 2 additions & 2 deletions src/LiveChartsCore/Drawing/CoreVectorGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,6 @@ public override void CompleteTransition(params string[]? propertyName)
base.CompleteTransition(propertyName);
}

/// <inheritdoc cref="IDrawable.Measure(Paint)" />
public LvcSize Measure(Paint paint) => new();
/// <inheritdoc cref="IDrawable.Measure()" />
public LvcSize Measure() => new();
}
3 changes: 1 addition & 2 deletions src/LiveChartsCore/Drawing/IDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ public interface IDrawable : ITimeLineElement
/// <summary>
/// Measures the instance.
/// </summary>
/// <param name="paint">The paint.</param>
/// <returns>The size.</returns>
LvcSize Measure(Paint paint);
LvcSize Measure();
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/LiveChartsCore/Kernel/SeriesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ private void CalculatePieLabelsOuterSpace<TLabel>()
label.TextSize = (float)series.DataLabelsSize;
label.Padding = series.DataLabelsPadding;
label.RotateTransform = r;
var labelSize = label.Measure(series.DataLabelsPaint);
label.Paint = series.DataLabelsPaint;
var labelSize = label.Measure();

var h = 1.5f * (float)Math.Sqrt(Math.Pow(labelSize.Width, 2) + Math.Pow(labelSize.Height, 2));
if (h > _pieLabelsSize) _pieLabelsSize = h;
Expand Down
Loading

0 comments on commit 834f80a

Please sign in to comment.