Skip to content

Commit 85d8083

Browse files
committed
Cursor Label
Split cursor label string format by axis. Add cursor label prefix and postfix.
1 parent 789f138 commit 85d8083

File tree

9 files changed

+208
-20
lines changed

9 files changed

+208
-20
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,5 @@ pip-log.txt
216216

217217
#TortoiseHg (Mercurial)
218218
.hg*
219-
NUGet Packages/MSChartExtension.1.4.1.nupkg
219+
NUGet Packages/MSChartExtension.1.4.1.nupkg
220+
.vs/
Binary file not shown.
Binary file not shown.
Binary file not shown.

MSChartExtension/ChartOption.cs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,82 @@ public class ChartOption
5050
/// </summary>
5151
public bool SnapCursorToData { get; set; } = true;
5252
/// <summary>
53-
/// Define string format of cussors value. Default is "F4", 4 digits fixed decimal.
53+
/// Define string format of cursor value. Default is "F4", 4 digits fixed decimal.
5454
/// <see cref="double.ToString(string)"/>
55+
/// Properties split into X1, X2, Y1, Y2 from Version 3.2.0 onwards. Writting to this properties update all 4 properties listed below.
56+
/// Read from this properties return value from <see cref="CursorLabelStringFormatY1"/>
57+
/// For cursor moved callback, <see cref="CursorLabelStringFormatX1"/> and <see cref="CursorLabelStringFormatY1"/> will be use to format label string.
58+
/// <para><see cref="CursorLabelStringFormatX1"/></para>
59+
/// <para><see cref="CursorLabelStringFormatX2"/></para>
60+
/// <para><see cref="CursorLabelStringFormatY1"/></para>
61+
/// <para><see cref="CursorLabelStringFormatY2"/></para>
5562
/// </summary>
5663
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
57-
public string CursorLabelStringFormat { get; set; } = "F4";
64+
65+
public string CursorLabelStringFormat
66+
{
67+
get => CursorLabelStringFormatY1;
68+
set
69+
{
70+
CursorLabelStringFormatX1 = CursorLabelStringFormatX2 = value;
71+
CursorLabelStringFormatY1 = CursorLabelStringFormatY2 = value;
72+
}
73+
}
74+
/// <summary>
75+
/// Define string format for cursor value which use X primary axis.
76+
/// </summary>
77+
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
78+
public string CursorLabelStringFormatX1 { get; set; } = "F4";
79+
/// <summary>
80+
/// Define string format for cursor value which use X secondary axis.
81+
/// </summary>
82+
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
83+
public string CursorLabelStringFormatX2 { get; set; } = "F4";
84+
/// <summary>
85+
/// Define string format for cursor value which use Y primary axis.
86+
/// </summary>
87+
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
88+
public string CursorLabelStringFormatY1 { get; set; } = "F4";
89+
/// <summary>
90+
/// Define string format for cursor value which use Y secondary axis.
91+
/// </summary>
92+
/// <remarks>More details regarding string format at https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings </remarks>
93+
public string CursorLabelStringFormatY2 { get; set; } = "F4";
94+
95+
/// <summary>
96+
/// Assign prefix on label string, default is empty
97+
/// </summary>
98+
public string CursorLabelPrefixX1 { get; set; }
99+
/// <summary>
100+
/// Assign prefix on label string, default is empty
101+
/// </summary>
102+
public string CursorLabelPrefixX2 { get; set; }
103+
/// <summary>
104+
/// Assign prefix on label string, default is empty
105+
/// </summary>
106+
public string CursorLabelPrefixY1 { get; set; }
107+
/// <summary>
108+
/// Assign prefix on label string, default is empty
109+
/// </summary>
110+
public string CursorLabelPrefixY2 { get; set; }
111+
112+
/// <summary>
113+
/// Assign postfix on label string, default is empty
114+
/// </summary>
115+
public string CursorLabelPostfixX1 { get; set; }
116+
/// <summary>
117+
/// Assign postfix on label string, default is empty
118+
/// </summary>
119+
public string CursorLabelPostfixX2 { get; set; }
120+
/// <summary>
121+
/// Assign postfix on label string, default is empty
122+
/// </summary>
123+
public string CursorLabelPostfixY1 { get; set; }
124+
/// <summary>
125+
/// Assign postfix on label string, default is empty
126+
/// </summary>
127+
public string CursorLabelPostfixY2 { get; set; }
128+
58129
/// <summary>
59130
/// Display cursor value on chart
60131
/// </summary>

