How to create Axis labels with scientific notation
Scientific notation is a way of expressing numbers, which commonly used by scientists, mathematicians, and engineers. This base 10 notation, where nonzero numbers are written in the form
Which could be read as, m times ten raised to the power of n, where n is an integer, and the coefficient m is a nonzero real number.
Officially superscripts formatting is not supported in C# / .NET. However, there is some workaround. Basically one needs to use Unicode characters to show exponents like the ones described below.
This approach is centered around method for converting number to scientific notation (with superscript). Here is the example of such method
/// <summary>
/// Converted number to scientific notation
/// </summary>
/// <param name="val">Number to be converted to scientific notation</param>
/// <returns></returns>
public string FormatAs10Power(double val)
{
string SuperscriptDigits = "\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
string expstr = string.Format("{0:0.#E0}", val);
var numparts = expstr.Split('E');
char[] powerchars = numparts[1].ToArray();
for (int i = 0; i < powerchars.Length; i++)
{
powerchars[i] = (powerchars[i] == '-') ? '\u207b' : SuperscriptDigits[powerchars[i] - '0'];
}
numparts[1] = new string(powerchars);
return string.Join(" x 10", numparts);
}
Once user has way to convert number to right string, then there is no limits in LightningChart how it could be used. For axis labels most relevant would CustomTicks and FormatValueLabel event. CustomTicks allow to customize position, style and label text of each axis tick. Therefore, above scientific notation string could be used as LabelText.
axis.FormatValueLabel event handler allows formatting axis value labels as well. And following examples will demonstrates its usage. For experimenting with this approach you can extract any example from our Demo and add few lines there. ExampleLogAxesXY could be one such example.
First, subscribe to event
yAxis.FormatValueLabel += Axis_FormatValueLabel;
axisX.FormatValueLabel += Axis_FormatValueLabel;
Second, create event handler like this
private string Axis_FormatValueLabel(object sender, FormatValueLabelEventArgs e)
{
return FormatAs10Power(e.Value);
}
And that's it, all the labels now will have scientific notation
Axes labels formatted as scientific notation