ついでに印刷ボタンも追加
コンパイルは
csc ChartTest.cs /r:System.Windows.Forms.DataVisualization.dll
ソースはこんな感じ
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
public class ChartTest:Form
{
private Chart chart;
private PrintDocument pd;
public ChartTest()
{
this.SuspendLayout();
Button btn = new Button();
btn.Text = "印刷";
btn.Location = new Point(10,420);
btn.Click += new EventHandler(OKBtn_Click);
Button btn2 = new Button();
btn2.Text = "チャートタイプ変更";
btn2.Location = new Point(150,420);
btn2.Size = new Size(200,btn2.Size.Height);
btn2.Click += new EventHandler(OKBtn2_Click);
Size = new Size(500,500);
SetChart();
Controls.Add(btn);
Controls.Add(btn2);
this.ResumeLayout();
}
//印刷ボタン押下時の処理
void OKBtn_Click (Object sender,
EventArgs e){
pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
//変更ボタン押下時の処理
void OKBtn2_Click (Object sender,
EventArgs e){
chart.Series[0].ChartType = SeriesChartType.Bar;
}
//印刷ページ設定
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Size s = chart.Size;
Bitmap bmp = new Bitmap(s.Width,s.Height);
Graphics g = ev.Graphics;
chart.DrawToBitmap(bmp,new Rectangle(0,0,s.Width,s.Height));
g.DrawImage(bmp,0,0);
}
//チャート画面の作成
private void SetChart()
{
//チャート画面の作成
chart = new Chart();
chart.ChartAreas.Add(new ChartArea("chart1"));
chart.Size = new Size(400,400);
Controls.Add(chart);
//グラフの画面
Series series;
chart.Series.Add(new Series());
series = chart.Series[0];
series.ChartArea = "chart1";
series.ChartType = SeriesChartType.Line;
//データの作成
int ii;
for(ii = 1; ii < 10 ; ii++)
{
DataPoint dp = new DataPoint(ii,ii*2);
series.Points.Add(dp);
}
}
public static int Main(string[] args)
{
ChartTest ct = new ChartTest();
ct.ShowDialog();
return 0;
}
}
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
public class ChartTest:Form
{
private Chart chart;
private PrintDocument pd;
public ChartTest()
{
this.SuspendLayout();
Button btn = new Button();
btn.Text = "印刷";
btn.Location = new Point(10,420);
btn.Click += new EventHandler(OKBtn_Click);
Button btn2 = new Button();
btn2.Text = "チャートタイプ変更";
btn2.Location = new Point(150,420);
btn2.Size = new Size(200,btn2.Size.Height);
btn2.Click += new EventHandler(OKBtn2_Click);
Size = new Size(500,500);
SetChart();
Controls.Add(btn);
Controls.Add(btn2);
this.ResumeLayout();
}
//印刷ボタン押下時の処理
void OKBtn_Click (Object sender,
EventArgs e){
pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
//変更ボタン押下時の処理
void OKBtn2_Click (Object sender,
EventArgs e){
chart.Series[0].ChartType = SeriesChartType.Bar;
}
//印刷ページ設定
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
Size s = chart.Size;
Bitmap bmp = new Bitmap(s.Width,s.Height);
Graphics g = ev.Graphics;
chart.DrawToBitmap(bmp,new Rectangle(0,0,s.Width,s.Height));
g.DrawImage(bmp,0,0);
}
//チャート画面の作成
private void SetChart()
{
//チャート画面の作成
chart = new Chart();
chart.ChartAreas.Add(new ChartArea("chart1"));
chart.Size = new Size(400,400);
Controls.Add(chart);
//グラフの画面
Series series;
chart.Series.Add(new Series());
series = chart.Series[0];
series.ChartArea = "chart1";
series.ChartType = SeriesChartType.Line;
//データの作成
int ii;
for(ii = 1; ii < 10 ; ii++)
{
DataPoint dp = new DataPoint(ii,ii*2);
series.Points.Add(dp);
}
}
public static int Main(string[] args)
{
ChartTest ct = new ChartTest();
ct.ShowDialog();
return 0;
}
}
0 件のコメント:
コメントを投稿