MSChartExtension/MSChartExtension.cs

Lines changed: 123 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -660,10 +660,11 @@ private static void MoveCursor(Chart chart, CursorDirection dir)
660660
else if (dir == CursorDirection.Right) ptrCursor.DataIndex++;
661661
Debug.WriteLine("New DataIndex = " + ptrCursor.DataIndex.ToString());
662662

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

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

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

713716
if (ptrChartData.Option.ShowCursorValue)
714717
{
718+
string xPrefix, xPostfix, yPrefix, yPostfix;
719+
if (ptrSeries.XAxisType == AxisType.Primary)
720+
{
721+
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
722+
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
723+
}
724+
else
725+
{
726+
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
727+
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
728+
}
729+
730+
if (ptrSeries.YAxisType == AxisType.Primary)
731+
{
732+
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
733+
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
734+
}
735+
else
736+
{
737+
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
738+
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
739+
}
740+
715741
//Add Cursor Value : X, Y
716-
string cursorValue = ptrChartData.Cursor1.XFormattedString + "," + ptrChartData.Cursor1.YFormattedString;
742+
string cursorValue = xPrefix + ptrChartData.Cursor1.XFormattedString + xPostfix + "," +
743+
yPrefix + ptrChartData.Cursor1.YFormattedString + yPostfix;
717744
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor1_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
718745
}
719746
ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor1.Clone() as ChartCursor);
@@ -746,14 +773,41 @@ private static void MoveCursor(this Chart ptrChart, double newX, double newY)
746773
DrawHorizontalLine(ptrChart, YStart, cursorColor, ptrChartArea.Name + "Cursor_2Y", lineWidth, cursorDashStyle, ptrChartArea, ptrSeries.YAxisType);
747774
ptrChartData.Cursor2.X = XStart;
748775
ptrChartData.Cursor2.Y = YStart;
749-
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
750-
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
776+
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
777+
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
778+
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
779+
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
751780
ptrChartData.Cursor2.ChartArea = ptrChartArea;
752781

782+
753783
if (ptrChartData.Option.ShowCursorValue)
754784
{
785+
string xPrefix, xPostfix, yPrefix, yPostfix;
786+
if (ptrSeries.XAxisType == AxisType.Primary)
787+
{
788+
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
789+
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
790+
}
791+
else
792+
{
793+
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
794+
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
795+
}
796+
797+
if (ptrSeries.YAxisType == AxisType.Primary)
798+
{
799+
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
800+
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
801+
}
802+
else
803+
{
804+
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
805+
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
806+
}
807+
755808
//Add Cursor Value : X, Y
756-
string cursorValue = ptrChartData.Cursor2.XFormattedString + "," + ptrChartData.Cursor2.YFormattedString;
809+
string cursorValue = xPrefix + ptrChartData.Cursor2.XFormattedString + xPostfix + "," +
810+
yPrefix + ptrChartData.Cursor2.YFormattedString + yPostfix;
757811
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor2_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
758812
}
759813
ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor2.Clone() as ChartCursor);
@@ -936,20 +990,47 @@ private static void ChartControl_MouseDown(object sender, MouseEventArgs e)
936990
ptrChartData.Cursor1.X = XStart;
937991
ptrChartData.Cursor1.Y = YStart;
938992
ptrChartData.Cursor1.DataIndex = dataIndex;
939-
ptrChartData.Cursor1.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
940-
ptrChartData.Cursor1.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
993+
ptrChartData.Cursor1.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
994+
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
995+
ptrChartData.Cursor1.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
996+
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
941997
ptrChartData.Cursor1.ChartArea = ptrChartArea;
942998

943999
if (ptrChartData.Option.ShowCursorValue)
9441000
{
1001+
string xPrefix, xPostfix, yPrefix, yPostfix;
1002+
if (ptrSeries.XAxisType == AxisType.Primary)
1003+
{
1004+
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
1005+
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
1006+
}
1007+
else
1008+
{
1009+
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
1010+
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
1011+
}
1012+
1013+
if (ptrSeries.YAxisType == AxisType.Primary)
1014+
{
1015+
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
1016+
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
1017+
}
1018+
else
1019+
{
1020+
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
1021+
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
1022+
}
1023+
9451024
//Add Cursor Value : X, Y
946-
string cursorValue = ptrChartData.Cursor1.XFormattedString + "," + ptrChartData.Cursor1.YFormattedString;
1025+
string cursorValue = xPrefix + ptrChartData.Cursor1.XFormattedString + xPostfix + "," +
1026+
yPrefix + ptrChartData.Cursor1.YFormattedString + yPostfix;
9471027
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor1_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
9481028
}
9491029

