c#使用System.Windows.Forms.DataVisualization.Charting.dll绘制图表实例
首先下载System.Windows.Forms.DataVisualization.Charting.dll,然后引用到项目中
手动在代码中创建chart类型并将其添加到某个控件中(control.controls.add(chart)),然后参数初始化添加样式和数据就可以了。
以下是效果图和代码
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 test_chart
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
InitChart();
for (int i = 0; i < 3; i++)
{
AddSeries(r.Next(1,100).ToString(), Color.Red);
}
}
static int range = 0;
Random r = new Random(6);
private void AddSeries(string seriersName, Color serierscolor)
{
Series series = new Series(seriersName);
//图表类型 设置为样条图曲线
series.ChartType = SeriesChartType.Line;
series.IsXValueIndexed = true;
series.XValueType = ChartValueType.Time;
series.MarkerStyle = MarkerStyle.Circle;
series.MarkerColor = Color.Black;
//设置点的大小
series.MarkerSize = 5;
//设置曲线的颜色
series.Color = serierscolor;
//设置曲线宽度
series.BorderWidth = 2;
series.CustomProperties = "PointWidth=2";
series.IsValueShownAsLabel = true;
chart.Series.Add(series);
}
private void CreateChart()
{
chart = new Chart();
this.panel1.Controls.Add(chart);
chart.Dock = DockStyle.Fill;
chart.Visible = true;
ChartArea chartArea = new ChartArea();
//chartArea.Name = "FirstArea";
chartArea.CursorX.IsUserEnabled = true;
chartArea.CursorX.IsUserSelectionEnabled = true;
chartArea.CursorX.SelectionColor = Color.SkyBlue;
chartArea.CursorY.IsUserEnabled = true;
chartArea.CursorY.AutoScroll = true;
chartArea.CursorY.IsUserSelectionEnabled = true;
chartArea.CursorY.SelectionColor = Color.SkyBlue;
chartArea.CursorX.IntervalType = DateTimeIntervalType.Auto;
chartArea.AxisX.ScaleView.Zoomable = false;
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.All;//启用X轴滚动条按钮
chartArea.BackColor = Color.BlueViolet; //背景色
chartArea.BackSecondaryColor = Color.White; //渐变背景色
chartArea.BackGradientStyle = GradientStyle.TopBottom; //渐变方式
chartArea.BackHatchStyle = ChartHatchStyle.None; //背景阴影
chartArea.BorderDashStyle = ChartDashStyle.NotSet; //边框线样式
chartArea.BorderWidth = 1; //边框宽度
chartArea.BorderColor = Color.Black;
//chartArea.AxisX.
chartArea.AxisX.MajorGrid.Enabled = true;
chartArea.AxisY.MajorGrid.Enabled = true;
// Axis
chartArea.AxisY.Title = @"Value";
chartArea.AxisY.LineWidth = 2;
chartArea.AxisY.LineColor = Color.Black;
chartArea.AxisY.Enabled = AxisEnabled.True;
chartArea.AxisX.Title = @"Time";
chartArea.AxisX.IsLabelAutoFit = true;
chartArea.AxisX.LabelAutoFitMinFontSize = 5;
chartArea.AxisX.LabelStyle.Angle = -15;
chartArea.AxisX.LabelStyle.IsEndLabelVisible = true; //show the last label
chartArea.AxisX.Interval = 10;
chartArea.AxisX.IntervalAutoMode = IntervalAutoMode.FixedCount;
chartArea.AxisX.IntervalType = DateTimeIntervalType.NotSet;
chartArea.AxisX.TextOrientation = TextOrientation.Auto;
chartArea.AxisX.LineWidth = 2;
chartArea.AxisX.LineColor = Color.Black;
chartArea.AxisX.Enabled = AxisEnabled.True;
chartArea.AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Months;
chartArea.AxisX.Crossing = 0;
chartArea.Position.Height = 85;
chartArea.Position.Width = 85;
chartArea.Position.X = 0;
chartArea.Position.Y = 13;
chart.ChartAreas.Add(chartArea);
chart.BackGradientStyle = GradientStyle.TopBottom;
//图表的边框颜色、
chart.BorderlineColor = Color.FromArgb(26, 59, 105);
//图表的边框线条样式
chart.BorderlineDashStyle = ChartDashStyle.Solid;
//图表边框线条的宽度
chart.BorderlineWidth = 2;
//图表边框的皮肤
chart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
}
bool flag = false;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "Zoom")
{
flag = true;
}
else if (comboBox1.SelectedItem.ToString() == "OverView" || comboBox1.SelectedItem.ToString() == "Follow")
{
comboBox1.Items.Remove("Zoom");
flag = false;
}
}
private void button_Stop_Click(object sender, EventArgs e)
{
switch (button_Stop.Text)
{
case "Stop":
{
button_Stop.Text = "Start";
t.Stop();
break;
}
case "Start":
{
button_Stop.Text = "Stop";
t.Start();
break;
}
}
}
private void InitChart()
{
CreateChart();
chart.ChartAreas[0].AxisX.ScaleView.Scroll(ScrollType.Last);
t.Interval = 100;
t.Start();
}
int sum = 0;
private void t_Tick(object sender, EventArgs e)
{
sum++;
Random ra = new Random();
DateTime nowTime = DateTime.Now;
if (chart.Series.Count <= 0)
{
return;
}
for (int i = 0; i < chart.Series.Count; i++)
{
Series series = chart.Series[i];
string value = ra.Next(1, 10).ToString();
series.Points.AddXY(nowTime, value);
if (comboBox1.SelectedItem.ToString() == "OverView")
{
chart.ChartAreas[0].AxisX.ScaleView.Position = 1;
if (sum > 10)
{
double max = chart.ChartAreas[0].AxisX.Maximum;
max = (sum / 10 + 1) * 10;
chart.ChartAreas[0].AxisX.Interval = max / 10;
}
chart.ChartAreas[0].AxisX.ScaleView.Size = sum * 1.1;
}
if (comboBox1.SelectedItem.ToString() == "Follow")
{
chart.ChartAreas[0].AxisX.Interval = 1D;
chart.ChartAreas[0].AxisX.ScaleView.Size = 10D;
if (sum <= chart.ChartAreas[0].AxisX.ScaleView.Size)
chart.ChartAreas[0].AxisX.ScaleView.Position = 1;
else
chart.ChartAreas[0].AxisX.ScaleView.Position = sum - chart.ChartAreas[0].AxisX.ScaleView.Size;
}
}
}
}
}
//设计器
namespace test_chart
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.button_Stop = new System.Windows.Forms.Button();
this.t = new System.Windows.Forms.Timer(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"Follow",
"OverView"});
this.comboBox1.Location = new System.Drawing.Point(840, 429);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 1;
this.comboBox1.Text = "Follow";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// button_Stop
//
this.button_Stop.Location = new System.Drawing.Point(12, 429);
this.button_Stop.Name = "button_Stop";
this.button_Stop.Size = new System.Drawing.Size(75, 23);
this.button_Stop.TabIndex = 2;
this.button_Stop.Text = "Stop";
this.button_Stop.UseVisualStyleBackColor = true;
this.button_Stop.Click += new System.EventHandler(this.button_Stop_Click);
//
// t
//
this.t.Tick += new System.EventHandler(this.t_Tick);
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(12, 13);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(906, 410);
this.panel1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(973, 461);
this.Controls.Add(this.panel1);
this.Controls.Add(this.button_Stop);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataVisualization.Charting.Chart chart;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button_Stop;
private System.Windows.Forms.Timer t;
private System.Windows.Forms.Panel panel1;
}
}