在面板上绘制直方图

时间:2014-06-28 14:24:06

标签: c# sql-server panel

有人能告诉我如何在面板上绘制直方图吗?用于直方图的数据来自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()获取每个条形图的数据,因此每个条形图的高度将取决于每个grupa从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 ???
            }
        }
    }

请帮忙。谢谢:))

1 个答案:

答案 0 :(得分:0)

可以编写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控件的作用。在表单上放置一个而不是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()子句中插入所需的数据类型;对于浮动它是:选择List<float>。您可以看到通用集合的灵活性在工作中!

编辑: // *第一个版本出错!