ASP.NET 图表控件 - 如何创建此条形图?

发布于 2024-08-29 22:02:25 字数 420 浏览 7 评论 0原文

我想从数据表中制作一个图表控件。

该表格如下所示:

alt text http://www.freeimagehosting.net/uploads/5d02ce1558.png

我想要的图表将如下所示:

''' 
''''
'''''       '' '  
'''''       '' '
ECCTMP      ECCTMP       ECCTMP   
Monday      Tuesday      Wednesday

希望这对于按类型(电子邮件、电话)分组的每一天都有意义。

我现在才确定如何对其进行数据绑定?

比利

Got a chart control i wanna make from a data table.

the table looks like this:

alt text http://www.freeimagehosting.net/uploads/5d02ce1558.png

the chart i want will look like this:

''' 
''''
'''''       '' '  
'''''       '' '
ECCTMP      ECCTMP       ECCTMP   
Monday      Tuesday      Wednesday

hope this makes sense for each day its grouped b y the type (email, calls).

I'm just now sure how to databind it?

Billy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带上头具痛哭 2024-09-05 22:02:25

如果您希望在条形图中对系列进行分组,那么您需要使用 Chart.DataBindTable 方法 (MSDN)。

只需添加以下代码:

Chart1.DataBindTable(IEtable, "Day");

这将生成一个如下所示的图表:
alt text

这是一些用作测试的虚拟代码:

DataTable table = new DataTable();
table.Columns.Add("Day", typeof(string));
table.Columns.Add("Email", typeof(int));
table.Columns.Add("Calls", typeof(int));
table.Columns.Add("Contacts", typeof(int));
table.Columns.Add("Tasks", typeof(int));
table.Columns.Add("Meetings", typeof(int));
table.Columns.Add("Proposals", typeof(int));

table.Rows.Add("Monday", 1, 3, 3, 4, 5, 5);
table.Rows.Add("Tuesday", 1,6,8,2,0,3);
table.Rows.Add("Wednesday", 7, 6,3,0,2,1);
table.Rows.Add("Thursday", 1,5,5,9,3,1);
table.Rows.Add("Friday", 4,7,3,5,2,3);

//convert datatable to a IEnumerable form
var IEtable = (table as System.ComponentModel.IListSource).GetList();

//Bind the datatable to the chart using the DataBindTable method
Chart1.DataBindTable(IEtable, "Day");

也可以让标签按照您使用 ECCTMP 的描述显示但添加图例可能看起来更干净。

If you're looking to group series in a bar chart then you'll need to use the Chart.DataBindTable method (MSDN).

Just add the following code:

Chart1.DataBindTable(IEtable, "Day");

This will produce a chart that looks something like the following:
alt text

Here's some dummy code to use as a test:

DataTable table = new DataTable();
table.Columns.Add("Day", typeof(string));
table.Columns.Add("Email", typeof(int));
table.Columns.Add("Calls", typeof(int));
table.Columns.Add("Contacts", typeof(int));
table.Columns.Add("Tasks", typeof(int));
table.Columns.Add("Meetings", typeof(int));
table.Columns.Add("Proposals", typeof(int));

table.Rows.Add("Monday", 1, 3, 3, 4, 5, 5);
table.Rows.Add("Tuesday", 1,6,8,2,0,3);
table.Rows.Add("Wednesday", 7, 6,3,0,2,1);
table.Rows.Add("Thursday", 1,5,5,9,3,1);
table.Rows.Add("Friday", 4,7,3,5,2,3);

//convert datatable to a IEnumerable form
var IEtable = (table as System.ComponentModel.IListSource).GetList();

//Bind the datatable to the chart using the DataBindTable method
Chart1.DataBindTable(IEtable, "Day");

It is also possible to have the labels appear as you describe with ECCTMP but adding a legend will probably look cleaner.

