Skip to content

Commit

Permalink
Cursor Label
Browse files Browse the repository at this point in the history
Split cursor label string format by axis.
Add cursor label prefix and postfix.
  • Loading branch information
Code-Artist committed Dec 8, 2019
1 parent 789f138 commit 85d8083
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,5 @@ pip-log.txt

#TortoiseHg (Mercurial)
.hg*
NUGet Packages/MSChartExtension.1.4.1.nupkg
NUGet Packages/MSChartExtension.1.4.1.nupkg
.vs/
Binary file modified .vs/MSChartExtension/v16/Server/sqlite3/storage.ide
Binary file not shown.
Binary file modified .vs/MSChartExtension/v16/Server/sqlite3/storage.ide-shm
Binary file not shown.
Binary file modified .vs/MSChartExtension/v16/Server/sqlite3/storage.ide-wal
Binary file not shown.
75 changes: 73 additions & 2 deletions MSChartExtension/ChartOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,82 @@ public class ChartOption
/// </summary>
public bool SnapCursorToData { get; set; } = true;
/// <summary>
/// Define string format of cussors value. Default is "F4", 4 digits fixed decimal.
/// Define string format of cursor value. Default is "F4", 4 digits fixed decimal.
/// <see cref="double.ToString(string)"/>
/// Properties split into X1, X2, Y1, Y2 from Version 3.2.0 onwards. Writting to this properties update all 4 properties listed below.
/// Read from this properties return value from <see cref="CursorLabelStringFormatY1"/>
/// For cursor moved callback, <see cref="CursorLabelStringFormatX1"/> and <see cref="CursorLabelStringFormatY1"/> will be use to format label string.
/// <para><see cref="CursorLabelStringFormatX1"/></para>
/// <para><see cref="CursorLabelStringFormatX2"/></para>
/// <para><see cref="CursorLabelStringFormatY1"/></para>
/// <para><see cref="CursorLabelStringFormatY2"/></para>
/// </summary>
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
public string CursorLabelStringFormat { get; set; } = "F4";

public string CursorLabelStringFormat
{
get => CursorLabelStringFormatY1;
set
{
CursorLabelStringFormatX1 = CursorLabelStringFormatX2 = value;
CursorLabelStringFormatY1 = CursorLabelStringFormatY2 = value;
}
}
/// <summary>
/// Define string format for cursor value which use X primary axis.
/// </summary>
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
public string CursorLabelStringFormatX1 { get; set; } = "F4";
/// <summary>
/// Define string format for cursor value which use X secondary axis.
/// </summary>
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
public string CursorLabelStringFormatX2 { get; set; } = "F4";
/// <summary>
/// Define string format for cursor value which use Y primary axis.
/// </summary>
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
public string CursorLabelStringFormatY1 { get; set; } = "F4";
/// <summary>
/// Define string format for cursor value which use Y secondary axis.
/// </summary>
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
public string CursorLabelStringFormatY2 { get; set; } = "F4";

/// <summary>
/// Assign prefix on label string, default is empty
/// </summary>
public string CursorLabelPrefixX1 { get; set; }
/// <summary>
/// Assign prefix on label string, default is empty
/// </summary>
public string CursorLabelPrefixX2 { get; set; }
/// <summary>
/// Assign prefix on label string, default is empty
/// </summary>
public string CursorLabelPrefixY1 { get; set; }
/// <summary>
/// Assign prefix on label string, default is empty
/// </summary>
public string CursorLabelPrefixY2 { get; set; }

/// <summary>
/// Assign postfix on label string, default is empty
/// </summary>
public string CursorLabelPostfixX1 { get; set; }
/// <summary>
/// Assign postfix on label string, default is empty
/// </summary>
public string CursorLabelPostfixX2 { get; set; }
/// <summary>
/// Assign postfix on label string, default is empty
/// </summary>
public string CursorLabelPostfixY1 { get; set; }
/// <summary>
/// Assign postfix on label string, default is empty
/// </summary>
public string CursorLabelPostfixY2 { get; set; }

/// <summary>
/// Display cursor value on chart
/// </summary>
Expand Down
138 changes: 123 additions & 15 deletions MSChartExtension/MSChartExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,10 +660,11 @@ private static void MoveCursor(Chart chart, CursorDirection dir)
else if (dir == CursorDirection.Right) ptrCursor.DataIndex++;
Debug.WriteLine("New DataIndex = " + ptrCursor.DataIndex.ToString());

if (ptrCursor.DataIndex < 0) ptrCursor.DataIndex = 0;
if (ptrCursor.DataIndex <= 0) ptrCursor.DataIndex = 0;
else if (ptrCursor.DataIndex >= ptrSeries.Points.Count()) ptrCursor.DataIndex = ptrSeries.Points.Count() - 1;

DataPoint[] datas = ptrSeries.Points.OrderBy(x => x.XValue).ToArray();
if (datas.Length == 0) return; //Skip the rest of the code when series have no valid data.

ptrCursor.X = datas[ptrCursor.DataIndex].XValue;
ptrCursor.Y = datas[ptrCursor.DataIndex].YValues.First();
Expand Down Expand Up @@ -706,14 +707,40 @@ private static void MoveCursor(this Chart ptrChart, double newX, double newY)
DrawHorizontalLine(ptrChart, YStart, cursorColor, ptrChartArea.Name + "Cursor_1Y", lineWidth, cursorDashStyle, ptrChartArea, ptrSeries.YAxisType);
ptrChartData.Cursor1.X = XStart;
ptrChartData.Cursor1.Y = YStart;
ptrChartData.Cursor1.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor1.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor1.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
ptrChartData.Cursor1.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
ptrChartData.Cursor1.ChartArea = ptrChartArea;

