在面板上绘制直方图

本文关键字:绘制 直方图 | 更新日期: 2023-09-27 18:10:35

谁能告诉我如何在面板上绘制直方图?直方图使用的数据来自sql语句。获取数据的函数是

 ArrayList getGrupe()
        {
            SqlConnection conn = new SqlConnection("server=localhost;" +
                         "Trusted_Connection=yes;database=erp;connection timeout=30");
            ArrayList listaGrupe = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select distinct(grupa) from [erp].[dbo].Clienti";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                conn.Open();
                SqlDataReader reader;
                reader = cmd.ExecuteReader();
                if (reader.HasRows)
                    listaGrupe = new ArrayList();
                while (reader.Read())
                {
                    listaGrupe.Add(reader[0].ToString());
                }
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
            return listaGrupe;
        }
        ArrayList getVanzariGrupa(string grupa)
        {
            SqlConnection conn = new SqlConnection("server=localhost;" +
                 "Trusted_Connection=yes;database=erp;connection timeout=30");
            ArrayList vanzari = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select sum(f.cantitate*lp.pret)+" +
                "(sum(f.cantitate*lp.pret)*24/100) as Vanzari_totale From " + 
                " [erp].[dbo].Facturi_iesire f, [erp].[dbo].Clienti c, " + 
                " [erp].[dbo].Arhiva_comenzi a, [erp].[dbo].Produse p, " + 
                " [erp].[dbo].Lista_preturi lp " + 
                " where f.id_arhiva_comenzi=a.id_arhiva_comenzi and " + 
                " a.cod_produs=p.cod_produs and p.cod_produs=lp.cod_produs and " + 
                "lp.id_lista=c.id_lista and c.grupa='"  +
                 grupa.ToString() + "' Group by c.nume;";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conn;
                conn.Open();
                SqlDataReader reader;
                reader = cmd.ExecuteReader();
                 if (reader.HasRows)
                    vanzari = new ArrayList();
                while (reader.Read())
                 {
                    vanzari.Add(reader[0].ToString());
                }
            }
            catch (Exception)
            {
                return null;
            }
             finally
            {
                conn.Close();
            }
             return vanzari;
        }

GetGrupe()将表示条形条的数量,getVanzariGrupa()获取每个条形条的数据,因此每个条形条的高度将取决于getVarnzariGrupa()为每个条形条返回的值,所以我认为使用2 foreach是可以的。像这样

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        grupe = new ArrayList();
        grupe=getGrupe();
        foreach (string grupa in grupe)
        {
            ArrayList date = getVanzariGrupa(grupa);
            foreach (String vz in date)
            {
                //proccesing the data to draw the histogram ???
            }
        }
    }

请帮助。谢谢:)

在面板上绘制直方图

可以编写Paint事件以使用

e.Graphics.FillRectangle(gBrush, x, y, w, h);

创建一个非常简单的直方图。你可以用

e.Graphics.DrawString(groups[g], someFont, gBrush, panel1.Width - marging, g*20f);

绘制最小图例。真正的工作是让margin, x, y, w, h的值正确…

这不是一个好主意!绘制直方图是工具箱中数据控件部分中漂亮且现成的Chart控件的工作。在Form上放置一个代替Panel,命名为groupChart

使用它只需要如下代码:

private void button_charts_Click(object sender, EventArgs e)
{
    // I recommend changing this too, say List<string>
    ArrayList grupe = getGrupe();
    // clear the chart
    groupChart.Series.Clear();
    // loop over the groups..
    for (int g = 0; g < groups.Count; g++)
    {
        // add a series and configure it..
        // I simply set it to be of type column:
        Series ser = groupChart.Series.Add(groups[g]);
        ser.ChartType = SeriesChartType.Column;
        // now get your list of values..
        // you pick the data type!!
        List<int> date = getVanzariGrupa(groups[g]); //*
        for (int v = 0; v < date .Count; v++)
        {
            ser.Points.AddXY(v, date [v]);
        }
    }
}

。您可以通过多种方式配置Chart,如刻度、标签等。很值得一看

要使其工作,请至少去掉'date'的Arrraylist !改变这个

ArrayList getVanzariGrupa(string grupa)
这些:

ArrayList vanzari = null;
vanzari = new ArrayList();

:

List<T> getVanzariGrupa(string grupa)

:

 List<T> vanzari = new List<T>();
 vanzari = new List<T>();

对于'T',您只需从sum()子句中插入数据类型;对于float,它是:选择List<float>。您可以看到泛型集合在工作中的奇妙灵活性!

编辑://*这在第一个版本中是错误的!