1

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

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 方法检测到)来完成这项工作。

有没有人有任何想法?谢谢!

4

1 回答 1

0

回应国王国王——

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

                //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 示例的好处非常有限。

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

于 2013-07-03T15:02:56.597 回答