Skip to content

Commit

Permalink
Column Chart - Position data labels above, level or below bar if posi…
Browse files Browse the repository at this point in the history
…tive, zero or negative, respectively. (#1270)

Bar Chart - Position data labels right, level or left of bar if positive, zero or negative, respectively.
  • Loading branch information
paulo-rico authored Dec 6, 2023
1 parent c711a0c commit 8ed441d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
12 changes: 8 additions & 4 deletions Radzen.Blazor/RadzenBarSeries.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,16 @@ public override IEnumerable<ChartDataLabel> GetDataLabels(double offsetX, double
{
var list = new List<ChartDataLabel>();

(string Anchor, int Sign) position;

foreach (var d in Data)
{
list.Add(new ChartDataLabel
{
Position = new Point() { X = TooltipX(d) + offsetX + 8, Y = TooltipY(d) + offsetY },
TextAnchor = "start",
position = Value(d) < 0 ? ("end", -1) : Value(d) == 0 ? ("middle", 0) : ("start", 1);

list.Add(new ChartDataLabel
{
Position = new Point() { X = TooltipX(d) + offsetX + (8 * position.Sign), Y = TooltipY(d) + offsetY },
TextAnchor = position.Anchor,
Text = Chart.ValueAxis.Format(Chart.CategoryScale, Value(d))
});
}
Expand Down
24 changes: 18 additions & 6 deletions Radzen.Blazor/RadzenColumnSeries.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ internal override double TooltipX(TItem item)
/// <inheritdoc />
internal override double TooltipY(TItem item)
{
var y = base.TooltipY(item);
var ticks = Chart.ValueScale.Ticks(Chart.ValueAxis.TickDistance);
var y0 = Chart.ValueScale.Scale(Math.Max(0, ticks.Start));

return Math.Min(y, y0);
return base.TooltipY(item);
}

/// <inheritdoc />
Expand Down Expand Up @@ -195,7 +191,23 @@ public override (object, Point) DataAt(double x, double y)
/// <inheritdoc />
public override IEnumerable<ChartDataLabel> GetDataLabels(double offsetX, double offsetY)
{
return base.GetDataLabels(offsetX, offsetY - 16);
var list = new List<ChartDataLabel>();

int sign;

foreach (var d in Data)
{
sign = Value(d) < 0 ? -1 : Value(d) == 0 ? 0 : 1;

list.Add(new ChartDataLabel
{
Position = new Point() { X = TooltipX(d) + offsetX, Y = TooltipY(d) - offsetY - (16 * sign) },
TextAnchor = "middle",
Text = Chart.ValueAxis.Format(Chart.ValueScale, Value(d))
});
}

return list;
}
}
}

0 comments on commit 8ed441d

Please sign in to comment.