29
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chartコントロールを使う

Last updated at Posted at 2015-06-28

#はじめに
ChartコントロールはVisualStudioに標準搭載されているグラフ用のコントロールです。
基本的な使い方を記載します。

#構成要素
Chartコントロールは次の要素から構成されます。

  • Titles(タイトル)
  • Series(系列)
  • ChartAreas(チャートエリア;複数チャートを作成するときに使用する)
// Titles,Series,ChartAreasはchartコントロール直下のメンバ
chart1.Titles.Clear();
chart1.Series.Clear();
chart1.ChartAreas.Clear();

以下の要素はSeriesに含まれます。

  • ChartType(チャートタイプ;折れ線グラフ、棒グラフなど)
  • Legend(凡例)
Series seriesLine = new Series();
seriesLine.ChartType = SeriesChartType.Line; // 折れ線グラフ
seriesLine.LegendText = "Legend:Line";       // 凡例

以下の要素はChartAreasに含まれます。

  • Axis Title(軸タイトル)
chart1.ChartAreas.Add(new ChartArea("Area1"));            // ChartArea作成
chart1.ChartAreas["Area1"].AxisX.Title = "Title-X-Axis";  // X軸タイトル設定
chart1.ChartAreas["Area1"].AxisY.Title = "Title-Y-Axis";  // Y軸タイトル設定

##例1.折れ線グラフ、棒グラフ、タイトルあり、軸タイトルあり、凡例あり
rei1.JPG

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;
using System.Windows.Forms.DataVisualization.Charting;

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

            // clear
            chart1.Series.Clear();
            chart1.ChartAreas.Clear();
            chart1.Titles.Clear();

            Title title1 = new Title("Title1");

            // series
            Random rdm = new Random();
            Series seriesLine = new Series();
            seriesLine.ChartType = SeriesChartType.Line;
            seriesLine.LegendText = "Legend:Line";
            seriesLine.BorderWidth = 2;
            seriesLine.MarkerStyle = MarkerStyle.Circle;
            seriesLine.MarkerSize = 12;
            for (int i = 0; i < 10; i++)
            {
                seriesLine.Points.Add(new DataPoint(i, rdm.Next(0, 210)));
            }

            Series seriesColumn = new Series();
            seriesColumn.LegendText = "Legend:Column";
            seriesColumn.ChartType = SeriesChartType.Column;
            for (int i = 0; i < 10; i++)
            {
                seriesColumn.Points.Add(new DataPoint(i, rdm.Next(0, 210)));
            }

            // chartarea
            ChartArea area1 = new ChartArea();
            area1.AxisX.Title = "Title-XAxis";
            area1.AxisY.Title = "Title-YAxis";

            chart1.Titles.Add(title1);
            chart1.ChartAreas.Add(area1);
            chart1.Series.Add(seriesColumn);
            chart1.Series.Add(seriesLine);
        }
    }
}

##例2.複数ChartArea、折れ線グラフ、棒グラフ、タイトルあり、軸タイトルあり、凡例あり
rei2.JPG

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;
using System.Windows.Forms.DataVisualization.Charting;

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

            // clear
            chart1.Series.Clear();
            chart1.ChartAreas.Clear();
            chart1.Titles.Clear();

            // chartarea
            ChartArea area1 = new ChartArea("Area1");
            area1.AxisX.Title = "Title1-XAxis";
            area1.AxisY.Title = "Title1-YAxis";

            ChartArea area2 = new ChartArea("Area2");
            area2.AxisX.Title = "Title2-XAxis";
            area2.AxisY.Title = "Title2-YAxis";

            Title title = new Title("Title");
            Title title1 = new Title("Title1");
            Title title2 = new Title("Title2");
            title1.DockedToChartArea = "Area1"; // ChartAreaとの紐付
            title2.DockedToChartArea = "Area2"; // ChartAreaとの紐付

            // series
            Random rdm = new Random();
            Series seriesLine = new Series();
            seriesLine.ChartType = SeriesChartType.Line;
            seriesLine.LegendText = "Legend:Line";
            seriesLine.BorderWidth = 2;
            seriesLine.MarkerStyle = MarkerStyle.Circle;
            seriesLine.MarkerSize = 12;
            for (int i = 0; i < 10; i++)
            {
                seriesLine.Points.Add(new DataPoint(i, rdm.Next(0, 210)));
            }
            seriesLine.ChartArea = "Area1"; // ChartAreaとの紐付

            Series seriesColumn = new Series();
            seriesColumn.LegendText = "Legend:Column";
            seriesColumn.ChartType = SeriesChartType.Column;
            for (int i = 0; i < 10; i++)
            {
                seriesColumn.Points.Add(new DataPoint(i, rdm.Next(0, 210)));
            }
            seriesColumn.ChartArea = "Area2"; // ChartAreaとの紐付

            chart1.Titles.Add(title);
            chart1.Titles.Add(title1);
            chart1.Titles.Add(title2);
            chart1.ChartAreas.Add(area1);
            chart1.ChartAreas.Add(area2);
            chart1.Series.Add(seriesColumn);
            chart1.Series.Add(seriesLine);
        }
    }
}
29
45
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?