https://youtu.be/I5mPS-N4D_8
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;
namespace SampleCode
{
public partial class ChartForm : Form
{
System.Windows.Forms.Timer ChartTimer = new System.Windows.Forms.Timer();
public ChartForm()
{
InitializeComponent();
}
private void ChartForm_Load(object sender, EventArgs e)
{
chart1.Series.Clear();
radioButton1.Checked = true;
ChartTimer.Interval = 500;
ChartTimer.Tick += new EventHandler(ChartTimer_Tick);
ChartTimer.Enabled = true ;
}
private void ChartTimer_Tick(object sender, EventArgs e)
{
Random rand = new Random();
int rI = rand.Next(100);
try
{
chart1.Series["yData"].Points.AddY(rI);
}
catch
{
chart1.Series.Add("yData");
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if(chart1.Series.Count==0) return ;
/*
// method 1
RadioButton rButton = sender as RadioButton;
if(rButton==radioButton1)
chart1.Series["yData"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
else if(rButton==radioButton2)
chart1.Series["yData"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
else if(rButton==radioButton3)
chart1.Series["yData"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Spline;
else if(rButton==radioButton4)
chart1.Series["yData"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
*/
// method 2
RadioButton rButton = sender as RadioButton;
// chart1.Series["yData"].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), rButton.Text);
chart1.Series["yData"].ChartType = StringToEnum<SeriesChartType>(rButton.Text);
}
private T StringToEnum<T>(string str)
{
return (T)Enum.Parse(typeof(T), str);
}
private void button1_Click(object sender, EventArgs e)
{
chart1.Series.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
ChartTimer.Enabled = !ChartTimer.Enabled;
}
private void button3_Click(object sender, EventArgs e)
{
ColorDialog cDlg = new ColorDialog();
if(cDlg.ShowDialog() == DialogResult.Cancel) return ;
button3.BackColor = cDlg.Color;
chart1.Series["yData"].Color = cDlg.Color;
}
private void button4_Click(object sender, EventArgs e)
{
ColorDialog cDlg = new ColorDialog();
if(cDlg.ShowDialog() == DialogResult.Cancel) return ;
button4.BackColor = cDlg.Color;
chart1.BackColor = cDlg.Color;
}
private void radioButton8_CheckedChanged(object sender, EventArgs e)
{
RadioButton rButton = sender as RadioButton;
chart1.Series["yData"].Palette = StringToEnum<ChartColorPalette>(rButton.Text);
}
private void radioButton12_CheckedChanged(object sender, EventArgs e)
{
RadioButton rButton = sender as RadioButton;
chart1.Legends[0].Alignment = StringToEnum<StringAlignment>(rButton.Text);
}
private void button5_Click(object sender, EventArgs e)
{
chart1.Series["yData"].IsVisibleInLegend = !chart1.Series["yData"].IsVisibleInLegend;
if(chart1.Series["yData"].IsVisibleInLegend) chart1.Series["yData"].LegendToolTip = "yData Chart" ;
}
private void button6_Click(object sender, EventArgs e)
{
Button button = sender as Button;
chart1.Legends[0].Docking = StringToEnum<Docking>(button.Text);
}
private void button10_Click(object sender, EventArgs e)
{
chart1.Series["yData"].IsValueShownAsLabel = !chart1.Series["yData"].IsValueShownAsLabel;
}
private void button11_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
fontDialog.Font = chart1.Series["yData"].Font;
if(fontDialog.ShowDialog()==DialogResult.Cancel) return ;
chart1.Series["yData"].Font = fontDialog.Font;
chart1.ChartAreas[0].AxisX.LabelStyle.Font = fontDialog.Font;
chart1.ChartAreas[0].AxisY.LabelStyle.Font = fontDialog.Font;
}
private void button12_Click(object sender, EventArgs e)
{
ColorDialog cDlg = new ColorDialog();
if(cDlg.ShowDialog()==DialogResult.Cancel) return ;
button12.BackColor = cDlg.Color;
chart1.Series["yData"].BorderColor = cDlg.Color;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int angle;
int.TryParse(textBox1.Text, out angle);
if(angle<-90 || angle>90) return ;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = angle ;
chart1.ChartAreas[0].AxisY.LabelStyle.Angle = angle ;
}
}
}