MainWindow.xaml.csの全コード
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace MyGraph
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
myGraph myG = new myGraph();
myG.SetMyC();
myG.myLine(Grid1);
}
}
public class myGraph
{
public Color[] c = new Color[18];
public void SetMyC()
{
int i;
for (i = 0; i < 3; i++)
{
c[i] = Color.FromArgb(255, Convert.ToByte(255 - 75 * i), 0, 0);
c[i + 3] = Color.FromArgb(255, Convert.ToByte(150 - 40 * i), Convert.ToByte(150 - 40 * i), 0);
c[i + 6] = Color.FromArgb(255, 0, Convert.ToByte(255 - 75 * i), 0);
c[i + 9] = Color.FromArgb(255, 0, Convert.ToByte(150 - 40 * i), Convert.ToByte(150 - 40 * i));
c[i + 12] = Color.FromArgb(255, 0, 0, Convert.ToByte(255 - 75 * i));
c[i + 15] = Color.FromArgb(255, Convert.ToByte(150 - 40 * i), 0, Convert.ToByte(150 - 40 * i));
}
}
public void myLine(Grid Grid1)
{
int i;
for (i = 0; i < 18; i++)
{
Path myPath = new Path();
myPath.StrokeThickness = 2;
LineGeometry myGeometry = new LineGeometry();
myPath.Stroke = new SolidColorBrush(c[i]);
myGeometry.StartPoint = new System.Windows.Point(30, 50 + 15 * i);
myGeometry.EndPoint = new System.Windows.Point(250, 50 + 15 * i);
myPath.Data = myGeometry;
Grid1.Children.Add(myPath);
}
}
}
}
(1)myGraph myG = new myGraph();で変数myGをmyGraph型で生成します。
(2)myG.SetMyC();で独自定義に色配列を設定します。
(3)myG.myLine(Grid1);で直線を引きます。
感想:
(1)独自のクラスを定義して、独自の色配列を作成すると、折れ線グラフ作成が容易となります。