【问题标题】:winform mschart hittest not finding legendwinform mschart hittest 没有找到图例
【发布时间】:2013-06-30 11:40:09
【问题描述】:

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

chTo.Series.Add(chFrom.Series[s]); //Reassign series to new chart   
chTo.Legends.Add(chFrom.Legends[s]); //Reassign legend to new chart

效果很好。
但是,我正在尝试为图例实现工具提示,并且遇到了一个问题,即只有图表中的第一个图例会显示工具提示。当我做一个 hittest 时,只有第一个图例被识别出来。所有后续的图例,虽然在图表上可见,但对 hittest 方法并不“看到”。我在想这就是为什么工具提示没有显示的原因,因为没有对象可以触发工具提示的 mouseover 事件。

我一直无法找到一种方法来“扩展”图例区域(由 hittest 方法检测到)来完成这项工作。

有人有什么想法吗?谢谢!

【问题讨论】:

  • 你是如何为你实现工具提示的Legend?我已经通过设置系列的属性LegendToolTip 进行了测试。 Legend 可用于许多 Series,因此工具提示应与一些 Series 而非一些 Legend 相关联。

标签: c# winforms charts legend hittest


【解决方案1】:

回应King King--

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

                //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;

在重新分配系列和图例之后(使用我原始帖子中的代码),然后我根据鼠标按下事件的以下命中测试所定位的光标位置设置图例值:

                    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;
            }
        }           
    }

所以,我最终得到了我想要的图例,但工具提示只显示第一个图例项。我也尝试过自定义项目。有了它们,我得到了工具提示,但我失去了格式。这让我疯狂了好几个星期(断断续续),我真的很想继续讨论其他问题。显然(无论如何对我而言),我没有做正确的事情仅仅是因为我不了解有关图表的所有知识,而且 MSChart 示例的好处非常有限。

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

【讨论】:

    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2017-12-12
    • 2018-02-22
    • 1970-01-01
    • 2019-12-22
    相关资源
    最近更新 更多