Chinease, Japanese, Arabic, Russian
View model
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
namespace ViewModelsSamples.Axes.LabelsFormat2;
public partial class ViewModel : ObservableObject
{
public ISeries[] Series { get; set; } =
{
new ColumnSeries<double> { Values = new double[] { 426, 583, 104 } },
new LineSeries<double> { Values = new double[] { 200, 558, 458 }, Fill = null }
};
public Axis[] XAxes { get; set; } =
{
new Axis
{
Name = "Salesman/woman",
Labels = new string[] { "王", "赵", "张" },
LabelsPaint = new SolidColorPaint
{
Color = SKColors.Black,
// you need to enable the Chinese characters for SkiaSharp
// use the SKFontManager.Default.MatchCharacter() SkiaSharp function.
SKTypeface = SKFontManager.Default.MatchCharacter('汉') // 汉语 // mark
// SKTypeface = SKFontManager.Default.MatchCharacter('أ'), // Arab
// SKTypeface = SKFontManager.Default.MatchCharacter('あ'), // Japanese
// SKTypeface = SKFontManager.Default.MatchCharacter('Ж'), // Russian
// You can also register a default global SKTypeface // mark
// this will load the font in any Paint when the SKTypeface property is null. // mark
// for more info see: ToDo: Add link!!! // mark
}
}
};
public Axis[] YAxes { get; set; } =
{
new Axis
{
Name = "Sales amount",
NamePadding = new LiveChartsCore.Drawing.Padding(0, 15),
Labeler = Labelers.Currency,
LabelsPaint = new SolidColorPaint
{
Color = SKColors.Blue,
FontFamily = "Times New Roman",
SKFontStyle = new SKFontStyle(SKFontStyleWeight.ExtraBold, SKFontStyleWidth.Normal, SKFontStyleSlant.Italic)
},
}
};
}
HTML
@page "/Axes/LabelsFormat2"
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore;
@using LiveChartsCore.SkiaSharpView;
@using SkiaSharp;
@using ViewModelsSamples.Axes.LabelsFormat2
@inject HttpClient Http;
<CartesianChart
Series="ViewModel.Series"
XAxes="ViewModel.XAxes"
YAxes="ViewModel.YAxes">
</CartesianChart>
@code {
public ViewModel ViewModel { get; set; } = new();
protected async override Task OnInitializedAsync()
{
// On .net7, SkiaSharp 2.88.3 and SkiaSharp.HarfBuzz 2.88.3 the MatchCharacter does not load the font.
// The MatchCharacter function loads a font from the system,
// it seems that on WASM we must load the font manually.
// In this case, the font was added to the wwwroot folder, then we load it using the HTTPClient
// This is only necessary once, ideally when the app starts.
using var stream = await Http.GetStreamAsync("NotoSansSC-Regular.otf");
var typeface = SKTypeface.FromStream(stream);
LiveCharts.Configure(config => // mark
config // mark
.UseDefaults() // mark
.WithGlobalSKTypeface(typeface)); // mark
}
}
Articles you might also find useful: