工具提示的值替换为最后的foreach值

时间:2018-08-16 22:34:51

标签: c#

我有一个事件来获取柱状图中的工具提示文本,例如:

wrap_content

然后方法就像:

empChart.GetToolTipText += empChart_GetToolTipText;

目前我收到两个系列,因此我想在每个系列中为其当前private void empChart_GetToolTipText(object sender, ToolTipEventArgs e ) { if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint) { Chart senderChart = (Chart)sender; var series = senderChart.Series; var points = e.HitTestResult.Series.Points; foreach (var s in series) { var finalString = s.LegendToolTip.Replace("\r", ""); string[] tooltipstring = finalString.Split('\n'); string progresstoBackCheckString = string.Join("\n", tooltipstring.Where(x => x.Contains("progress to backcheck"))); string backcheckToCorrectionsString = string.Join("\n", tooltipstring.Where(x => x.Contains("backcheck to corrections"))); string correctionsToCompletedString = string.Join("\n", tooltipstring.Where(x => x.Contains("corrections to completed"))); string progressToCompletedString = string.Join("\n", tooltipstring.Where(x => x.Contains("progress to completed"))); int i = e.HitTestResult.PointIndex; DataPoint dp = e.HitTestResult.Series.Points[i]; switch (dp.AxisLabel) { case "Progress to Back Check": e.Text = $"{progresstoBackCheckString}"; break; case "Back Check to Corrections": e.Text = $"{backcheckToCorrectionsString}"; break; case "Corrections to Completed": e.Text = $"{correctionsToCompletedString}"; break; case "Progress to Completed": e.Text = $"{progressToCompletedString}"; break; } } } } 分配一个不同的工具提示。问题是两个系列替换了第一个系列的值,因此两个系列具有相同的ToolTipText。如何根据收到的s.LegendToolTip设置每个系列的工具提示?

没有办法知道我徘徊的意甲?在s.LegendToolTip事件中发生?问候

2 个答案:

答案 0 :(得分:1)

ToolTipEventArgs e具有属性HitTestResult,提供了引发图表事件GetToolTipText的详细信息。 HitTestResult Class具有Series属性,而该属性又具有您可以测试的Name属性。

if (e.ChartElementType == ChartElementType.DataPointLabel) {
    switch (e.HitTestResult.Series.Name)
    {
        case "My series 1 name":
            ...
            break;
        case "My series 2 name":
            ...
            break;
    }
}

当您将鼠标悬停在图表的不同部分时,必须进行测试以查看ToolTipEventArgs到底包含什么。

如果这不可能(因为名称未知),请像这样测试

if (empChart.Series[0] == e.HitTestResult.Series) {
   // First series
} 

但是我想图表对象会自动分配一个默认的序列名称,例如“ Series 1”。 (未测试)。使用调试器进行检查。

答案 1 :(得分:0)

为此,我创建了一个模型,然后在图表上搜索击中或悬停的条形:

  if (e.HitTestResult.ChartElementType == ChartElementType.DataPoint)
            {
                Chart senderChart = (Chart)sender;
                var series = senderChart.Series;
                var points = e.HitTestResult.Series.Points;

                //e.HitTestResult.Series.
                var legendTip = series.Select(x => new
                {
                    x.LegendToolTip,
                    x.Name
                }).ToList();

                List<TooltipTextModel> tooltipModel = new List<TooltipTextModel>();
                foreach (var lt in legendTip)
                {
                TooltipTextModel assignTooltip = new TooltipTextModel();
                    var finalString = lt.LegendToolTip.Replace("\r", "");
                    string[] tooltipstring = finalString.Split('\n');

                    var progresstoBackCheckString = string.Join("\n", tooltipstring.Where(x => x.Contains("progress to backcheck")));
                    var backcheckToCorrectionsString = string.Join("\n", tooltipstring.Where(x => x.Contains("backcheck to corrections")));
                    var correctionsToCompletedString = string.Join("\n", tooltipstring.Where(x => x.Contains("corrections to completed")));
                    var progressToCompletedString = string.Join("\n", tooltipstring.Where(x => x.Contains("progress to completed")));

                    assignTooltip.ProgresstoBackCheckString = progresstoBackCheckString;
                    assignTooltip.BackcheckToCorrectionsString = backcheckToCorrectionsString;
                    assignTooltip.CorrectionsToCompletedString = correctionsToCompletedString;
                    assignTooltip.ProgressToCompletedString = progressToCompletedString;
                    assignTooltip.SerieName = lt.Name;
                    tooltipModel.Add(assignTooltip);
                    }

                var hitSerie = e.HitTestResult.Series.Name.ToString();

                foreach (var tt in tooltipModel.Where(x=>x.SerieName == hitSerie))
                {
                    int i = e.HitTestResult.PointIndex;
                    var chartSerie = empChart.Series[e.HitTestResult.Series.Name].Name.ToString();

                        DataPoint dp = e.HitTestResult.Series.Points[i];

                        switch (dp.AxisLabel)
                        {
                            case "Progress to Back Check":
                                e.Text = $"{tt.ProgresstoBackCheckString}";
                                break;
                            case "Back Check to Corrections":
                                e.Text = $"{tt.BackcheckToCorrectionsString}";
                                break;
                            case "Corrections to Completed":
                                e.Text = $"{tt.CorrectionsToCompletedString}";
                                break;
                            case "Progress to Completed":
                                e.Text = $"{tt.ProgressToCompletedString}";
                                break;
                    }
                }
            }