C# winform mschart hittest未找到图例

C# winform mschart hittest未找到图例,c#,winforms,charts,legend,hittest,C#,Winforms,Charts,Legend,Hittest,我有一个图表应用程序,它有一个覆盖功能,可以使用以下代码将“从”图表系列重新分配到“到”图表: chTo.Series.Add(chFrom.Series[s]); //Reassign series to new chart chTo.Legends.Add(chFrom.Legends[s]); //Reassign legend to new chart 效果很好但是,我正在尝试实现图例的工具提示,并且遇到了一个问题,即只有图表中的第一个图例才会显示工具提示。当我进行命中测试时,只

我有一个图表应用程序,它有一个覆盖功能,可以使用以下代码将“从”图表系列重新分配到“到”图表:

chTo.Series.Add(chFrom.Series[s]); //Reassign series to new chart   
chTo.Legends.Add(chFrom.Legends[s]); //Reassign legend to new chart
效果很好
但是,我正在尝试实现图例的工具提示,并且遇到了一个问题,即只有图表中的第一个图例才会显示工具提示。当我进行命中测试时,只会识别出第一个传奇。所有后续的图例,虽然在图表上可见,但对于hittest方法来说都不“可见”。我想这就是工具提示没有显示的原因,因为没有对象触发工具提示的鼠标悬停事件

我一直无法找到一种方法来“扩展”图例区域(通过hittest方法检测到),以使其正常工作

有人有什么想法吗?谢谢

回应国王--

原始图例的创建方法与图表相同,因此:

                //Create the series legend
                chartSel.Series[ySeries.Name].ChartArea = "ChartArea1";
                chartSel.Legends.Remove(chartSel.Legends.FindByName("Legend1"));
                chartSel.Legends.Add(ySeries.Name);
                chartSel.Legends[0].Name = ySeries.Name;

                //Format the series legend
                chartSel.Legends[ySeries.Name].Docking = Docking.Right;
                chartSel.Legends[ySeries.Name].DockedToChartArea = "ChartArea1";
                chartSel.Legends[ySeries.Name].Alignment = StringAlignment.Near;
                chartSel.Legends[ySeries.Name].IsDockedInsideChartArea = false;
                chartSel.Legends[ySeries.Name].LegendStyle = LegendStyle.Table; //.Row;
                chartSel.Legends[ySeries.Name].TableStyle = LegendTableStyle.Tall;
                chartSel.Legends[ySeries.Name].IsEquallySpacedItems = false;
                chartSel.Legends[ySeries.Name].Font = new Font("Segoe UI", 7, FontStyle.Bold);
                //chartSel.Legends[ySeries.Name].TextWrapThreshold = 17; // 19;
                chartSel.Legends[ySeries.Name].Position.Auto = false;
                chartSel.Legends[ySeries.Name].Position.X = 80;
                chartSel.Legends[ySeries.Name].Position.Y = 2;
                chartSel.Legends[ySeries.Name].Position.Width = 18;
                chartSel.Legends[ySeries.Name].Position.Height = 12;

                //Format series data point value cell
                chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ""));
                chartSel.Legends[ySeries.Name].CellColumns[0].Alignment = ContentAlignment.MiddleLeft; //.TopLeft;
                chartSel.Legends[ySeries.Name].CellColumns[0].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(10, 10, 1, 1);
                chartSel.Legends[ySeries.Name].CellColumns[0].MinimumWidth = 500;
                chartSel.Legends[ySeries.Name].CellColumns[0].MaximumWidth = 500;
                chartSel.Legends[ySeries.Name].CellColumns[0].BackColor = Color.FromArgb(120, chartSel.Series[ySeries.Name].Color);

                //Format legend cell spacer
                chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ""));
                chartSel.Legends[ySeries.Name].CellColumns[1].Alignment = ContentAlignment.TopLeft;
                chartSel.Legends[ySeries.Name].CellColumns[1].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 0, 0);
                chartSel.Legends[ySeries.Name].CellColumns[1].MinimumWidth = 25;
                chartSel.Legends[ySeries.Name].CellColumns[1].MaximumWidth = 25;
                chartSel.Legends[ySeries.Name].CellColumns[1].BackColor = Color.Black;

                //Format series title cell
                chartSel.Legends[ySeries.Name].CellColumns.Add(new LegendCellColumn("", LegendCellColumnType.Text, ySeries.Name));
                chartSel.Legends[ySeries.Name].CellColumns[2].Alignment = ContentAlignment.MiddleLeft;
                chartSel.Legends[ySeries.Name].CellColumns[2].Margins = new System.Windows.Forms.DataVisualization.Charting.Margins(0, 0, 1, 1);
                chartSel.Legends[ySeries.Name].CellColumns[2].MinimumWidth = 1475; //1500;
                chartSel.Legends[ySeries.Name].CellColumns[2].MaximumWidth = 1475; //1500;
                chartSel.Legends[ySeries.Name].CellColumns[2].ToolTip = ySeries.Name;