9501030
ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor1.Clone() as ChartCursor);
9511031
}
9521032
}
1033+
ptrChart.Focus();
9531034
}
9541035
else if (ptrChartData.ToolState == MSChartExtensionToolState.Select2)
9551036
{
@@ -980,20 +1061,47 @@ private static void ChartControl_MouseDown(object sender, MouseEventArgs e)
9801061
ptrChartData.Cursor2.X = XStart;
9811062
ptrChartData.Cursor2.Y = YStart;
9821063
ptrChartData.Cursor2.DataIndex = dataIndex;
983-
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType, ptrChartData.Option.CursorLabelStringFormat);
984-
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType, ptrChartData.Option.CursorLabelStringFormat);
1064+
ptrChartData.Cursor2.XFormattedString = FormatCursorValue(XStart, ptrSeries.XValueType,
1065+
(ptrSeries.XAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatX1 : ptrChartData.Option.CursorLabelStringFormatX2));
1066+
ptrChartData.Cursor2.YFormattedString = FormatCursorValue(YStart, ptrSeries.YValueType,
1067+
(ptrSeries.YAxisType == AxisType.Primary ? ptrChartData.Option.CursorLabelStringFormatY1 : ptrChartData.Option.CursorLabelStringFormatY2));
9851068
ptrChartData.Cursor2.ChartArea = ptrChartArea;
9861069

9871070
if (ptrChartData.Option.ShowCursorValue)
9881071
{
1072+
string xPrefix, xPostfix, yPrefix, yPostfix;
1073+
if (ptrSeries.XAxisType == AxisType.Primary)
1074+
{
1075+
xPrefix = ptrChartData.Option.CursorLabelPrefixX1;
1076+
xPostfix = ptrChartData.Option.CursorLabelPostfixX1;
1077+
}
1078+
else
1079+
{
1080+
xPrefix = ptrChartData.Option.CursorLabelPrefixX2;
1081+
xPostfix = ptrChartData.Option.CursorLabelPostfixX2;
1082+
}
1083+
1084+
if (ptrSeries.YAxisType == AxisType.Primary)
1085+
{
1086+
yPrefix = ptrChartData.Option.CursorLabelPrefixY1;
1087+
yPostfix = ptrChartData.Option.CursorLabelPostfixY1;
1088+
}
1089+
else
1090+
{
1091+
yPrefix = ptrChartData.Option.CursorLabelPrefixY2;
1092+
yPostfix = ptrChartData.Option.CursorLabelPostfixY2;
1093+
}
1094+
9891095
//Add Cursor Value : X, Y
990-
string cursorValue = ptrChartData.Cursor2.XFormattedString + "," + ptrChartData.Cursor2.YFormattedString;
1096+
string cursorValue = xPrefix + ptrChartData.Cursor2.XFormattedString + xPostfix + "," +
1097+
yPrefix + ptrChartData.Cursor2.YFormattedString + yPostfix;
9911098
AddText(ptrChart, cursorValue, XStart, YStart, cursorColor, ptrChartArea.Name + "cursor2_Label", TextStyle.Default, ptrChartArea, ptrSeries.XAxisType, ptrSeries.YAxisType);
9921099
}
9931100

9941101
ptrChartData.PositionChangedCallback?.Invoke(ptrChart, ptrChartData.Cursor2.Clone() as ChartCursor);
9951102
}
9961103
}
1104+
ptrChart.Focus();
9971105
}
9981106
}
9991107

@@ -1082,8 +1190,8 @@ private static void ChartControl_MouseMove(object sender, MouseEventArgs e)
10821190
X = selX,
10831191
Y = selY,
10841192
ChartArea = ptrChartArea,
1085-
XFormattedString = FormatCursorValue(selX, xValueType, ptrChartData.Option.CursorLabelStringFormat),
1086-
YFormattedString = FormatCursorValue(selY, yValueType, ptrChartData.Option.CursorLabelStringFormat)
1193+
XFormattedString = FormatCursorValue(selX, xValueType, ptrChartData.Option.CursorLabelStringFormatX1),
1194+
YFormattedString = FormatCursorValue(selY, yValueType, ptrChartData.Option.CursorLabelStringFormatY1)
10871195
});
10881196
}
10891197
catch (Exception)

0 commit comments

Comments
 (0)