解决方案 »

  1.   

    注意看 Data 属性那里。你觉得那个值是数据吗?
      

  2.   

    加载了一个Excel文件进入DataGridView
    ---------
    你怎么加载的?目测是OLEDB
    应该是先把EXCEL读到datatable里,然后绑定,对吧chart需要输入数据,你的数据应该是从datatable里提取出来的(遍历,把你想显示的数据放数组)
      

  3.   

    对,现在DataGridView里已经有数据了,怎么把它作为chart的数据源呢
      

  4.   

    我第一次接触chart,不懂啊,照着例子和自己的理解来做的
      

  5.   

    我第一次接触chart,不懂啊,照着例子和自己的理解来做的
    我觉得你的问题不是不懂chart,而是根本没明白程序是如何取数据的,而数据集和控件又是什么关系
    读EXCEL的功能也是网上代码直接扒下来的,自己完全没理解吧
      

  6.   

    哎,你首要条件是先把Excel的数据放到DataTable中,然后才能附给Data
      

  7.   

    datagirdview里面已经有数据表了啊
      

  8.   

    参考: Working with chart in C#
    鉴于你已经将Excel表格的数据添加到DataGridView,图表直接使用DataGridView的数据。参考下面的CreateChart函数(以柱形图为例)。using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;namespace DataGridViewChart
    {
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public class MainForm : Form
    {
    /// <summary>
    /// Designer variable used to keep track of non-visual components.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Disposes resources used by the form.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
    if (disposing) {
    if (components != null) {
    components.Dispose();
    }
    }
    base.Dispose(disposing);
    }

    /// <summary>
    /// This method is required for Windows Forms designer support.
    /// Do not change the method contents inside the source code editor. The Forms designer might
    /// not be able to load this method if it was changed manually.
    /// </summary>
    private void InitializeComponent()
    {
    System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
    System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
    System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
    this.dataGridView1 = new System.Windows.Forms.DataGridView();
    this.colName = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.colAge = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.colScore = new System.Windows.Forms.DataGridViewTextBoxColumn();
    this.btnLoadData = new System.Windows.Forms.Button();
    this.btnCreateChart = new System.Windows.Forms.Button();
    this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
    this.SuspendLayout();
    // 
    // dataGridView1
    // 
    this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
    this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
    this.colName,
    this.colAge,
    this.colScore});
    this.dataGridView1.Location = new System.Drawing.Point(12, 42);
    this.dataGridView1.Name = "dataGridView1";
    this.dataGridView1.RowTemplate.Height = 23;
    this.dataGridView1.Size = new System.Drawing.Size(280, 207);
    this.dataGridView1.TabIndex = 0;
    // 
    // colName
    // 
    this.colName.HeaderText = "Name";
    this.colName.Name = "colName";
    // 
    // colAge
    // 
    this.colAge.HeaderText = "Age";
    this.colAge.Name = "colAge";
    this.colAge.Width = 50;
    // 
    // colScore
    // 
    this.colScore.HeaderText = "Score";
    this.colScore.Name = "colScore";
    this.colScore.Width = 80;
    // 
    // btnLoadData
    // 
    this.btnLoadData.Location = new System.Drawing.Point(13, 13);
    this.btnLoadData.Name = "btnLoadData";
    this.btnLoadData.Size = new System.Drawing.Size(75, 23);
    this.btnLoadData.TabIndex = 1;
    this.btnLoadData.Text = "加载数据";
    this.btnLoadData.UseVisualStyleBackColor = true;
    this.btnLoadData.Click += new System.EventHandler(this.BtnLoadDataClick);
    // 
    // btnCreateChart
    // 
    this.btnCreateChart.Location = new System.Drawing.Point(95, 13);
    this.btnCreateChart.Name = "btnCreateChart";
    this.btnCreateChart.Size = new System.Drawing.Size(75, 23);
    this.btnCreateChart.TabIndex = 2;
    this.btnCreateChart.Text = "创建图表";
    this.btnCreateChart.UseVisualStyleBackColor = true;
    this.btnCreateChart.Click += new System.EventHandler(this.BtnCreateChartClick);
    // 
    // chart1
    // 
    chartArea1.Name = "ChartArea1";
    this.chart1.ChartAreas.Add(chartArea1);
    legend1.Name = "Legend1";
    this.chart1.Legends.Add(legend1);
    this.chart1.Location = new System.Drawing.Point(298, 12);
    this.chart1.Name = "chart1";
    series1.ChartArea = "ChartArea1";
    series1.Legend = "Legend1";
    series1.Name = "Score";
    this.chart1.Series.Add(series1);
    this.chart1.Size = new System.Drawing.Size(274, 237);
    this.chart1.TabIndex = 3;
    this.chart1.Text = "chart1";
    // 
    // MainForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(584, 261);
    this.Controls.Add(this.chart1);
    this.Controls.Add(this.btnCreateChart);
    this.Controls.Add(this.btnLoadData);
    this.Controls.Add(this.dataGridView1);
    this.Name = "MainForm";
    this.Text = "DataGridViewChart";
    ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
    this.ResumeLayout(false);
    }
    private System.Windows.Forms.DataGridViewTextBoxColumn colScore;
    private System.Windows.Forms.DataGridViewTextBoxColumn colAge;
    private System.Windows.Forms.DataGridViewTextBoxColumn colName;
    private System.Windows.Forms.Button btnCreateChart;
    private System.Windows.Forms.Button btnLoadData;
            private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
    private System.Windows.Forms.DataGridView dataGridView1;

    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }

    public void LoadData()
    {
    try
    {
    dataGridView1.Rows.Add("alex stewart", 24, 56);
    dataGridView1.Rows.Add("chris harris", 34, 76);
    dataGridView1.Rows.Add("frank smith", 21, 46);
    dataGridView1.Rows.Add("henry paul", 27, 66);
    dataGridView1.Rows.Add("lan bishop", 31, 59);
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }

    public void CreateChart()
    {
    //先清空现有Points
    this.chart1.Series["Score"].Points.Clear();
    try {
    for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
    {
    //向Series添加数据,AddXY的第一个参数是X轴的值,第二个参数是Y轴的值
    this.chart1.Series["Score"].Points.AddXY(
    dataGridView1.Rows[i].Cells[0].Value.ToString(),
    Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value.ToString())
    );
    }
    } catch (Exception ex) {

    MessageBox.Show(ex.Message);
    }
    }

    void BtnLoadDataClick(object sender, EventArgs e)
    {
    LoadData();
    }

    void BtnCreateChartClick(object sender, EventArgs e)
    {
    CreateChart();
    }
    }
    }
      

  9.   

    Quote: 引用 11 楼 save4me 的回复:

    参考: Working with chart in C#
    鉴于你已经将Excel表格的数据添加到DataGridView,图表直接使用DataGridView的数据。参考下面的CreateChart函数(以柱形图为例)。
    chart1.Series["Score"].中的score是什么啊
      

  10.   

    score是我的示例里面柱状图的X轴序列的名字,你可以换成你自己需要的值。下图标示我是如何设置的
      

  11.   


            private void fillChart(DataTable dt)
            {
                if (rbPie.Checked)//饼图
                {
                    chart1.Series[0].ChartType = SeriesChartType.Pie;
                    chart1.Series[0].IsVisibleInLegend = true;
                    chart1.Series[0].IsValueShownAsLabel = true;                var query = dt.Select("").ToList();                chart1.Series[0].Points.DataBind(query, "DAState", "Num", "LegendText=DAState");
                  //  this.chart1.Series[0].LegendText += "#PERCENT{P}"; 
                    chart1.Series[0].Label = "#LEGENDTEXT:#VAL{D}";
                    //鼠标停留显示
                    chart1.Series[0].ToolTip = "#PERCENT{P}";
                }
                else
                {
                    chart1.Series[0].ChartType = SeriesChartType.Column;
                    chart1.Series[0].IsVisibleInLegend = false;
                    chart1.Series[0].IsValueShownAsLabel = true;                var query = dt.Select("").ToList();
                    chart1.Series[0].Points.DataBind(query, "DAState", "Num", "LegendText=DAState");
                    chart1.Series[0].Label = "#VAL{D}";
                    //鼠标停留显示
                    chart1.Series[0].ToolTip = "#PERCENT{P}";                chart1.ChartAreas[0].AxisY.Title = "数量";
                    chart1.ChartAreas[0].AxisY.TitleAlignment = StringAlignment.Far;                chart1.ChartAreas[0].AxisX.Title = "状态";
                    chart1.ChartAreas[0].AxisX.TitleAlignment = StringAlignment.Far;
                    chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;            }
            }