移动鼠标时如何显示X轴和Y轴值? [英] How to display X-Axis and Y-Axis values when move the mouse?

查看:1417
本文介绍了移动鼠标时如何显示X轴和Y轴值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标在图表区域(如图片)中的任何位置移动
时,如何显示X轴和Y轴的值?





HitTest 方法不能应用于移动,或者它只能应用于点击图表?



请帮助我。提前感谢。





还要注意,该示例显示原始数据,而x轴标签显示数据为 DateTimes 。使用

  string s = DateTime.FromOADate(x).ToShortDateString 

或类似的东西将值转换为日期!



检查在实际plotarea内部使用这两个有用的函数:

  RectangleF ChartAreaClientRectangle(Chart chart,ChartArea CA )
{
RectangleF CAR = CA.Position.ToRectangleF();
float pw = chart.ClientSize.Width / 100f;
float ph = chart.ClientSize.Height / 100f;
return new RectangleF(pw * CAR.X,ph * CAR.Y,pw * CAR.Width,ph * CAR.Height);
}

RectangleF InnerPlotPositionClientRectangle(Chart chart,ChartArea CA)
{
RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
RectangleF CArp = ChartAreaClientRectangle(chart,CA);

float pw = CArp.Width / 100f;
float ph = CArp.Height / 100f;

返回新的RectangleF(CArp.X + pw * IPP.X,CArp.Y + ph * IPP.Y,
pw * IPP.Width,ph * IPP.Height);
}



如果你愿意,可以缓存 InnerPlotPositionClientRectangle ;您需要在更改数据布局或调整图表大小时执行此操作。


How can I display the values of X-Axis and Y-Axis when the mouse is moved anywhere within the chart area (like the picture)?

The HitTest method can not be applied for moving or it can only be applied for clicking on the chart?

Please help me. Thanks in advance.

The tooltip shows data outside the real area data drawn

解决方案

Actually the Hittest method works just fine in a MouseMove; its problem is that it will only give a hit when you are actually over a DataPoint.

The Values on the Axes can be retrieved/converted from pixel coordinates by these axis-functions:

ToolTip tt = null;
Point tl = Point.Empty;

private void chart1_MouseMove(object sender, MouseEventArgs e)
{
    if (tt == null )  tt = new ToolTip();

    ChartArea ca = chart1.ChartAreas[0];

    if (InnerPlotPositionClientRectangle(chart1, ca).Contains(e.Location))
    {

        Axis ax = ca.AxisX;
        Axis ay = ca.AxisY;
        double x = ax.PixelPositionToValue(e.X);
        double y = ay.PixelPositionToValue(e.Y);
        string s = DateTime.FromOADate(x).ToShortDateString();
        if (e.Location != tl)
            tt.SetToolTip(chart1, string.Format("X={0} ; {1:0.00}", s, y));
        tl = e.Location;
    }
    else tt.Hide(chart1);
}

Note that they will not work while the chart is busy laying out the chart elements or before it has done so. MouseMove is fine, though.

Also note that the example displays the raw data while the x-axis labels show the data as DateTimes. Use

string s = DateTime.FromOADate(x).ToShortDateString();

or something similar to convert the values to dates!

The check for being inside the actual plotarea uses these two useful functions:

RectangleF ChartAreaClientRectangle(Chart chart, ChartArea CA)
{
    RectangleF CAR = CA.Position.ToRectangleF();
    float pw = chart.ClientSize.Width / 100f;
    float ph = chart.ClientSize.Height / 100f;
    return new RectangleF(pw * CAR.X, ph * CAR.Y, pw * CAR.Width, ph * CAR.Height);
}

RectangleF InnerPlotPositionClientRectangle(Chart chart, ChartArea CA)
{
    RectangleF IPP = CA.InnerPlotPosition.ToRectangleF();
    RectangleF CArp = ChartAreaClientRectangle(chart, CA);

    float pw = CArp.Width / 100f;
    float ph = CArp.Height / 100f;

    return new RectangleF(CArp.X + pw * IPP.X, CArp.Y + ph * IPP.Y,
                            pw * IPP.Width, ph * IPP.Height);
}

If you want to you can cache the InnerPlotPositionClientRectangle; you need to do so when you change the data layout or resize the chart.

这篇关于移动鼠标时如何显示X轴和Y轴值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