2010-04-21 11 views
6

Ho ottenuto un controllo grafico che voglio fare da una tabella di dati.Controlli del grafico ASP.NET: come posso creare questo grafico a barre?

tavolo assomiglia a questo:

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

il grafico che voglio sarà simile a questa:

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

speranza questo ha un senso per ogni giorno la sua raggruppati per tipo (e-mail, chiamate).

Ora sono sicuro di come collegarlo?

Billy

+0

Puoi fornire maggiori dettagli sulla struttura di backend del datatable? –

risposta

5

Se stai cercando di serie del gruppo in un grafico a barre, allora avrete bisogno di utilizzare il metodo Chart.DataBindTable (MSDN).

basta aggiungere il seguente codice:

Chart1.DataBindTable(IEtable, "Day"); 

Questo produrrà un grafico che sembra qualcosa di simile al seguente: alt text

Ecco alcuni codice fittizio da usare come 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"); 

È anche possibile visualizzare le etichette come descritto con ECCTMP, ma l'aggiunta di una legenda sarà probabilmente più pulita.

+0

Questo codice sembra fantastico grazie, non vedo l'ora di provarlo. Ti farò sapere come vado avanti! – iamjonesy

+1

Felice di sentire che sta funzionando per voi. È anche possibile cambiare le etichette come hai scritto nella tua domanda in modo che appaiano come "ECCTMP Monday" ma ho pensato che la Legend avrebbe funzionato altrettanto bene per te. –

+0

@Alison, è istogramma giusto, se voglio lo stesso nel grafico a barre, allora come lo farò? Ho provato lo stesso codice ma genera un errore "Assi area grafico: l'area del grafico contiene tipi di grafici incompatibili. Ad esempio, grafici a barre e istogrammi non possono esistere nella stessa area del grafico." Credo che manchi qualcosa nel mark up. – Rishi

0
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")); 
    } 
} 
Problemi correlati