下面给一个自己做的例子:
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;
namespace MSChart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
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;
}
}
}
最后显示的图片:
string fileName = "C:\\chartdata.mdb";
Chart1.Width = 600; //图表宽度
Chart1.Height = 400; //图表高度
Chart1.BackColor = Color.Azure; //图表背景色
Chart1.Titles.Add("房地产"); //图表标题
//新建连接
using (OleDbConnection con = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileName))
{
OleDbCommand cmd = new OleDbCommand("SELECT Name, Sales FROM REPS", con);
con.Open();
OleDbDataReader read = cmd.ExecuteReader();
Chart1.DataBindTable(read, "Name");
read.Close();
}
//注意数据绑定后,它的series 是1 而不是0
Chart1.Series[1].IsValueShownAsLabel = true; //是否显示数据
Chart1.Series[1].IsVisibleInLegend = false; //是否显示数据说明
Chart1.Series[1].MarkerStyle = MarkerStyle.Circle; //线条上的数据点标志类型
Chart1.Series[1].MarkerSize = 8; //标志大小
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.ChartAreas[0].AxisY.Title = "价格"; //Y 轴标题
数据源绑定方式(二)
string fileName = "C:\\chartdata.mdb";
Chart1.Width = 600; //图表宽度
Chart1.Height = 400; //图表高度
Chart1.BackColor = Color.Azure; //图表背景色
Chart1.Titles.Add("房地产"); //图表标题
//新建连接
using (OleDbConnection con = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileName))
{
OleDbCommand cmd = new OleDbCommand("SELECT Name, Sales FROM REPS", con);
con.Open();
Chart1.DataSource = cmd;
Chart1.Series[0].XValueMember = "Name";
Chart1.Series[0].YValueMembers = "Sales";
con.Close();
Chart1.DataBind();
}
数据源绑定(三)
string fileName = "C:\\chartdata.mdb";
Chart1.Width = 600; //图表宽度
Chart1.Height = 400; //图表高度
Chart1.BackColor = Color.Azure; //图表背景色
Chart1.Titles.Add("房地产"); //图表标题
//新建连接
using (OleDbConnection con = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileName))
{
OleDbCommand cmd = new OleDbCommand("SELECT GrossSales FROM SALES WHEREQuarterEnding < #01/01/2002#", con);
con.Open();
OleDbDataReader read = cmd.ExecuteReader();
Chart1.Series[0].Points.DataBindY(read, "GrossSales");
read.Close();
}
数据源绑定(四)
string fileName = "C:\\chartdata.mdb";
Chart1.Width = 600; //图表宽度
Chart1.Height = 400; //图表高度
Chart1.BackColor = Color.Azure; //图表背景色
Chart1.Titles.Add("房地产"); //图表标题
//新建连接
using (OleDbConnection con = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DataSource=" + fileName))
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM REPSALES", con);
con.Open();
OleDbDataReader read = cmd.ExecuteReader();
Chart1.DataBindCrossTable(read,
"Name",
"Year",
"Sales",
"Label=Commissions{C}");
read.Close();
}
数据源绑定(五)
// Align series using their X axis labels
Chart1.AlignDataPointsByAxisLabel();
Chart1.ChartAreas[0].Area3DStyle.Enable3D = true; //展示3D
// Initialize arrays for series 1
double[] yval1 = { 2, 6, 5 };
string[] xval1 = { "Peter", "Andrew", "Julie" };
// Initialize arrays for series 2
double[] yval2 = { 4, 5, 3 };
string[] xval2 = { "Peter", "Andrew", "Dave" };
// Initialize arrays for series 3
double[] yval3 = { 6, 5 };
string[] xval3 = { "Julie", "Mary" };
// Bind the arrays to each data series
Chart1.Series["Series1"].Points.DataBindXY(xval1, yval1);
Chart1.Series["Series2"].Points.DataBindXY(xval2, yval2);
Chart1.Series["Series3"].Points.DataBindXY(xval3, yval3);
// Align series using their X axis labels
Chart1.AlignDataPointsByAxisLabel();
Chart1.ChartAreas[0].Area3DStyle.Enable3D = true; //展示3D