从datagridview制作图表 [英] making chart from datagridview

查看:97
本文介绍了从datagridview制作图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview哪个用户填写它。计算结果并返回到datagridview的只读列。
我想为我的datagridview的两列制作一个图表。但我没有将datagridview保存在表中,因为表中有以前的结果!
有人告诉我创建图表,您应该将datagridview保存到表中,但是在这种情况下,您应该将datagridview转换为datatable,然后清除它!
请告诉我该怎么办! :(
这是我创建图表的代码,但它返回一个空图表!(我的datagridview中有19列)

I have a datagridview which user fill it. the result is calculated and return to read only columns of datagridview. I want to make a chart for two columns of my datagridview. but I don't save datagridview in a table because there is previous results in the table! somebody told me for creating chart you should save your datagridview to a table but in this case you should convert datagridview to datatable and then clear it! please tell me what should I do! :( this is my code for creating chart but it returns a empty chart!( I have 19 columns in my datagridview)

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 finalproject
{
    public partial class Time_chart_Form : Form
    {
    private DataGridView DGV;
    public Time_chart_Form(DataGridView DGV)
    {
        InitializeComponent();
        this.DGV = DGV;
    }

    private void Time_chart_Form_Load(object sender, EventArgs e)
    {
        using (DataTable table = new DataTable("MyTable"))
        {

            DataRow newRow;
            table.Columns.Add(new DataColumn("Time", typeof(string)));
            table.Columns.Add(new DataColumn("Concentration", typeof(string)));

            int column;
            for (int i = 0; i < this.DGV.Rows.Count; i++)
            {
                column = 0;
                newRow = table.NewRow();

                if (!this.DGV[column, i].FormattedValue.Equals(""))
                    newRow["Time"] = this.DGV[0, i].Value.ToString();
                else
                    newRow["Time"] = null;

                column = 16;
                if (!this.DGV[16, i].FormattedValue.Equals(""))
                    newRow["Concentration"] = this.DGV[column, i].Value.ToString();
                else
                    newRow["Concentration"] = null;

                table.Rows.Add(newRow);

            }

            Chart chart = new Chart();

            chart.Width = 1600;
            chart.Height = 900;

            Series Series = new Series();
            Series.Name = "Series";

            Series.Color = Color.Red;

            Series.BorderColor = Color.FromArgb(255, 0, 0);
            Series.ChartType = SeriesChartType.Point;

            Series.BorderDashStyle = ChartDashStyle.Solid;
            //Series.BorderWidth = 10000;

            Series.ShadowColor = Color.FromArgb(128, 128, 128);
            Series.ShadowOffset = 0;
            Series.BorderColor = Color.FromArgb(0, 0, 0);

            //Chart Area-------------------------

            ChartArea ca1 = new ChartArea();
            ca1.Name = "ChartArea";
            ca1.BackColor = Color.White;
            ca1.BorderColor = Color.FromArgb(255, 255, 255);
            ca1.BorderWidth = 10;
            ca1.BorderDashStyle = ChartDashStyle.Solid;
            ca1.AxisX = new Axis();
            ca1.AxisY = new Axis();

            chart.BackColor = Color.FromArgb(255, 255, 255);
            chart.BackSecondaryColor = Color.White;
            chart.BackGradientStyle = GradientStyle.TopBottom;
            ca1.BackColor = System.Drawing.Color.FromArgb(64, System.Drawing.Color.White);

            chart.ChartAreas.Add(ca1);
            chart.Series.Add(Series);
            chart.Series["Series"].BorderWidth = 500;

            chart.Series["Series"].Points.DataBindXY(table.DefaultView, DGV.Columns[0].Name, table.DefaultView, DGV.Columns[16].Name);

            chart.ChartAreas[0].AxisY.Minimum = -6;
            chart.ChartAreas[0].AxisY.Maximum = 6;
            chart.ChartAreas[0].AxisX.Minimum = 0;
            chart.ChartAreas[0].AxisX.Maximum = 10;
            chart.ChartAreas[0].AxisX.Interval = 1;
            chart.ChartAreas[0].AxisY.Interval = 1;

            chart.Titles.Add(new Title("OverView Plot", Docking.Top, new Font("Verdana", 19.5f, FontStyle.Bold), Color.Black));

            chart.ChartAreas[0].AxisY.Title = "Time";
            chart.ChartAreas[0].AxisX.Title = "Concentration";

            chart.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Verdana", 19, FontStyle.Bold);
            chart.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Verdana", 19, FontStyle.Bold);

            chart.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Verdana", 19.25f, System.Drawing.FontStyle.Bold);
            chart.ChartAreas[0].AxisY.LabelStyle.Font = new System.Drawing.Font("Verdana", 19.25f, System.Drawing.FontStyle.Bold);


            chart.SaveImage(@"C:\Users\Maryam\Documents\Visual Studio 2010\prjchart" + (0).ToString() + ".png", ChartImageFormat.Png);

            //table.Clear();
        }
    }


}

}

推荐答案

可以使用DataBindXY方法将数据系列的X和Y值绑定到数据源,因此我在这种情况下,最适合您使用它。
我相信这里的真正问题不是从DataGridView控件本身中取出数据(只是一些数据的容器,与图表相同),而是也将DataGridView.DataSource用于图表。
不管原始数据源是什么:DataTable,IDbDataReader,csv文件,Excel,Access db等...,请使用该数据源而不是DataGridView进行绑定。

The X and Y values of a data series can be bound to data sources using the DataBindXY method, therefore I agree with your using it as the most appropriate in this case. I believe the real issue here is not taking data out of the DataGridView control itself (just a container for some data, same as the chart), but rather using the DataGridView.DataSource for the chart too. Whatever your original data source is: DataTable, IDbDataReader, csv file, Excel, Access db, ... etc, use that for binding instead of the DataGridView.

// considering you have 
this.DGV = someDataSource;
// use this instead
chart.Series["Series"].Points.DataBindXY(someDataSource, "Time", someDataSource, "Concentration");
//lose the additional DataTable creation

这篇关于从datagridview制作图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