先新建一个项目 选择 Windows窗体应用程序
工具箱中选择Chart
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data.SqlClient;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class zhuzhuangtu : Form
{
public zhuzhuangtu()
{
InitializeComponent();
}
private void zhuzhuangtu_Load(object sender, EventArgs e)
{
// 数组
// Initialize arrays for series 1
/** double[] yval1 = { 2, 6, 5 };
string[] xval1 = { "Peter", "Andrew", "Julie" };
// Bind the arrays to each data series
Chart1.Series["Series1"].Points.DataBindXY(xval1, yval1);
Chart1.Series["Series1"].ChartType = SeriesChartType.Line;
// Align series using their X axis labels
//Chart1.AlignDataPointsByAxisLabel();
//Chart1.ChartAreas[0].Area3DStyle.Enable3D = true; //展示3D */
int[] date = new int[] { 20, 40, 50, 80, 30, 10, 60 };
int[] test = new int[] { 10, 80, 70, 40, 20, 50, 90 };
Chart1.Width = 600; //图片宽度
Chart1.Height = 400; //图片高度
Chart1.BackColor = Color.Azure; //图片背景色
//建一个图表集合
Series series = new Series("test");
series.ChartType = SeriesChartType.Column; //图标集类型,Line为直线,SpLine为曲线
series.Color = Color.Green; //线条颜色
series.BorderWidth = 2; //线条宽度
series.ShadowOffset = 1; //阴影宽度
series.IsVisibleInLegend = false; //是否显示数据说明
series.IsValueShownAsLabel = true;
series.MarkerStyle = MarkerStyle.Diamond; //线条上的数据点标志类型
series.MarkerSize = 8; // 标志的大小
DateTime date1 = DateTime.Now.Date;
for (int i = 0; i < date.Length; i++)
{
series.Points.AddXY(date1, date[i]);
date1 = date1.AddDays(1);
}
Chart1.Series.Add(series); //把数据集添加到Chart1中
//再建一个图表集合
Series series1 = new Series("ok");
series1.ChartType = SeriesChartType.Column; //图标集类型,Line为直线,SpLine为曲线
series1.Color = Color.Red; //线条颜色
series1.BorderWidth = 2; //线条宽度
series1.ShadowOffset = 1; //阴影宽度
series1.IsVisibleInLegend = false; //是否显示数据说明
series1.IsValueShownAsLabel = true;
series1.MarkerStyle = MarkerStyle.Diamond;
series1.MarkerSize = 8;
DateTime date2 = DateTime.Now.Date;
for (int i = 0; i < test.Length; i++)
{
series1.Points.AddXY(date2, test[i]);
date2 = date2.AddDays(1);
}
Chart1.Series.Add(series1); //把数据集添加到Chart1中
//设置坐标轴
Chart1.ChartAreas[0].AxisX.LineColor = Color.Blue;
Chart1.ChartAreas[0].AxisY.LineColor = Color.Blue;
Chart1.ChartAreas[0].AxisX.LineWidth = 2;
Chart1.ChartAreas[0].AxisY.LineWidth = 2;
Chart1.ChartAreas[0].AxisY.Title = "总额";
//设置网格线
Chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Blue;
Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Blue;
}
//跳转到另一个窗口
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
this.Hide();
form.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace WindowsFormsApplication3
{
public partial class zhexiantu : Form
{
public zhexiantu()
{
InitializeComponent();
}
private void zhexiantu_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“db1DataSet3.shebei”中。您可以根据需要移动或删除它。
this.shebeiTableAdapter1.Fill(this.db1DataSet3.shebei);
string fileName ="E:\\WindowsFormsApplication3\\db1.mdb" ; // (数据库路径)
Chart1.Width = 600; //图表宽度
Chart1.Height = 400; //图表高度
Chart1.BackColor = Color.Azure; //图表背景色
Chart1.Titles.Add("设备管理"); //图表标题
string str=this.db1DataSet3.Tables[0].Rows[0]["Pname"].ToString();
////新建连接
using (OleDbConnection con = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName))
{
OleDbCommand cmd = new OleDbCommand("SELECT ID, Pname FROM shebei", con);
con.Open();
OleDbDataReader read = cmd.ExecuteReader();
Chart1.Series["Series1"].Points.DataBindXY(read, "ID", read, "Pname");
// Chart1.DataBindTable(read,"Pname");
read.Close();
}
Chart1.ChartAreas[0].AxisX.LineColor = Color.Blue; //X轴颜色
Chart1.ChartAreas[0].AxisY.LineColor = Color.Blue; //Y轴颜色
Chart1.ChartAreas[0].AxisX.LineWidth = 2; //X轴宽度
Chart1.ChartAreas[0].AxisY.LineWidth = 2; //Y轴宽度
Chart1.Series["Series1"].ChartType = SeriesChartType.Line; // 图表类型为折线图
// Chart1.Series["Series1"].ChartType = SeriesChartType.Pie; //图表类型图饼图
}
private void button1_Click(object sender, EventArgs e) //跳转到另一个窗口
{
Form1 form = new Form1();
this.Hide();
form.Show();
}
}
}