谷夏 2024-09-05 22:02:25
protected void Page_Load(object sender, EventArgs e)
    {

        Title tl = new Title("Players Score Card");
        tl.Font = new System.Drawing.Font("vardana",12);
        //chrtGroup.Titles.Add(tl);
        //ChartArea a = new ChartArea("players");
        //a.AxisX.Title = "Player's Yearwise";
        //a.AxisY.Title = "Scores";
        //chrtGroup.ChartAreas.Add(a);


        DataTable dt = new DataTable();
        dt.Columns.Add("Years", typeof(int));
        dt.Columns.Add("Afridi", typeof(int));
        dt.Columns.Add("Akmal", typeof(int));
        dt.Columns.Add("Nasir", typeof(int));
        dt.Columns.Add("Shoib",typeof(int));
        dt.Columns.Add("Hafiz", typeof(int));


        Random rn = new Random();
        for (int i = 1; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr["Years"] = "200" +i;
            dr["Afridi"] = 700 + rn.Next(200,800);
            dr["Akmal"] = 500 + rn.Next(200,800);
            dr["Nasir"] = 400 + rn.Next(200,800);
            dr["Shoib"] = 800 + rn.Next(300,500);
            dr["Hafiz"] = 200 + rn.Next(200, 900);
            dt.Rows.Add(dr);
        }

        Series afridi = new Series("Afridi");
        Series akmal = new Series("Akmal");
        Series nasir = new Series("Nasir");
        Series shoib = new Series("Shoib");
        Series hafiz = new Series("Hafiz");

        afridi.IsValueShownAsLabel = true;
        akmal.IsValueShownAsLabel = true;
        nasir.IsValueShownAsLabel = true;
        shoib.IsValueShownAsLabel = true;
        hafiz.IsValueShownAsLabel = true;

        foreach (DataRow r in dt.Rows)
        {
            afridi.Points.AddXY(Convert.ToDouble(r["Years"]),Convert.ToDouble(r["Afridi"]));
            akmal.Points.AddXY(Convert.ToDouble(r["Years"]), Convert.ToDouble(r["Akmal"]));
            nasir.Points.AddXY(Convert.ToDouble(r["Years"]),Convert.ToDouble(r["Nasir"]));
            shoib.Points.AddXY(Convert.ToDouble(r["Years"]), Convert.ToDouble(r["Shoib"]));
            hafiz.Points.AddXY(Convert.ToDouble(r["Years"]), Convert.ToDouble(r["Hafiz"]));
        }

        chrtGroup.Series.Add(afridi);
        chrtGroup.Series.Add(akmal);
        chrtGroup.Series.Add(nasir);
        chrtGroup.Series.Add(shoib);
        chrtGroup.Series.Add(hafiz);

        chrtGroup.Legends.Add(new Legend("Afridi"));
        chrtGroup.Legends.Add(new Legend("Akmal"));
        chrtGroup.Legends.Add(new Legend("Nasir"));
        chrtGroup.Legends.Add(new Legend("Shoib"));
        chrtGroup.Legends.Add(new Legend("Hafiz"));
    }
}
protected void Page_Load(object sender, EventArgs e)
    {

        Title tl = new Title("Players Score Card");
        tl.Font = new System.Drawing.Font("vardana",12);
        //chrtGroup.Titles.Add(tl);
        //ChartArea a = new ChartArea("players");
        //a.AxisX.Title = "Player's Yearwise";
        //a.AxisY.Title = "Scores";
        //chrtGroup.ChartAreas.Add(a);


        DataTable dt = new DataTable();
        dt.Columns.Add("Years", typeof(int));
        dt.Columns.Add("Afridi", typeof(int));
        dt.Columns.Add("Akmal", typeof(int));
        dt.Columns.Add("Nasir", typeof(int));
        dt.Columns.Add("Shoib",typeof(int));
        dt.Columns.Add("Hafiz", typeof(int));


        Random rn = new Random();
        for (int i = 1; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr["Years"] = "200" +i;
            dr["Afridi"] = 700 + rn.Next(200,800);
            dr["Akmal"] = 500 + rn.Next(200,800);
            dr["Nasir"] = 400 + rn.Next(200,800);
            dr["Shoib"] = 800 + rn.Next(300,500);
            dr["Hafiz"] = 200 + rn.Next(200, 900);
            dt.Rows.Add(dr);
        }

        Series afridi = new Series("Afridi");
        Series akmal = new Series("Akmal");
        Series nasir = new Series("Nasir");
        Series shoib = new Series("Shoib");
        Series hafiz = new Series("Hafiz");

        afridi.IsValueShownAsLabel = true;
        akmal.IsValueShownAsLabel = true;
        nasir.IsValueShownAsLabel = true;
        shoib.IsValueShownAsLabel = true;
        hafiz.IsValueShownAsLabel = true;

        foreach (DataRow r in dt.Rows)
        {
            afridi.Points.AddXY(Convert.ToDouble(r["Years"]),Convert.ToDouble(r["Afridi"]));
            akmal.Points.AddXY(Convert.ToDouble(r["Years"]), Convert.ToDouble(r["Akmal"]));
            nasir.Points.AddXY(Convert.ToDouble(r["Years"]),Convert.ToDouble(r["Nasir"]));
            shoib.Points.AddXY(Convert.ToDouble(r["Years"]), Convert.ToDouble(r["Shoib"]));
            hafiz.Points.AddXY(Convert.ToDouble(r["Years"]), Convert.ToDouble(r["Hafiz"]));
        }

        chrtGroup.Series.Add(afridi);
        chrtGroup.Series.Add(akmal);
        chrtGroup.Series.Add(nasir);
        chrtGroup.Series.Add(shoib);
        chrtGroup.Series.Add(hafiz);

        chrtGroup.Legends.Add(new Legend("Afridi"));
        chrtGroup.Legends.Add(new Legend("Akmal"));
        chrtGroup.Legends.Add(new Legend("Nasir"));
        chrtGroup.Legends.Add(new Legend("Shoib"));
        chrtGroup.Legends.Add(new Legend("Hafiz"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文