cscの作法 その70
概要
cscの作法、調べてみた。
mdbにsql投げて合計求めて、円グラフにしてみた。
参考にしたページ
写真
コンパイル手順
>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%
>csc pie0.cs /platform:x86 /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;
using System.Data.OleDb;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form {
public Form1() {
ClientSize = new Size(900, 600);
var chart1 = new Chart {
Dock = DockStyle.Fill,
};
this.Controls.Add(chart1);
Series mySeries = new Series("series");
int[] data = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
string[] moku = new string[] { "光熱・水道", "家具・家事用品", "教養娯楽", "住居", "保健医療", "交通・通信", "交際費", "非消費支出(直接税・社会保険料)", "食料", "被服及び履物", "雑費", "貯蓄" };
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
con.Open();
for (int i = 0; i < 11; i++)
{
string sSQL = "SELECT SUM(金額) FROM sisyutu WHERE 項目 = '" + moku[i] + "'";
OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sSQL, con);
object sum = cmd.ExecuteScalar();
if (sum != DBNull.Value)
{
int k = Convert.ToInt32(sum);
Console.WriteLine(k);
data[i] = k;
}
}
mySeries.Points.DataBindXY(moku, data);
mySeries.ChartType = SeriesChartType.Pie;
mySeries["PieLabelStyle"] = "Inside";
mySeries["PieDrawingStyle"] = "Default";
ChartArea myArea = new ChartArea("area");
chart1.ChartAreas.Add("area");
chart1.Series.Add(mySeries);
mySeries.ChartArea = "area";
chart1.Legends.Add(new Legend("Default"));
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderText = "";
chart1.Legends["Default"].CellColumns.Add(firstColumn);
LegendCellColumn percentColumn = new LegendCellColumn();
percentColumn.Text = "#PERCENT";
percentColumn.HeaderText = "Percentage";
percentColumn.Name = "nameColumn";
chart1.Legends["Default"].CellColumns.Add(percentColumn);
chart1.Legends["Default"].LegendStyle = LegendStyle.Table;
chart1.Legends["Default"].TableStyle = LegendTableStyle.Tall;
chart1.Legends["Default"].DockedToChartArea = "area";
chart1.Legends["Default"].IsDockedInsideChartArea = false;
chart1.Legends["Default"].Docking = Docking.Right;
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
}
>set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%
>csc pie0.cs /platform:x86 /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;
using System.Data.OleDb;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form {
public Form1() {
ClientSize = new Size(900, 600);
var chart1 = new Chart {
Dock = DockStyle.Fill,
};
this.Controls.Add(chart1);
Series mySeries = new Series("series");
int[] data = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
string[] moku = new string[] { "光熱・水道", "家具・家事用品", "教養娯楽", "住居", "保健医療", "交通・通信", "交際費", "非消費支出(直接税・社会保険料)", "食料", "被服及び履物", "雑費", "貯蓄" };
OleDbConnection con = new OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
con.Open();
for (int i = 0; i < 11; i++)
{
string sSQL = "SELECT SUM(金額) FROM sisyutu WHERE 項目 = '" + moku[i] + "'";
OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sSQL, con);
object sum = cmd.ExecuteScalar();
if (sum != DBNull.Value)
{
int k = Convert.ToInt32(sum);
Console.WriteLine(k);
data[i] = k;
}
}
mySeries.Points.DataBindXY(moku, data);
mySeries.ChartType = SeriesChartType.Pie;
mySeries["PieLabelStyle"] = "Inside";
mySeries["PieDrawingStyle"] = "Default";
ChartArea myArea = new ChartArea("area");
chart1.ChartAreas.Add("area");
chart1.Series.Add(mySeries);
mySeries.ChartArea = "area";
chart1.Legends.Add(new Legend("Default"));
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderText = "";
chart1.Legends["Default"].CellColumns.Add(firstColumn);
LegendCellColumn percentColumn = new LegendCellColumn();
percentColumn.Text = "#PERCENT";
percentColumn.HeaderText = "Percentage";
percentColumn.Name = "nameColumn";
chart1.Legends["Default"].CellColumns.Add(percentColumn);
chart1.Legends["Default"].LegendStyle = LegendStyle.Table;
chart1.Legends["Default"].TableStyle = LegendTableStyle.Tall;
chart1.Legends["Default"].DockedToChartArea = "area";
chart1.Legends["Default"].IsDockedInsideChartArea = false;
chart1.Legends["Default"].Docking = Docking.Right;
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
}
}
以上。
Author And Source
この問題について(cscの作法 その70), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/c0f5f2c9669661eedb39著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .