Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
40
Help us understand the problem. What is going on with this article?
@tobira-code

Chartコントロールを使う

More than 5 years have passed since last update.

はじめに

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);
        }
    }
}
40
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
40
Help us understand the problem. What is going on with this article?