Reputation: 239
[EDIT]
After a lot of trial and error, I realized a version of my tooltip code could scroll while the rest of the codes are unchanged. The only difference between the two tooltip usage is that the pos.x value is shifted, instead of being exactly where the mouse is.
Which means instead of
/*X-axis Tooltip*/
tooltip.Show(Math.Truncate(xValue * 1000) / 1000 + unit_Converter(), this.chart1, pos.X, pos.Y - 15);
I did this
/*X-axis Tooltip*/
tooltip.Show(Math.Truncate(xValue * 1000) / 1000 + unit_Converter(), this.chart1, pos.X - 70, pos.Y - 15);
That was all the difference. Now I can click and drag the X-axis scroll bar around. I suppose the reason why I always could scroll my Y-axis is it was shifted by 15 to begin with.
So if you set your tooltip position exactly where your mouse position is, then apparently you will be clicking on the tooltip itself, instead of the scrollbar when you attempt to scroll.
Answer provided by Josh W is equally valid, because using just "this" instead of "this.chart" automatically shifts the tooltip a little bit for some reason. thanks for the help!
[Original Question]
I have a chart that has Y-axis and X-axis scroll bar. For a while both my scrollbars worked. As my code grow, the X-axis scrollbar now won't move, while the Y-axis scrollbar moves just fine. I am puzzled. Any help would be appreciated.
I have referenced other answers such as
c# chart control, vertical scrolling problems with zoom ["Stuck Scroll bar"]
But X-axis scrollbar still stuck....
[EDIT]: I realized that my tooltip on mouse move code is causing this. If I disable my call to my mouse move code, the X-axis scrollbar would be functional again. But how do I make it so that both can function? I don't really know how the crossair tooltip would disable scrolling on the X-axis only, but not the Y-axis...
void chart1_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.Location;
_point.X = e.Location.X;
_point.Y = e.Location.Y;
try
{
if ((chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X) >= 0) && (chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X) <= max))
{
//Crossair
chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(_point, true);
//Tooltips
double xValue = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
/*X-axis Tooltip*/
tooltip.Show(Math.Truncate(xValue * 1000) / 1000 + unit_Converter(), this.chart1, pos.X, pos.Y - 15);
}
}
catch (Exception exception)
{
}
}
My code to set series settings:
var series = chart1.ChartAreas[chart1.Series[iname].ChartArea];
//Line thickness
chart1.Series[iname].BorderWidth = 2;
series.AxisX.Minimum = 0;
series.AxisX.Maximum = max;
series.AxisY.Minimum = 0;
series.AxisY.Maximum = checkedListBox1.CheckedItems.Count * 3 - 2;
series.AxisX.MajorGrid.Interval = time_of_cycle;
series.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.DashDotDot;
series.AxisY.MajorGrid.Interval = 2;
series.CursorX.Interval = 0;
series.CursorY.Interval = 0;
series.AxisX.ScaleView.SmallScrollSize = time_of_cycle /100 ;
series.AxisY.ScaleView.SmallScrollSize = 1;
//Disables Y axis lable
series.AxisY.LabelStyle.Enabled = false;
series.AxisX.LabelStyle.ForeColor = Color.White;
series.AxisY.LabelStyle.ForeColor = Color.White;
series.AxisX.LabelStyle.Format = label_Style_Converter();
series.AxisX.LabelStyle.Interval = time_of_cycle * 2;
series.AxisX.MajorGrid.LineColor = Color.DimGray;
series.AxisY.MajorGrid.LineColor = Color.DimGray;
series.AxisX.ScrollBar.BackColor = Color.LightGray;
series.AxisY.ScrollBar.BackColor = Color.LightGray;
series.AxisX.ScrollBar.ButtonColor = Color.LightGray;
series.AxisY.ScrollBar.ButtonColor = Color.LightGray;
series.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
series.AxisY.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
series.AxisX.ScrollBar.Enabled = true;
series.AxisY.ScrollBar.Enabled = true;
series.AxisX.ScrollBar.IsPositionedInside = false;
series.AxisY.ScrollBar.IsPositionedInside = false;
series.AxisX.IsMarginVisible = true;
series.AxisY.IsMarginVisible = false;
series.AxisX.Name = "µs";
series.AxisX.ScaleView.Size = max - time_of_cycle / 100;
series.AxisY.ScaleView.Size = (checkedListBox1.CheckedItems.Count * 3 - 2) + 1 ;
series.BackColor = Color.Black;
//crosshair
var cursor_Y = chart1.ChartAreas["ChartArea1"].CursorY;
var cursor_X = chart1.ChartAreas["ChartArea1"].CursorX;
cursor_Y.LineWidth = 1;
cursor_Y.LineDashStyle = ChartDashStyle.Solid;
cursor_Y.LineColor = Color.DarkRed;
cursor_Y.SelectionColor = Color.LightGray;
cursor_X.LineWidth = 1;
cursor_X.LineDashStyle = ChartDashStyle.Solid;
cursor_X.LineColor = Color.DarkRed;
chart1.MouseMove += new MouseEventHandler(chart1_MouseMove);
Upvotes: 2
Views: 2477
Reputation: 1171
At first I thought maybe your call to .Show()
was blocking the GUI thread, but some quick and dirty code didn't seem to have that issue. One odd thing I did notice with the tooltip though is that when you have something like your Chart subscribed to the MouseMove event, and your tool-tip is given a 'this.chart1' reference instead of just this
, then if the tooltip is under your mouse, it continues to fire the event.
That is, even if the tooltip moves off the chart for example.
Here was some test code I just played with using a panel instead of a chart.
private int Counter = 0;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Console.WriteLine(string.Format("X{0}, Y{1}\t Count = {2}", e.X, e.Y, Counter));
Counter++;
toolTip1.Show(
string.Format("X{0}, Y{1}\t Count = {2}", e.X, e.Y, Counter),
this.panel1,
e.X - 75,
e.Y -5);
}
First you should also get rid of that try/catch statement as you aren't doing anything with the error... either handle errors, or let them bubble up to crash something (so that you can 'fix' it). What you have there now might throw an error, but you'd never know, because you are catching it and ignoring it.
Second, the MouseMove event happens a LOT... perhaps use the MouseHover for showing the tooltip?
Other than that... it does not appear to be where your code is breaking, though it could be inside the unit_Converter()
call or even on the math function... your try/catch may be hiding an exception on that line.
Upvotes: 1