在重新分配系列和图例后(使用我原始帖子中的代码),我然后根据以下hittest定位的光标位置设置图例值,以响应鼠标按下事件:

                    pt = activePanel.PointToClient(Control.MousePosition);
                    ch = activePanel.GetChildAtPoint(pt) as Chart;
                    if (ch != null)
                    {
                        HitTestResult ht = ch.HitTest(e.X, e.Y, false);
                        if (ht.ChartElementType == ChartElementType.PlottingArea)
                        {
                            SetLegendValueText(ht, ch);
                        }
                    }

    private void SetLegendValueText(HitTestResult ht, Chart ch)
    {
        //Get the datapoint 'x' index value
        int dpIndex = 0;
        if (ht != null)
        {
            switch (ht.ChartElementType)
            {
                case ChartElementType.DataPoint: //Cursor is on a series line
                    DataPoint dp = ht.Object as DataPoint;
                    if (dp != null)
                    {
                        dpIndex = ht.PointIndex;
                    }
                    break;

                case ChartElementType.PlottingArea: //Cursor is somewhere in the plot area of the chart
                    dpIndex = (int)ht.ChartArea.CursorX.Position;
                    break;
            }
        }

        //Set legend value and legend tooltip
        for (int x = 0; x < ch.Legends.Count; x++) //foreach (Series s in ch.Series)
        {
            if (dpIndex > 0)
            {
                ch.Legends[x].Name = "Legend_" + x;
                ch.Legends[x].CellColumns[0].Text = ch.Series[x].Points[dpIndex - 1].YValues[0].ToString();
                ch.Legends[x].CellColumns[0].ToolTip = ch.Legends[x].CellColumns[0].Text;
            }
        }           
    }
pt=activePanel.PointToClient(Control.MousePosition);
ch=activePanel.GetChildAtPoint(pt)作为图表;
如果(ch!=null)
{
HitTestResult ht=ch.HitTest(e.X,e.Y,false);
if(ht.ChartElementType==ChartElementType.PlottingArea)
{
SetLegendValueText(ht,ch);
}
}
私有void SetLegendValueText(HitTestResult ht,图表ch)
{
//获取数据点“x”索引值
int-dpIndex=0;
如果(ht!=null)
{
开关(ht.ChartElementType)
{
case ChartElementType.DataPoint://光标位于一个系列线上
数据点dp=ht。对象作为数据点;
如果(dp!=null)
{
dpIndex=ht.PointIndex;
}
打破
case ChartElementType.PlottingArea://光标位于图表的绘图区域中的某个位置
dpIndex=(int)ht.ChartArea.CursorX.Position;
打破
}
}
//设置图例值和图例工具提示
for(int x=0;x0)
{
图例[x].Name=“图例”+x;
ch.Legends[x]。CellColumns[0]。Text=ch.Series[x]。Points[dpIndex-1]。yValue[0]。ToString();
ch.Legends[x].CellColumns[0]。工具提示=ch.Legends[x]。CellColumns[0]。文本;
}
}           
}
因此,我最终得到了我想要的图例,但是工具提示只显示第一个图例项。我也尝试过做定制项目。有了它们,我得到了工具提示,但我丢失了格式。这几周来(断断续续)一直让我发疯,我真的很想继续讨论其他问题。显然(无论如何对我来说),我没有做正确的事情,因为我不知道关于图表的所有知识,MSChart样本的好处非常有限


如果能为我指明正确的方向,我将不胜感激。

您是如何为您实现工具提示的
Legend
?我已经通过设置一个系列的属性
LegendToolTip
测试了OK。
图例可以用于许多
系列
,因此工具提示应该与一些
系列
关联,而不是与一些
图例
关联。