Reputation: 192
There is not much documentation on Mscharts in general for C# in Visual Studio 2008. I have researched alot for solutions but nothing seems to work! When the user hovers over the line at a data point, the value of that particular point should be displayed and when lost focus should disappear. I understand the best way to do this, I think, is to add Tooltips to my chart, but my problem is actually getting the value from a data point. The chart looks like this at the moment with just hard-coded data for testing but will be getting round to importing the data from a file:
_Note: I do realise the lines are not realistic - not for my project anyway. It's just for testing. When I have completed the project, there will be several data points._
var sv = new System.Windows.Forms.DataVisualization.Charting.Series("1");
sv.ChartType = SeriesChartType.Line;
sv.Points.Add(new DataPoint(201, 10));
sv.Points.Add(new DataPoint(3, 11));
sv.YAxisType = AxisType.Primary;
chart1.Series.Add(sv);
var ov = new System.Windows.Forms.DataVisualization.Charting.Series("2");
ov.ChartType = SeriesChartType.Line;
ov.Points.Add(new DataPoint(201, 25));
ov.Points.Add(new DataPoint(8, 3));
ov.YAxisType = AxisType.Secondary;
chart1.Series.Add(ov);
var oc = new System.Windows.Forms.DataVisualization.Charting.Series("3");
oc.ChartType = SeriesChartType.Line;
oc.YAxisType = AxisType.Secondary;
oc.Points.Add(new DataPoint(1, 5));
oc.Points.Add(new DataPoint(190, 90));
oc.Points.Add(new DataPoint(200, 90));
chart1.Series.Add(oc);
var sn = new System.Windows.Forms.DataVisualization.Charting.Series("4");
sn.ChartType = SeriesChartType.Line;
sn.Points.Add(new DataPoint(9, 30));
sn.Points.Add(new DataPoint(150, 28));
sn.XAxisType = AxisType.Primary;
chart1.Series.Add(sn);
Chart_Line.ChartAreas[0].AxisX.LabelStyle.Font = Chart_Line.ChartAreas[0].AxisY.LabelStyle.Font = Chart_Line.ChartAreas[0].AxisY2.LabelStyle.Font = new System.Drawing.Font("Calibri Light", 8);
Chart_Line.ChartAreas[0].AxisX.Minimum = 0;
Chart_Line.ChartAreas[0].AxisX.Maximum = 201;
Chart_Line.ChartAreas[0].AxisX.Interval = 15;
Chart_Line.ChartAreas[0].AxisY.Maximum = 140;
Chart_Line.ChartAreas[0].AxisY.Interval = 20;
Chart_Line.ChartAreas[0].AxisY.Minimum = 0;
Chart_Line.ChartAreas[0].AxisY2.Maximum = 30;
Chart_Line.ChartAreas[0].AxisY2.Interval = 5;
Chart_Line.ChartAreas[0].AxisY2.Minimum = 0;
Chart_Line.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
Chart_Line.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
Chart_Line.ChartAreas[0].AxisY2.MajorGrid.Enabled = false;
private void Chart1_MouseMove(object sender, MouseEventArgs e)
{
HitTestResult result = chart1.HitTest(e.X, e.Y);
System.Drawing.Point p = new System.Drawing.Point(e.X, e.Y);
chart1.ChartAreas[0].CursorX.Interval = 0;
chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(p, true);
chart1.ChartAreas[0].CursorX.LineColor = Color.Crimson;
chart1.ChartAreas[0].CursorX.LineWidth = 1;
chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(p, true);
chart1.ChartAreas[0].CursorY.LineColor = Color.Crimson;
chart1.ChartAreas[0].CursorY.LineWidth = 1;
}
I'm not sure how to go about automatically creating tooltips for datapoints, activating when mouse is hovering over and then removing it. The cursor lines work fine!
Upvotes: 1
Views: 7514
Reputation: 127
The one marked as the solution is correct but is not so clear and I would like to add details.
When you populate the graph, after that, you should construct the String info
variable that has all the pretty information an Analyst like me will value.
What are those symbols like VAL{N2}
?
Those symbols have a different meaning which been explained by Microsoft.
string info = "Value: " + "#VAL{N2}" + "\n" + "Average: " + "#AVG{N2}" + "\n" + "Minimum: " + "#MIN{N2}" + "\n" + "Maximum: " + "#MAX{N2}" + "\n"
+ "First: " + "#FIRST{N2}" + "\n" + "Last: " + "#LAST{N2}";
Grafico.Series["valores"].ToolTip = "Detalles..." + "\n" + info;
Grafico.Series["valores2"].ToolTip = "Detalles..." + "\n" + info;
Where valores
and valores2
are different graphs.
Then add a procedure that give instructions about the mouse movement over the graph and it should work.
private void Grafico_MouseMove(object sender, MouseEventArgs e)
{
HitTestResult result = Grafico.HitTest(e.X, e.Y);
System.Drawing.Point p = new System.Drawing.Point(e.X, e.Y);
Grafico.ChartAreas["valores"].CursorX.Interval = 0;
Grafico.ChartAreas["valores2"].CursorX.SetCursorPixelPosition(p, true);
}
Upvotes: 0
Reputation: 192
Easier than I first thought :) Populating the graph was pretty complex, that is most likely why I got so confused!
string info = "Value: " + "#VAL{N2}" + "\n" + "Average: " + "#AVG{N2}" + "\n" + "Minimum: " + "#MIN{N2}" + "\n" + "Maximum: " + "#MAX{N2}" + "\n"
+ "First: " + "#FIRST{N2}" + "\n" + "Last: " + "#LAST{N2}";
Chart1.Series[0].ToolTip = "Series name..." + "\n" + info;
Chart1.Series[1].ToolTip = "Series name2..." + "\n" + info;
Chart1.Series[2].ToolTip = "Series name3..." + "\n" + info;
private void Chart1_MouseMove(object sender, MouseEventArgs e)
{
HitTestResult result = Chart1.HitTest(e.X, e.Y);
System.Drawing.Point p = new System.Drawing.Point(e.X, e.Y);
Chart1.ChartAreas[0].CursorX.Interval = 0;
Chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(p, true);
Chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(p, true);
}
Upvotes: 1
Reputation: 2369
To provide the answer for your question.
chart1.Series[0].Points[n].ToolTip = "#VALX";
Tried and tested with a line chart.
Upvotes: 0