如何使用鼠标事件在Zedgraph图表外滚动轴刻度

时间:2012-09-05 06:57:36

标签: zedgraph

图表外的轴刻度是否可以使用鼠标事件“mouse_down and hold”进行缩放,并在y轴上向上或向下移动,x轴向左或向右移动?恩。当我触发MouseDownEvent并保持x轴刻度0.6或者在该空间中放置该刻度并将其向右移动时,刻度应该滚动依赖于图表分数?你能发一个例子吗?提前谢谢!

3 个答案:

答案 0 :(得分:1)

使用ZedGraph的鼠标事件可以实现分别平移和缩放Y轴:MouseDownEventMouseMoveEventMouseUpEventMouseWheel事件(信用转到同事的矿)。

它适用于多个GraphPanes和多个Y轴。

当按下按钮移动鼠标时,MouseMoveEvent用于移动Y轴的最小值和最大值。如果没有,它用于获取鼠标悬停在其上的Y轴对象的引用。

MouseDownEvent用于启动轴平移操作。

MouseWheel用于在Y轴上执行缩放。

当缩放和平移操作完成时,MouseUpEvent用于清理事物。

以下是代码:

// The axis that is currently hovered by the mouse
YAxis hoveredYAxis;

// The graphpane that contains the axis
GraphPane foundPane;

// The scale of the axis before it is panned
double movedYAxisMin;
double movedYAxisMax;

// The Y on the axis when the panning operation is starting
float movedYAxisStartY;

void z_MouseWheel(object sender, MouseEventArgs e)
{
    if (hoveredYAxis != null)
    {
        var direction = e.Delta < 1 ? -.05f : .05f;
        var increment = direction * (hoveredYAxis.Scale.Max - hoveredYAxis.Scale.Min);
        var newMin = hoveredYAxis.Scale.Min + increment;
        var newMax = hoveredYAxis.Scale.Max - increment;

        hoveredYAxis.Scale.Min = newMin;
        hoveredYAxis.Scale.Max = newMax;

        foundPane.AxisChange();

        z.Invalidate();
    }
}

bool z_MouseUpEvent(ZedGraphControl sender, MouseEventArgs e)
{
    hoveredYAxis = null;
    return false;
}

bool z_MouseMoveEvent(ZedGraphControl sender, MouseEventArgs e)
{
    var pt = e.Location;

    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        if (hoveredYAxis != null)
        {
            var yOffset = hoveredYAxis.Scale.ReverseTransform(pt.Y) - hoveredYAxis.Scale.ReverseTransform(movedYAxisStartY);
            hoveredYAxis.Scale.Min = movedYAxisMin - yOffset;
            hoveredYAxis.Scale.Max = movedYAxisMax - yOffset;

            sender.Invalidate();
            return true;
        }
    }
    else
    {
        var foundObject = findZedGraphObject(null);
        hoveredYAxis = foundObject as YAxis;

        if (hoveredYAxis != null)
        {
            z.Cursor = Cursors.SizeNS;
            return true;
        }
        else
        {
            if (z.IsShowPointValues)
            {
                z.Cursor = Cursors.Cross;
                return false;
            }
            else
            {
                z.Cursor = Cursors.Default;
                return true;
            }
        }
    }

    return false;
}

bool z_MouseDownEvent(ZedGraphControl sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        if (hoveredYAxis != null)
        {
            movedYAxisStartY = e.Location.Y;
            movedYAxisMin = hoveredYAxis.Scale.Min;
            movedYAxisMax = hoveredYAxis.Scale.Max;
            return true;
        }
    }

    return false;
}

这是一个帮助器,它可以解释ZedGraph的对象查找操作。

object findZedGraphObject(GraphPane pane = null)
{
    var pt = zgc.PointToClient(Control.MousePosition);

    if (pane == null)
    {
        foundPane = zgc.MasterPane.FindPane(pt);
        if (foundPane != null)
        {
            object foundObject;
            int forget;

            using (var g = zgc.CreateGraphics())
                if (foundPane.FindNearestObject(pt, g, out foundObject, out forget))
                    return foundObject;
        }
    }

    return null;
}

答案 1 :(得分:0)

如果我理解你的问题,这是我的回答:

zedgraph有一个内置函数叫做“Pan”,你可以改变x&amp;的规模。 y轴。

  

将光标放在“图表区域”内   按住'ctrl'按钮&amp;将鼠标移向x&amp; y方向改变规模。

你可以通过'Un-Pan'(上下文菜单)恢复到原始状态

干杯..:)

答案 2 :(得分:0)

您想创建ScrollBar吗?

 zedGraphControl1.IsShowHScrollbar = true;
//Set borders for the scale
zedGraphControl1.GraphPane.XAxis.Scale.Max = Xmax;
zedGraphControl1.GraphPane.XAxis.Scale.Min = Xmin;