2017-09-14 28 views
1

C#でキャンドルチャートを作成しています。さまざまなデータを含むツールチップをMSチャートに表示する方法

私は現在のデータグリッドビューからのデータを使用してキャンドルチャートを作成しています。私は、チャートのキャンドルポイントの上にカーソルを置くと

enter image description here

はまた、私は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); 
      } 
     } 

    } 

ありがとうございました。ありがとうございました:)

答えて

0

MouseMoveToolTipsを作成しています。ポイントは、どちらかDataBoundをしている場合、

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

...か:これは一つの方法ですが、最も簡単な方法は、あなたがDataPointsを作成するときにチャートがDataPoint.Tooltip財産権を設定することで、仕事をさせることです。..バインドした直後にToolTipsを追加するか、バインディング自体に含めることもできます。

various data binding methodsの一部のみが、ツールチップのような '拡張チャートプロパティ'をバインドできることに注意してください。 Points.DataBindが明示的に言及されています。これは、otherField文字列に連結式を記述する方法がないことを知っているので、データソースのツールチップの準備フィールドが必要であることを意味します。

データがDataTableの場合、結合のために、このような構文:

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

あなたがそれをしたい場合はMouseMoveあなたは簡単にあなたが終わったDataPointへの参照を取得し、もしあれば、上記のようなすべての値を使用して作業することができます..:

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の結果は、いくつかのプロパティを持つ1つのオブジェクトであることに注意してください。ループする必要はありません!

+0

TaWありがとうございました。それは役に立ちました。 –

関連する問題