案例:通过datagridview创建图表。
Datagridview代码(有效):
//Datagrid
data.Location = new Point(200, 75);
data.Size = new Size(600, 400);
data.BackgroundColor = Color.White;
data.Font = new Font("Times New Roman", 14.0f);
//Columns
data.ColumnCount = 2;
data.ColumnHeadersVisible = true;
DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();
columnHeaderStyle.BackColor = Color.White;
columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);
data.ColumnHeadersDefaultCellStyle = columnHeaderStyle;
// Set the column header names.
data.Columns[0].Name = "ID";
data.Columns[1].Name = "Value";
//Width of columns
DataGridViewColumn column = data.Columns[0];
column.Width = 279;
DataGridViewColumn column1 = data.Columns[1];
column1.Width = 278;
//Add datagridview
tabpage2.Controls.Add(data);
datagridview的图片: Datagridview
为了测试我到目前为止已经得到了这个:
如果点击按钮,此代码会更新图表并更新。
private void createChart()
{
chart.Size = new Size(600, 400);
chart.Location = new Point(100, 100);
chart.BackColor = Color.Blue;
chart.BorderlineColor = Color.Black;
chart.Series.Add("test");
chart.Series["test"].ChartType = SeriesChartType.Bar;
Button button = new Button();
button.Size = new Size(50,50);
button.Text = "moi";
button.Location = new Point(0,0);
button.Click += (sender, args) =>
{
updateChart();
};
tabpage3.Controls.Add(button);
tabpage3.Controls.Add(chart);
}
此方法更新图表:
private void updateChart()
{
chart.Series[0] = new Series();
chart.Series[0].XValueMember = data.Columns[0].DataPropertyName;
chart.Series[0].YValueMembers = data.Columns[1].DataPropertyName;
chart.DataSource = data.DataSource;
}
我想获得这些值的条形图。
我之前从未在C#中使用过图表,感谢您的帮助!