Show tooltip in LineSeries WinForms Chart?

天大地大妈咪最大 提交于 2020-01-04 04:33:41

问题


I am working on a Dashboard System where i am using Line Chart in WinForms. I need to show the tooptip on each line. I have tried this

var series = new Series
                    {
                        Name = chartPoint.SetName,
                        Color = chartPoint.ChartColor,
                        ChartType = SeriesChartType.Line,
                        BorderDashStyle = chartPoint.ChartDashStyle,
                        BorderWidth = chartPoint.BorderWidth,
                        IsVisibleInLegend = !chartPoint.HideLegend,
                        ToolTip = "Hello World"
                    };

but its not working for me


回答1:


You have two options either use Keywords accepted by the Chart control.

myChart.Series[0].ToolTip = "Name #SERIESNAME : X - #VALX{F2} , Y - #VALY{F2}";

In the Chart control, a Keyword is a character sequence that is replaced with an automatically calculated value at run time. For a comprehensive list of keywords accepted by the Chart control look up Keyword reference or

if you want something more fanciful, you have to handle the event GetToolTipText

this.myChart.GetToolTipText += new System.Windows.Forms.DataVisualization.Charting.Chart.ToolTipEventHandler(this.myChart_GetToolTipText);

Now I am not sure what you want to show on the ToolTip but you could add the logic accordingly. Assuming you want to show the values of the DataPoints in the series

private void myChart_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)
{
    switch(e.HitTestResult.ChartElementType)
    {  
        case ChartElementType.DataPoint:
            e.Text = myChart.Series[0].Points[e.HitTestResult.PointIndex]).ToString()
                     + /* something for which no keyword exists */;
            break;
        case ChartElementType.Axis:
            // add logic here
        case ....
        .
        .    
        default:
            // do nothing       
    }



回答2:


After some RnD i got tooltips on Line Series, but still confused why its not working with this solution. Here is the solution

series.ToolTip = string.Format("Name '{0}' : X - {1} , Y - {2}", chartPoint.SetName, "#VALX{F2}",
                                               "#VALY{F2}");
        mainChartControl.GetToolTipText += ChartControlGetToolTipText;

 private void ChartControlGetToolTipText(object sender, ToolTipEventArgs e)
    {
    }


来源:https://stackoverflow.com/questions/17962754/show-tooltip-in-lineseries-winforms-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!