如何在MS图表中显示带有各种数据的工具提示 [英] How to display tooltips with various data in MS charts

查看:141
本文介绍了如何在MS图表中显示带有各种数据的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发蜡烛图。

I am developing a candle chart in C #.

我一直在使用当前datagridview中的数据创建蜡烛图。

I've been working on creating candle charts using data from the current datagridview.

另外,当我将光标放在图表的蜡烛点上时,我想显示datagridview的信息(打开,关闭,高,低)。 (查看图片)

Additionally, when I place the cursor over the candle point of the chart, I want to show the information (open, close, high, low) of the datagridview. (See image)

当前开发的来源。

    DataTable table_ChartData = new DataTable();
    table_ChartData.Columns.Add("Id");
    table_ChartData.Columns.Add("Open");
    table_ChartData.Columns.Add("Close");
    table_ChartData.Columns.Add("High");
    table_ChartData.Columns.Add("Low");
    table_ChartData.Columns.Add("Day");
    dataGridView1.DataSource = table_ChartData;  

    chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineWidth = 1;
    chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
    chart1.ChartAreas["ChartArea1"].AxisY.Maximum = max;
    chart1.ChartAreas["ChartArea1"].AxisY.Minimum = min;

    chart1.Series["Daily"].XValueMember = "Day";
    chart1.Series["Daily"].YValueMembers = "High,Low,Open,Close";
    chart1.Series["Daily"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;

    chart1.Series["Daily"].CustomProperties = "PriceDownColor=Blue,PriceUpColor=Red";
    chart1.Series["Daily"]["OpenCloseStyle"] = "Triangle";
    chart1.Series["Daily"]["ShowOpenClose"] = "Both";

    chart1.DataSource = table_ChartData;
    chart1.DataBind();

    private void chart1_MouseMove(object sender, MouseEventArgs e)
    {

        Point mousePoint = new Point(e.X, e.Y);
        chart1.ChartAreas[0].CursorX.SetCursorPixelPosition(mousePoint, true);
        chart1.ChartAreas[0].CursorY.SetCursorPixelPosition(mousePoint, true);`

        var pos = e.Location;
        if (prevPosition.HasValue && pos == prevPosition.Value)
            return;
        tooltip.RemoveAll();
        prevPosition = pos;
        var results = chart1.HitTest(pos.X, pos.Y, false, ChartElementType.DataPoint); // set ChartElementType.PlottingArea for full area, not only DataPoints
        foreach (var result in results)
        {
            if (result.ChartElementType == ChartElementType.DataPoint) // set ChartElementType.PlottingArea for full area, not only DataPoints
            {
                var yVal = result.ChartArea.AxisY.PixelPositionToValue(pos.Y);
                tooltip.Show(((int)yVal).ToString(), chart1, pos.X, pos.Y - 15);
            }
        }

    }

谢谢您的帮助。谢谢:)

Thank you for your help. Thank you :)

推荐答案

您正在 ToolTips 中创建c $ c> MouseMove ;这是一种方法,但是最简单的方法是在创建 DataPoints 时通过设置 DataPoint.Tooltip 属性来让图表完成工作。 code> ..:

You are creating ToolTips in the MouseMove; this is one way but the easiest approach is to let the Chart do the work by setting the DataPoint.Tooltip property right when you create the DataPoints..:

DataPoint dp = new DataPoint (..);
dp.ToolTip = "x=" + dp.XValue + "\n high=" + dp.YValues[0]+ "\n low=" + dp.YValues[1] + ..;
yourSeries.Points.Add(dp);






..或者,如果积分 DataBound 或者在绑定后立即添加 ToolTips ,或者可以将它们包括在绑定本身中。


.. or, if the Points are DataBound either add ToolTips right after you bind or, maybe include them in the binding itself.

请注意,只有某些各种数据绑定方法可让您绑定工具提示之类的扩展图表属性。明确提到了 Points.DataBind 。这意味着您需要为数据源中的工具提示准备一个准备好的字段,因为我不知道如何在 otherField 字符串中编写连接表达式。

Note that only some of the various data binding methods will let you bind 'extended chart properties' like tooltips. Points.DataBind is mentioned explicitly. This means that you need a prepared field for the tooltips in your datasource as I know of no way to write a concatenating expression in the otherField string..

如果您的数据包含在 DataTable 中,且具有以下字段,则可以使用以下语法进行绑定:

If you have your data in a DataTable with the fields below you can use syntax like this for the binding:

var enumerableTable = (dt as System.ComponentModel.IListSource).GetList();
yourSeries.Points.DataBind(enumerableTable, "x-col", 
                          "highField, lowField..", "Tooltip=tooltipField");






如果要在 MouseMove ,您可以轻松获得对结束的 DataPoint 的引用(如果有),并使用上面的所有值。 。:


If you want to do it in the MouseMove you can easily get a reference to the DataPoint you are over, if any, and work with all its values like above..:

DataPoint dp = null;
if (results.PointIndex >= 0 && results.ChartElementType == ChartElementType.DataPoint)
{
    dp = results.Series.Points[hitt.PointIndex];
    string tText = "x=" + dp.XValue + "\n high=" + 
                   dp.YValues[0]+ "\n low=" + dp.YValues[1] + ..;
..
}

请注意, HitTest 结果是具有多个属性的一个对象。无需循环!

Note that the HitTest result is one object with several properties. No need to loop over it!

这篇关于如何在MS图表中显示带有各种数据的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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