if (ptrChartData.Option.ShowCursorValue)
{
string xPrefix, xPostfix, yPrefix, yPostfix;
if (ptrSeries.XAxisType == AxisType.Primary)
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
}
else
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
}

if (ptrSeries.YAxisType == AxisType.Primary)
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
}
else
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
}

//Add Cursor Value : X, Y
string cursorValue = ptrChartData.Cursor1.XFormattedString + "," + ptrChartData.Cursor1.YFormattedString;
string cursorValue = xPrefix + ptrChartData.Cursor1.XFormattedString + xPostfix + "," +
yPrefix + ptrChartData.Cursor1.YFormattedString + yPostfix;
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor1_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
}
ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor1.Clone() as ChartCursor);
Expand Down Expand Up @@ -746,14 +773,41 @@ private static void MoveCursor(this Chart ptrChart, double newX, double newY)
DrawHorizontalLine(ptrChart, YStart, cursorColor, ptrChartArea.Name + "Cursor_2Y", lineWidth, cursorDashStyle, ptrChartArea, ptrSeries.YAxisType);
ptrChartData.Cursor2.X = XStart;
ptrChartData.Cursor2.Y = YStart;
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
ptrChartData.Cursor2.ChartArea = ptrChartArea;


if (ptrChartData.Option.ShowCursorValue)
{
string xPrefix, xPostfix, yPrefix, yPostfix;
if (ptrSeries.XAxisType == AxisType.Primary)
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
}
else
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
}

if (ptrSeries.YAxisType == AxisType.Primary)
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
}
else
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
}

//Add Cursor Value : X, Y
string cursorValue = ptrChartData.Cursor2.XFormattedString + "," + ptrChartData.Cursor2.YFormattedString;
string cursorValue = xPrefix + ptrChartData.Cursor2.XFormattedString + xPostfix + "," +
yPrefix + ptrChartData.Cursor2.YFormattedString + yPostfix;
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor2_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
}
ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor2.Clone() as ChartCursor);
Expand Down Expand Up @@ -936,20 +990,47 @@ private static void ChartControl_MouseDown(object sender, MouseEventArgs e)
ptrChartData.Cursor1.X = XStart;
ptrChartData.Cursor1.Y = YStart;
ptrChartData.Cursor1.DataIndex = dataIndex;
ptrChartData.Cursor1.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor1.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor1.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
ptrChartData.Cursor1.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
ptrChartData.Cursor1.ChartArea = ptrChartArea;

if (ptrChartData.Option.ShowCursorValue)
{
string xPrefix, xPostfix, yPrefix, yPostfix;
if (ptrSeries.XAxisType == AxisType.Primary)
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
}
else
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
}

if (ptrSeries.YAxisType == AxisType.Primary)
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
}
else
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
}

//Add Cursor Value : X, Y
string cursorValue = ptrChartData.Cursor1.XFormattedString + "," + ptrChartData.Cursor1.YFormattedString;
string cursorValue = xPrefix + ptrChartData.Cursor1.XFormattedString + xPostfix + "," +
yPrefix + ptrChartData.Cursor1.YFormattedString + yPostfix;
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor1_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
}

ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor1.Clone() as ChartCursor);
}
}
ptrChart.Focus();
}
else if (ptrChartData.ToolState == MSChartExtensionToolState.Select2)
{
Expand Down Expand Up @@ -980,20 +1061,47 @@ private static void ChartControl_MouseDown(object sender, MouseEventArgs e)
ptrChartData.Cursor2.X = XStart;
ptrChartData.Cursor2.Y = YStart;
ptrChartData.Cursor2.DataIndex = dataIndex;
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
ptrChartData.Cursor2.ChartArea = ptrChartArea;

if (ptrChartData.Option.ShowCursorValue)
{
string xPrefix, xPostfix, yPrefix, yPostfix;
if (ptrSeries.XAxisType == AxisType.Primary)
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
}
else
{
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
}

if (ptrSeries.YAxisType == AxisType.Primary)
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
}
else
{
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
}

//Add Cursor Value : X, Y
string cursorValue = ptrChartData.Cursor2.XFormattedString + "," + ptrChartData.Cursor2.YFormattedString;
string cursorValue = xPrefix + ptrChartData.Cursor2.XFormattedString + xPostfix + "," +
yPrefix + ptrChartData.Cursor2.YFormattedString + yPostfix;
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor2_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
}

ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor2.Clone() as ChartCursor);
}
}
ptrChart.Focus();
}
}

Expand Down Expand Up @@ -1082,8 +1190,8 @@ private static void ChartControl_MouseMove(object sender, MouseEventArgs e)
X = selX,
Y = selY,
ChartArea = ptrChartArea,
XFormattedString = FormatCursorValue(selX, xValueType, ptrChartData.Option.CursorLabelStringFormat),
YFormattedString = FormatCursorValue(selY, yValueType, ptrChartData.Option.CursorLabelStringFormat)
XFormattedString = FormatCursorValue(selX, xValueType, ptrChartData.Option.CursorLabelStringFormatX1),
YFormattedString = FormatCursorValue(selY, yValueType, ptrChartData.Option.CursorLabelStringFormatY1)
});
}
catch (Exception)
Expand Down
Loading

0 comments on commit 85d8083

Please sign in to comment.