Logarithmic Scale
This web site builds the control from code behind but you could also grab it from the toolbox, this sample also uses a ViewModel to populate the properties of the control(s) in this sample.
View model
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
namespace ViewModelsSamples.Axes.Logarithmic;
public partial class ViewModel : ObservableObject
{
// base 10 log, change the base if you require it.
// or use any custom scale the logic is the same.
private static readonly int s_logBase = 10;
public ISeries[] Series { get; set; } =
{
new LineSeries<LogarithmicPoint>
{
// for the x coordinate, we use the X property
// and for the Y coordinate, we will map it to the logarithm of the value
Mapping = (logPoint, index) => new(logPoint.X, Math.Log(logPoint.Y, s_logBase)),
Values = new LogarithmicPoint[]
{
new() { X = 1, Y = 1 },
new() { X = 2, Y = 10 },
new() { X = 3, Y = 100 },
new() { X = 4, Y = 1000 },
new() { X = 5, Y = 10000 },
new() { X = 6, Y = 100000 },
new() { X = 7, Y = 1000000 },
new() { X = 8, Y = 10000000 }
}
}
};
public Axis[] YAxes { get; set; } =
{
new LogaritmicAxis(s_logBase)
{
SeparatorsPaint = new SolidColorPaint
{
Color = SKColors.Black.WithAlpha(100),
StrokeThickness = 1,
},
SubseparatorsPaint = new SolidColorPaint
{
Color = SKColors.Black.WithAlpha(50),
StrokeThickness = 0.5f
},
SubseparatorsCount = 9,
}
};
}
LogarithmicPoint.cs
namespace ViewModelsSamples.Axes.Logarithmic;
public class LogarithmicPoint
{
public double X { get; set; }
public double Y { get; set; }
}
Form code behind
using System.Windows.Forms;
using LiveChartsCore.SkiaSharpView.WinForms;
using ViewModelsSamples.Axes.Logarithmic;
namespace WinFormsSample.Axes.Logarithmic;
public partial class View : UserControl
{
private readonly CartesianChart cartesianChart;
public View()
{
InitializeComponent();
Size = new System.Drawing.Size(50, 50);
var viewModel = new ViewModel();
cartesianChart = new CartesianChart
{
Series = viewModel.Series,
YAxes = viewModel.YAxes,
// out of livecharts properties...
Location = new System.Drawing.Point(0, 0),
Size = new System.Drawing.Size(50, 50),
Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom
};
Controls.Add(cartesianChart);
}
}