MESCIUS SPREAD for Windows Forms 15.0J サンプルコード集
選択されたタイミングを取得する(チャート内のオブジェクト)

チャート上のオブジェクトを選択した場合にはSpreadChartクラスのSelectionChangedイベントが発生します。このイベント内でSelectedObjectを参照することで、チャート内のどのオブジェクトが選択されたかを判断することができます。

【実行例】

チャート

出力例

チャート チャート

[シリーズが選択されました]FarPoint.Win.Chart.SeriesInfo

[2番目のデータポイントが選択されました]FarPoint.Win.Chart.SeriesInfo

チャート チャート

[シリーズ以外のオブジェクトが選択されました]FarPoint.Win.Chart.LegendArea

[シリーズ以外のオブジェクトが選択されました]FarPoint.Win.Chart.AxisInfo

 private FarPoint.Win.Spread.Chart.SpreadChart SPChart;
 private void Form1_Load(object sender, System.EventArgs e)
 {

   // データを設定します
   fpSpread1.Sheets[0].Cells[0, 1].Value = "S1";
   fpSpread1.Sheets[0].Cells[0, 2].Value = "S2";
   fpSpread1.Sheets[0].Cells[0, 3].Value = "S3";

   fpSpread1.Sheets[0].Cells[1, 0].Value = "1Q";
   fpSpread1.Sheets[0].Cells[2, 0].Value = "2Q";
   fpSpread1.Sheets[0].Cells[3, 0].Value = "3Q";
   fpSpread1.Sheets[0].Cells[4, 0].Value = "4Q";

   fpSpread1.Sheets[0].Cells[1, 1].Value = 50;
   fpSpread1.Sheets[0].Cells[2, 1].Value = 30;
   fpSpread1.Sheets[0].Cells[3, 1].Value = 20;
   fpSpread1.Sheets[0].Cells[4, 1].Value = 40;

   fpSpread1.Sheets[0].Cells[1, 2].Value = 30;
   fpSpread1.Sheets[0].Cells[2, 2].Value = 15;
   fpSpread1.Sheets[0].Cells[3, 2].Value = 50;
   fpSpread1.Sheets[0].Cells[4, 2].Value = 20;

   fpSpread1.Sheets[0].Cells[1, 3].Value = 10;
   fpSpread1.Sheets[0].Cells[2, 3].Value = 40;
   fpSpread1.Sheets[0].Cells[3, 3].Value = 25;
   fpSpread1.Sheets[0].Cells[4, 3].Value = 25;

   // シリーズを設定します
   FarPoint.Win.Chart.LineSeries series1 = new FarPoint.Win.Chart.LineSeries();
   series1.SeriesNameDataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$B$1:$B$1", FarPoint.Win.Spread.Chart.SegmentDataType.Text);
   series1.Values.DataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$B$2:$B$5");
   series1.CategoryNames.DataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$A$2:$A$5", FarPoint.Win.Spread.Chart.SegmentDataType.Text);

   FarPoint.Win.Chart.LineSeries series2 = new FarPoint.Win.Chart.LineSeries();
   series2.SeriesNameDataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$C$1:$C$1", FarPoint.Win.Spread.Chart.SegmentDataType.Text);
   series2.Values.DataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$C$2:$C$5");
   series2.CategoryNames.DataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$A$2:$A$5", FarPoint.Win.Spread.Chart.SegmentDataType.Text);

   FarPoint.Win.Chart.LineSeries series3 = new FarPoint.Win.Chart.LineSeries();
   series3.SeriesNameDataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$D$1:$D$1", FarPoint.Win.Spread.Chart.SegmentDataType.Text);
   series3.Values.DataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$D$2:$D$5");
   series3.CategoryNames.DataSource = new FarPoint.Win.Spread.Chart.SeriesDataField(fpSpread1, "Values", "Sheet1!$A$2:$A$5", FarPoint.Win.Spread.Chart.SegmentDataType.Text);

   FarPoint.Win.Chart.YPlotArea plotArea = new FarPoint.Win.Chart.YPlotArea();
   plotArea.Location = new PointF(0.2f, 0.2f);
   plotArea.Size = new SizeF(0.55f, 0.6f);
   plotArea.Series.Add(series1);
   plotArea.Series.Add(series2);
   plotArea.Series.Add(series3);

   // ラベルを設定します
   FarPoint.Win.Chart.LabelArea label = new FarPoint.Win.Chart.LabelArea();
   label.Text = "四半期別";
   label.Location = new PointF(0.5f, 0.02f);
   label.AlignmentX = 0.5f;
   label.AlignmentY = 0f;

   // 凡例を設定します
   FarPoint.Win.Chart.LegendArea legend = new FarPoint.Win.Chart.LegendArea();
   legend.Location = new PointF(0.98f, 0.5f);
   legend.AlignmentX = 1f;
   legend.AlignmentY = 0.5f;

   // チャートモデルに各情報を追加します
   FarPoint.Win.Chart.ChartModel model = new FarPoint.Win.Chart.ChartModel();
   model.LabelAreas.Add(label);
   model.LegendAreas.Add(legend);
   model.PlotAreas.Add(plotArea);

   // SPREADチャートにチャートモデルを設定します
   FarPoint.Win.Spread.Chart.SpreadChart chart = new FarPoint.Win.Spread.Chart.SpreadChart();
   chart.ChartName = "barchart";
   chart.Size = new Size(400, 200);
   chart.Location = new Point(3, 130);
   chart.Model = model;

   // シートにSPREADチャートを追加します
   fpSpread1.Sheets[0].Charts.Add(chart);

   SPChart = chart;
   SPChart.SelectionChanged += new EventHandler(SPChart_SelectionChanged);

 }
 private void SPChart_SelectionChanged(object sender, System.EventArgs e)
 {

   if (SPChart.SelectedObject != null)
   {
      if ((SPChart.SelectedObject) is FarPoint.Win.Chart.SeriesInfo)
      {
         // データポイントのインデックスを取得します
         int dataPidx = ((FarPoint.Win.Chart.SeriesInfo)(System.Object)SPChart.SelectedObject).PointIndex;
         if (dataPidx == -1)
         {
            Console.Write("[シリーズが選択されました]");
            Console.WriteLine(SPChart.SelectedObject.ToString());
         }
         else
         {
            Console.Write("[" + (dataPidx + 1).ToString() + "番目のデータポイントが選択されました]");
            Console.WriteLine(SPChart.SelectedObject.ToString());
         }
      }
      else
      {
         Console.Write("[シリーズ以外のオブジェクトが選択されました]");
         Console.WriteLine(SPChart.SelectedObject.ToString());
      }
   }
   else
   {
      Console.WriteLine("Nothing");
   }

 }
 Private WithEvents SPChart As New FarPoint.Win.Spread.Chart.SpreadChart()

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

   ' データを設定します
   FpSpread1.Sheets(0).Cells(0, 1).Value = "S1"
   FpSpread1.Sheets(0).Cells(0, 2).Value = "S2"
   FpSpread1.Sheets(0).Cells(0, 3).Value = "S3"

   FpSpread1.Sheets(0).Cells(1, 0).Value = "1Q"
   FpSpread1.Sheets(0).Cells(2, 0).Value = "2Q"
   FpSpread1.Sheets(0).Cells(3, 0).Value = "3Q"
   FpSpread1.Sheets(0).Cells(4, 0).Value = "4Q"

   FpSpread1.Sheets(0).Cells(1, 1).Value = 50
   FpSpread1.Sheets(0).Cells(2, 1).Value = 30
   FpSpread1.Sheets(0).Cells(3, 1).Value = 20
   FpSpread1.Sheets(0).Cells(4, 1).Value = 40

   FpSpread1.Sheets(0).Cells(1, 2).Value = 30
   FpSpread1.Sheets(0).Cells(2, 2).Value = 15
   FpSpread1.Sheets(0).Cells(3, 2).Value = 50
   FpSpread1.Sheets(0).Cells(4, 2).Value = 20

   FpSpread1.Sheets(0).Cells(1, 3).Value = 10
   FpSpread1.Sheets(0).Cells(2, 3).Value = 40
   FpSpread1.Sheets(0).Cells(3, 3).Value = 25
   FpSpread1.Sheets(0).Cells(4, 3).Value = 25

   ' シリーズを設定します
   Dim series1 As New FarPoint.Win.Chart.LineSeries()
   series1.SeriesNameDataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$B$1:$B$1", FarPoint.Win.Spread.Chart.SegmentDataType.Text)
   series1.Values.DataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$B$2:$B$5")
   series1.CategoryNames.DataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$A$2:$A$5", FarPoint.Win.Spread.Chart.SegmentDataType.Text)

   Dim series2 As New FarPoint.Win.Chart.LineSeries()
   series2.SeriesNameDataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$C$1:$C$1", FarPoint.Win.Spread.Chart.SegmentDataType.Text)
   series2.Values.DataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$C$2:$C$5")
   series2.CategoryNames.DataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$A$2:$A$5", FarPoint.Win.Spread.Chart.SegmentDataType.Text)

   Dim series3 As New FarPoint.Win.Chart.LineSeries()
   series3.SeriesNameDataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$D$1:$D$1", FarPoint.Win.Spread.Chart.SegmentDataType.Text)
   series3.Values.DataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$D$2:$D$5")
   series3.CategoryNames.DataSource = New FarPoint.Win.Spread.Chart.SeriesDataField(FpSpread1, "Values", "Sheet1!$A$2:$A$5", FarPoint.Win.Spread.Chart.SegmentDataType.Text)

   Dim plotArea As New FarPoint.Win.Chart.YPlotArea()
   plotArea.Location = New PointF(0.2F, 0.2F)
   plotArea.Size = New SizeF(0.55F, 0.6F)
   plotArea.Series.Add(series1)
   plotArea.Series.Add(series2)
   plotArea.Series.Add(series3)

   ' ラベルを設定します
   Dim label As New FarPoint.Win.Chart.LabelArea()
   label.Text = "四半期別"
   label.Location = New PointF(0.5F, 0.02F)
   label.AlignmentX = 0.5F
   label.AlignmentY = 0.0F

   ' 凡例を設定します
   Dim legend As New FarPoint.Win.Chart.LegendArea()
   legend.Location = New PointF(0.98F, 0.5F)
   legend.AlignmentX = 1.0F
   legend.AlignmentY = 0.5F

   ' チャートモデルに各情報を追加します
   Dim model As New FarPoint.Win.Chart.ChartModel()
   model.LabelAreas.Add(label)
   model.LegendAreas.Add(legend)
   model.PlotAreas.Add(plotArea)

   ' SPREADチャートにチャートモデルを設定します
   Dim chart As New FarPoint.Win.Spread.Chart.SpreadChart()
   chart.ChartName = "barchart"
   chart.Size = New Size(400, 200)
   chart.Location = New Point(3, 130)
   chart.Model = model

   ' シートにSPREADチャートを追加します
   FpSpread1.Sheets(0).Charts.Add(chart)

   SPChart = chart
 End Sub

 Private Sub SPChart_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SPChart.SelectionChanged

   If Not SPChart.SelectedObject Is Nothing Then
      If (TypeOf (SPChart.SelectedObject) Is FarPoint.Win.Chart.SeriesInfo) Then
         ' データポイントのインデックスを取得します
         Dim dataPidx As Integer = DirectCast(DirectCast(SPChart.SelectedObject, System.Object), FarPoint.Win.Chart.SeriesInfo).PointIndex
         If dataPidx = -1 Then
            Console.Write("[シリーズが選択されました]")
            Console.WriteLine(SPChart.SelectedObject.ToString())
         Else
            Console.Write("[" & (dataPidx + 1).ToString & "番目のデータポイントが選択されました]")
            Console.WriteLine(SPChart.SelectedObject.ToString())
         End If
      Else
         Console.Write("[シリーズ以外のオブジェクトが選択されました]")
         Console.WriteLine(SPChart.SelectedObject.ToString())
      End If
   Else
      Console.WriteLine("Nothing")
   End If

 End Sub

 

 


© MESCIUS inc. All rights reserved.