概要
cscの作法、調べてみた。
積み上げ縦棒グラフやってみた。
写真
コンパイル手順
>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%
>csc ch4.cs /r:System.Windows.Forms.DataVisualization.dll
サンプルコード
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() {
var chart1 = new Chart {
Dock = DockStyle.Fill,
};
this.Controls.Add(chart1);
string[] legends = new string[] { "グラフ1", "グラフ2" };
string[] xValues = new string[] { "A", "B", "C", "D", "E" };
int[,] yValues = new int[,] {{ 10, 20, 30, 40, 50 }, {20, 40, 60, 80, 100}};
chart1.Series.Clear();
chart1.ChartAreas.Clear();
chart1.Titles.Clear();
foreach (var item in legends)
{
chart1.Series.Add(item);
chart1.Series[item].ChartType = SeriesChartType.StackedColumn;
chart1.Series[item].LegendText = item;
}
for (int i = 0; i < xValues.Length; i++)
{
for (int j = 0; j < yValues.GetLength(0); j++)
{
DataPoint dp = new DataPoint();
dp.SetValueXY(xValues[i], yValues[j, i]);
dp.IsValueShownAsLabel = true;
chart1.Series[legends[j]].Points.Add(dp);
}
}
ChartArea area1 = new ChartArea();
area1.AxisX.Title = "Title-XAxis";
area1.AxisY.Title = "Title-YAxis";
Title title1 = new Title("Title1");
chart1.Titles.Add(title1);
chart1.ChartAreas.Add(area1);
Legend legend = new Legend();
chart1.Legends.Add(legend);
legend.Docking = Docking.Top;
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
}
以上。