HotTest()的替代品

时间:2013-07-18 11:44:24

标签: c# visual-studio-2012 charts

目前我的软件在MSCharts中使用了图表对象的HitTest()方法,但随着我将其扩展到我的图表上越来越多的数据点以及其他因素,这可能会对性能造成巨大影响。

我想知道是否有任何替代方法可以提供相同的功能(在图表上获取光标位置的X坐标)但没有性能命中,因为命中测试似乎是一种非常强大的获取方式我的回答。

我的图表是从班级System.Windows.Forms.DataVisualization.Charting.Chart

创建的

为清晰起见编辑:我需要在图表上找到一条线的位置,以便将其用于其他计算。

1 个答案:

答案 0 :(得分:2)

鼠标滚轮事件存在相同的性能问题。

这是我的解决方案:

  1. 获取当前鼠标位置的轴值:

    double posX = Math.Round(currentArea.AxisX.PixelPositionToValue(e.X));
    double posY = Math.Round(currentArea.AxisY.PixelPositionToValue(e.Y));
    

    Showing Mouse Axis Coordinates on Chart Control获取一点点改动以使其更准确。

    但您应该先检查一下,鼠标是否在 ChartArea 中,否则会引发例外

  2. 获取鼠标指向的ChatElement:

    // Gets the ChartArea that the mouse points
    private ChartArea mouseinChartArea(Chart source, Point e)
    {
        double relativeX = (double)e.X * 100 / source.Width;
        double relativeY = (double)e.Y * 100 / source.Height;
    
        foreach (ChartArea ca in source.ChartAreas)
        {
            if (relativeX > ca.Position.X && relativeX < ca.Position.Right &&
                relativeY > ca.Position.Y && relativeY < ca.Position.Bottom)
                return ca;
        }
        return null;
    }
    
    // for my purpose, returns an axis. But you can return anything
    private Axis findAxisforZooming(Chart source, Point e)
    {
        ChartArea currentArea = mouseinChartArea(source, new Point(e.X, e.Y)); // Check if inside 
        if (currentArea == null)
            return null;
    
        double axisXfontSize = currentArea.AxisX.LabelAutoFitMinFontSize + ((double)source.Width / SystemInformation.PrimaryMonitorSize.Width)
            * (currentArea.AxisX.LabelAutoFitMaxFontSize - currentArea.AxisX.LabelAutoFitMinFontSize);
        double axisYfontSize = currentArea.AxisY.LabelAutoFitMinFontSize + ((double)source.Height / SystemInformation.PrimaryMonitorSize.Height)
            * (currentArea.AxisY.LabelAutoFitMaxFontSize - currentArea.AxisY.LabelAutoFitMinFontSize);
        double axisYfontHeightSize = (axisYfontSize - currentArea.AxisY.LabelStyle.Font.Size) + currentArea.AxisY.LabelStyle.Font.Height;
    
        Graphics g = this.CreateGraphics();
        if (currentArea.AxisX.LabelStyle.Font.Unit == GraphicsUnit.Point)
            axisXfontSize = axisXfontSize * g.DpiX / 72;
        if (currentArea.AxisY.LabelStyle.Font.Unit == GraphicsUnit.Point)
            axisYfontHeightSize = axisYfontHeightSize * g.DpiX / 72;
        g.Dispose();
    
        // Replacing the SystemInformation.PrimaryMonitorSize with the source.Width / Height will give the accurate TickMarks size.
        // But it doens't count for the gab between the tickMarks and the axis lables (so by replacing, it give a good proximity with the gab)
        int axisYTickMarks = (int)Math.Round(currentArea.AxisY.MajorTickMark.Size / 100 * SystemInformation.PrimaryMonitorSize.Width); // source.Width;
        int axisXTickMarks = (int)Math.Round(currentArea.AxisX.MajorTickMark.Size / 100 * SystemInformation.PrimaryMonitorSize.Height); // source.Height;
    
        int leftInnerPlot = (int)Math.Round(currentArea.Position.X / 100 * source.Width +
            currentArea.InnerPlotPosition.X / 100 * currentArea.Position.Width / 100 * source.Width);
        int rightInnerPlot = (int)Math.Round(currentArea.Position.X / 100 * this.chart1.Width +
            currentArea.InnerPlotPosition.Right / 100 * currentArea.Position.Width / 100 * source.Width);
        int topInnerPlot = (int)Math.Round(currentArea.Position.Y / 100 * this.chart1.Height +
            currentArea.InnerPlotPosition.Y / 100 * currentArea.Position.Height / 100 * source.Height);
        int bottomInnerPlot = (int)Math.Round(currentArea.Position.Y / 100 * source.Height +
            currentArea.InnerPlotPosition.Bottom / 100 * currentArea.Position.Height / 100 * source.Height);
    
        // Now you got the boundaries of every important ChartElement.
        // Only left to check if the mouse is within your desire ChartElement,
        // like the following:    
    
        bottomInnerPlot += axisXTickMarks + (int)Math.Round(axisXfontSize); // Include AxisX
    
        if (e.X > leftInnerPlot && e.X < rightInnerPlot &&
            e.Y > topInnerPlot && e.Y < bottomInnerPlot) // return AxisX if inside the InnerPlot area or on AxisX
            return currentArea.AxisX;
        else if (e.X > (leftInnerPlot - axisYTickMarks - (int)Math.Round(axisYfontHeightSize)) && e.X < rightInnerPlot &&
                 e.Y > topInnerPlot && e.Y < bottomInnerPlot) // return AxisY if on AxisY only
            return currentArea.AxisY;
    
        return null;
    }
    
  3. 可以看出,代码长于HitTest()。但运行时间更短