Skip to content

Commit

Permalink
Fixed letter spacing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
eman1986 committed Aug 9, 2020
1 parent 3e0d98d commit d6201ce
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 26 deletions.
4 changes: 2 additions & 2 deletions NuGet/Package.Forms.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Microcharts.Forms</id>
<version>0.9.5.6</version>
<version>0.9.5.7</version>
<title>Microcharts for Xamarin.Forms</title>
<authors>Aloïs Deniel, Ed Lomonaco</authors>
<owners>Aloïs Deniel, Ed Lomonaco</owners>
Expand All @@ -14,7 +14,7 @@
<tags>xamarin.forms chart skia</tags>
<dependencies>
<group targetFramework="netstandard2.0">
<dependency id="Microcharts" version="0.9.5.6" />
<dependency id="Microcharts" version="0.9.5.7" />
<dependency id="SkiaSharp" version="2.80.1" />
<dependency id="SkiaSharp.Views.Forms" version="2.80.1" />
<dependency id="System.ValueTuple" version="4.5.0" />
Expand Down
2 changes: 1 addition & 1 deletion NuGet/Package.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Microcharts</id>
<version>0.9.5.6</version>
<version>0.9.5.7</version>
<title>Microcharts</title>
<authors>Aloïs Deniel, Ed Lomonaco</authors>
<owners>Aloïs Deniel, Ed Lomonaco</owners>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Microcharts.Samples.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected override void OnCreate(Bundle savedInstanceState)
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

var charts = Data.RightToLeftTest();
var charts = Data.CreateQuickstart();

FindViewById<ChartView>(Resource.Id.chartView1).Chart = charts[0];
FindViewById<ChartView>(Resource.Id.chartView2).Chart = charts[1];
Expand Down
71 changes: 55 additions & 16 deletions Sources/Microcharts.Samples/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,24 @@ public static Chart[] RightToLeftTest()
Label = "שבוע 3",
ValueLabel = "100",
Color = SKColor.Parse("#90D585"),
},
}
};

return new Chart[]
{
new BarChart
{
Entries = entries,
LabelTextSize = 42,
LabelTextSize = 60,
LabelColor = SKColors.Red,
LabelOrientation = Orientation.Horizontal,
TextDirection = TextDirection.RTL
},
new PointChart
{
Entries = entries,
LabelTextSize = 42,
LabelTextSize = 60,
LabelColor = SKColors.Red,
LabelOrientation = Orientation.Horizontal,
TextDirection = TextDirection.RTL
},
Expand All @@ -206,29 +208,30 @@ public static Chart[] RightToLeftTest()
Entries = entries,
LineMode = LineMode.Straight,
LineSize = 8,
LabelTextSize = 42,
LabelTextSize = 60,
LabelColor = SKColors.Red,
PointMode = PointMode.Square,
PointSize = 18,
TextDirection = TextDirection.RTL
},
new DonutChart
{
Entries = entries,
LabelTextSize = 42,
LabelTextSize = 60,
GraphPosition = GraphPosition.Center,
LabelMode = LabelMode.RightOnly,
TextDirection = TextDirection.RTL
},
new RadialGaugeChart
{
Entries = entries,
LabelTextSize = 42,
LabelTextSize = 60,
TextDirection = TextDirection.RTL
},
new RadarChart
{
Entries = entries,
LabelTextSize = 42,
LabelTextSize = 60,
TextDirection = TextDirection.RTL
}
};
Expand All @@ -240,32 +243,68 @@ public static Chart[] CreateQuickstart()
{
new ChartEntry(200)
{
Label = "January",
Label = "Week 1",
ValueLabel = "200",
Color = SKColor.Parse("#266489"),
},
new ChartEntry(400)
{
Label = "February",
Label = "Week 2",
ValueLabel = "400",
Color = SKColor.Parse("#68B9C0"),
},
new ChartEntry(100)
{
Label = "March",
Label = "Week 3",
ValueLabel = "100",
Color = SKColor.Parse("#90D585"),
},
new ChartEntry(600)
{
Label = "Week 4",
ValueLabel = "600",
Color = SKColor.Parse("#32a852"),
}
};

return new Chart[]
{
new BarChart() { Entries = entries, LabelTextSize = 60, LabelOrientation = Orientation.Horizontal },
new PointChart() { Entries = entries, LabelTextSize = 60, LabelOrientation = Orientation.Horizontal },
new LineChart() { Entries = entries, LabelTextSize = 60, LabelOrientation = Orientation.Horizontal },
new DonutChart() { Entries = entries, LabelTextSize = 60 },
new RadialGaugeChart() { Entries = entries, LabelTextSize = 60 },
new RadarChart() { Entries = entries, LabelTextSize = 60 },
new BarChart
{
Entries = entries,
LabelTextSize = 55,
LabelOrientation = Orientation.Horizontal,
Margin = 10
},
new PointChart
{
Entries = entries,
LabelTextSize = 55,
LabelOrientation = Orientation.Horizontal,
Margin = 10
},
new LineChart
{
Entries = entries,
LabelTextSize = 55,
LabelOrientation = Orientation.Horizontal,
Margin = 10
},
new DonutChart
{
Entries = entries,
LabelTextSize = 60
},
new RadialGaugeChart
{
Entries = entries,
LabelTextSize = 60
},
new RadarChart
{
Entries = entries,
LabelTextSize = 60
}
};
}

