大復活C#学習_フォームを用いてサインカーブを描く

f:id:yasui_swift:20210214055323p:plain

サインカーブ


フォームを用いてグラフを描いてみる。

もっともポピュラーなサインカーブを描く。

ツールボックスで<データ>からchartを選択してグラフを表示する。

Form1.cs[デザイン]の画面に棒グラフのサンプルが表示されるので、グラフ領域をクリックして、プロパティウインドウの<Series>のコレクションを選択する。

コレクションのリストからグラフの種類や属性を決める。x軸、y軸の最大値、最小値、罫線の間隔を設定する。

Form1.csに切り替えてプロジェクトを書く。

サイン値は角度をラジアンの変換して計算する(C = Math.PI / 180)。

グラフの色や最大値などの細かい設定は随時行う。

あまり使いやすいとは言えないが、とりあえずグラフが描けたのでよしとする。

*********************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace graphSample_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
double x1 = new double[360];
double
y1 = new double[360];
double c = Math.PI / 180;

for (int i = 0; i < 360; i++)
{
x1[i] = i ;

y1[i] = Math.Sin((double)i* c );

chart1.Series["S1"].Points.AddXY(x1[i], y1[i]);
chart1.ChartAreas[0].AxisX.Maximum = 360;
chart1.ChartAreas[0].AxisX.Minimum = 0;


}
}
}
}

f:id:yasui_swift:20210213053226j:plain

足柄山の金太郎