我正在用C#開發蠟燭圖。如何在MS圖表中顯示各種數據的工具提示
我一直在使用當前datagridview的數據創建蠟燭圖。
此外,當我將光標放在圖表的蠟燭點上,我要顯示在DataGridView的信息(開,關,高,低)。 (見圖)
目前開發的源碼。
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);
}
}
}
謝謝你的幫忙。謝謝:)
TaW非常感謝。這很有幫助。 –