Expand Down
12 changes: 10 additions & 2 deletions Sources/Microcharts/Charts/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ public Chart()
/// <summary>
/// Get or set the direction the text should be facing.
/// </summary>
public TextDirection TextDirection { get; set; } = TextDirection.LTR;
public TextDirection TextDirection { get; set; } = TextDirection.Auto;

/// <summary>
/// Get or set label text spacing.
/// </summary>
public float LabelTextSpacing { get; set; } = 4.0f;

/// <summary>
/// Gets or sets a value indicating whether this <see cref="T:Microcharts.Chart"/> is animated when entries change.
Expand Down Expand Up @@ -149,6 +154,9 @@ public float LabelTextSize
set => Set(ref labelTextSize, value);
}

/// <summary>
/// Typeface for labels
/// </summary>
public SKTypeface Typeface
{
get => typeface;
Expand Down Expand Up @@ -359,7 +367,7 @@ protected void DrawCaptionElements(SKCanvas canvas, int width, int height, List<
captionX -= captionMargin;
}

canvas.DrawCaptionLabels(entry.Label, lblColor, TextDirection, entry.ValueLabel, valueColor, LabelTextSize, new SKPoint(captionX, y + (LabelTextSize / 2)), isLeft ? SKTextAlign.Left : SKTextAlign.Right, Typeface, out var labelBounds);
canvas.DrawCaptionLabels(entry.Label, lblColor, TextDirection, LabelTextSpacing, entry.ValueLabel, valueColor, LabelTextSize, new SKPoint(captionX, y + (LabelTextSize / 2)), isLeft ? SKTextAlign.Left : SKTextAlign.Right, Typeface, out var labelBounds);
labelBounds.Union(rect);

if (DrawDebugRectangles)
Expand Down
22 changes: 21 additions & 1 deletion Sources/Microcharts/Charts/PointChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,27 @@ protected void DrawLabels(SKCanvas canvas, string[] texts, SKPoint[] points, SKR
canvas.Translate(point.X - (bounds.Width / 2), y);
}

var rs = new RichString().Add(text, fontSize: LabelTextSize, textColor: colors[i], textDirection: TextDirection);
RichString rs;

if (Typeface != null)
{
rs = new RichString()
.FontFamily(Typeface.FamilyName)
.FontSize(LabelTextSize)
.LetterSpacing(LabelTextSpacing)
.TextColor(colors[i])
.TextDirection(TextDirection)
.Add(text);
}
else
{
rs = new RichString()
.FontSize(LabelTextSize)
.LetterSpacing(LabelTextSpacing)
.TextColor(colors[i])
.TextDirection(TextDirection)
.Add(text);
}

rs.Paint(canvas, new TextPaintOptions
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/Microcharts/Charts/RadarChart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override void DrawContent(SKCanvas canvas, int width, int height)
alignment = SKTextAlign.Right;
}

canvas.DrawCaptionLabels(entry.Label, entry.TextColor, TextDirection, entry.ValueLabel, entry.Color.WithAlpha((byte)(255 * AnimationProgress)), LabelTextSize, labelPoint, alignment, base.Typeface, out var _);
canvas.DrawCaptionLabels(entry.Label, entry.TextColor, TextDirection, LabelTextSpacing, entry.ValueLabel, entry.Color.WithAlpha((byte)(255 * AnimationProgress)), LabelTextSize, labelPoint, alignment, base.Typeface, out var _);
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions Sources/Microcharts/Extensions/CanvasExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microcharts
{
internal static class CanvasExtensions
{
public static void DrawCaptionLabels(this SKCanvas canvas, string label, SKColor labelColor, TextDirection textDirection, string value, SKColor valueColor, float textSize, SKPoint point, SKTextAlign horizontalAlignment, SKTypeface typeface, out SKRect totalBounds)
public static void DrawCaptionLabels(this SKCanvas canvas, string label, SKColor labelColor, TextDirection textDirection, float labelTextSpacing, string value, SKColor valueColor, float textSize, SKPoint point, SKTextAlign horizontalAlignment, SKTypeface typeface, out SKRect totalBounds)
{
var hasLabel = !string.IsNullOrEmpty(label);
var hasValueLabel = !string.IsNullOrEmpty(value);
Expand Down Expand Up @@ -39,7 +39,27 @@ public static void DrawCaptionLabels(this SKCanvas canvas, string label, SKColor

var y = point.Y - ((bounds.Top + bounds.Bottom) / 2) - space;

var rs = new RichString().Add(text, fontSize: textSize, textColor: labelColor, textDirection: textDirection);
RichString rs;

if (typeface != null)
{
rs = new RichString()
.FontFamily(typeface.FamilyName)
.FontSize(textSize)
.LetterSpacing(labelTextSpacing)
.TextColor(labelColor)
.TextDirection(textDirection)
.Add(text);
}
else
{
rs = new RichString()
.FontSize(textSize)
.LetterSpacing(labelTextSpacing)
.TextColor(labelColor)
.TextDirection(textDirection)
.Add(text);
}

rs.Paint(canvas, new SKPoint(point.X, y), new TextPaintOptions
{
Expand Down

0 comments on commit d6201ce

Please sign in to comment.