I am building datatable dynamically and the format is like following,
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);
dr["Question"] = "2D";
dr["User1"] = "1";
dr["Question"] = "3D";
dr["User1"] = "4";
dr["Question"] = "2D";
dr["User2"] = "2";
dr["Question"] = "3D";
dr["User2"] = "5";
How can I arrange is it like following,
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);
dr["Question"] = "2D";
dr["User1"] = "1";
dr["User2"] = "2";
dr["Question"] = "3D";
dr["User1"] = "4";
dr["User2"] = "5";
You can make linq cross join and filter it to list (you can bind list to your msChart)
var result = (from dr1 in dt.Select()
join dr2 in dt.Select() on dr1["Question"].ToString() equals dr2["Question"].ToString()
select new { q = dr1["question"], u1 = dr1["User1"], u2 = dr2["User2"] }
).Where(row => row.u1.ToString().Length > 0 && row.u2.ToString().Length > 0).ToList();
You could use code similar to this;
DataRow dr = dt.NewRow();
dr["Question"] = "2D";
dr["User1"] = "1";
dr["User2"] = "2";
dr = dt.NewRow();
dr["Question"] = "3D";
dr["User1"] = "4";
dr["User2"] = "5";
Supposing that dr is a variable of type DataRow obtained calling the DataTable method NewRow, then you can add the values to the row ItemArray property in just one line.
Of course you need to be absolutely sure of your columns order and type
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"Q1", "1", "2"};
dt.Rows.Add(dr);
dr = dt.NewRow();
dr.ItemArray = new object[] {"Q2", "3", "4"};
dt.Rows.Add(dr);
Related
I am selecting data from SQL into datagrid, the code works fine, but it is deleting the previous data from my datagrid.
My code (C# WPF SQL datagrid):
private void enter(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
SqlConnection com = new SqlConnection("Server = localhost; database = PrimaSOFT ; integrated security = true");
SqlCommand cmd = new SqlCommand("produktit", com);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Barkodi", txtBarkodi.Text);
//Created a new DataTable
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
dc.ColumnName = "Barkodi";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.String");
dc2.ColumnName = "Emertimi";
DataColumn dc3 = new DataColumn();
dc3.DataType = System.Type.GetType("System.Decimal");
dc3.ColumnName = "Sasia";
DataColumn dc4 = new DataColumn();
dc4.DataType = System.Type.GetType("System.Decimal");
dc4.ColumnName = "Cmimi";
DataColumn dc5 = new DataColumn();
dc5.DataType = System.Type.GetType("System.String");
dc5.Caption = "sds";
dc5.ColumnName = "TVSH";
DataColumn dc6 = new DataColumn();
dc6.DataType = System.Type.GetType("System.String");
dc6.ColumnName = "Total";
dc6.Expression = "Cmimi * Sasia";//Multiplying the Price and Quantity DataColumns
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc2);
dataTable.Columns.Add(dc3);
dataTable.Columns.Add(dc4);
dataTable.Columns.Add(dc5);
dataTable.Columns.Add(dc6);
dtgartikujt.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
com.Open();//Open the SQL connection
SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
while (reader.Read())//For each row that the SQL query returns do
{
DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
dr[1] = reader[1];
dr[2] = reader[2];
dr[3] = reader[3];
dr[4] = reader[4];
dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
}
}
}
Sum code
object sumObject;
sumObject = dataTable.Compute("Sum(Totali)",
string.Empty);
txttotali.Text = sumObject.ToString();
I am expecting a new data to be showed in datagrid without deleting previous data.
How should I approach this?
Edited: I also attached the sum code, to sum the datagrid column, and display to textbox
As I think I understand what you are doing : separate dataTable code from Sql(ADO.NET) code and run dataTable part once only. So for run CreateTable in form_load for example :
public void CreateTable()
{
DataColumn dc = new DataColumn();//Made a new DataColumn to populate above DataTable
dc.DataType = System.Type.GetType("System.String");//Defined the DataType inside, this can be [[int]] if you want.
dc.ColumnName = "Barkodi";//Gave it a name (important for the custom expression - can only be one word so use underscores if you need multiple words)
DataColumn dc2 = new DataColumn();
dc2.DataType = System.Type.GetType("System.String");
dc2.ColumnName = "Emertimi";
DataColumn dc3 = new DataColumn();
dc3.DataType = System.Type.GetType("System.Decimal");
dc3.ColumnName = "Sasia";
DataColumn dc4 = new DataColumn();
dc4.DataType = System.Type.GetType("System.Decimal");
dc4.ColumnName = "Cmimi";
DataColumn dc5 = new DataColumn();
dc5.DataType = System.Type.GetType("System.String");
dc5.Caption = "sds";
dc5.ColumnName = "TVSH";
DataColumn dc6 = new DataColumn();
dc6.DataType = System.Type.GetType("System.String");
dc6.ColumnName = "Total";
dc6.Expression = "Cmimi * Sasia";//Multiplying the Price and Quantity DataColumns
dataTable.Columns.Add(dc);//Add them to the DataTable
dataTable.Columns.Add(dc2);
dataTable.Columns.Add(dc3);
dataTable.Columns.Add(dc4);
dataTable.Columns.Add(dc5);
dataTable.Columns.Add(dc6);
dtgartikujt.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
}
And the rest of code :
long sum=0;
private void enter(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
SqlConnection com = new SqlConnection("Server = localhost; database = PrimaSOFT ; integrated security = true");
SqlCommand cmd = new SqlCommand("produktit", com);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#Barkodi", txtBarkodi.Text);
//Created a new DataTable
SqlDataAdapter da = new SqlDataAdapter(cmd);
com.Open();//Open the SQL connection
SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
while (reader.Read())//For each row that the SQL query returns do
{
DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
dr[1] = reader[1];
dr[2] = reader[2];
dr[3] = reader[3];
dr[4] = reader[4];
// sum+= Convert.ToInt64(reader[4]); // use the total index instead of 4 . or use reader["total"];
dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
// or maybe dtgartikujt.Rows.Add(dr);
}
// textbox1.Text=sum.Tostring();
}
}
I was trying to generate winform line chart from data table but UI does not look good.
see the UI
also see my full sample code. I believe I am missing something in code for which chart UI does not look good. I do not understand for what reason a white box is coming in chart?
please see my full code and tell me what I need to add or rectify in my code.
code
private void show()
{
DataRow dr = null;
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("data", typeof(Int32));
dt.Columns.Add("CountryCode", typeof(string));
dr = dt.NewRow();
dr[0] = DateTime.Parse("01/01/2017");
dr[1] = 03;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("02/01/2017");
dr[1] = 09;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("03/01/2017");
dr[1] = 15;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("04/01/2017");
dr[1] = 22;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("05/01/2017");
dr[1] = 13;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("06/01/2017");
dr[1] = 22;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("07/01/2017");
dr[1] = 07;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("08/01/2017");
dr[1] = 11;
dr[2] = "GB";
dt.Rows.Add(dr);
Chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
//Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
Chart1.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
Chart1.BorderlineWidth = 3;
Chart1.BackColor = Color.NavajoWhite;
Chart1.ChartAreas.Add("chtArea");
Chart1.ChartAreas[0].AxisX.Title = "Category Name";
Chart1.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
Chart1.ChartAreas[0].AxisY.Title = "UnitPrice";
Chart1.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
Chart1.ChartAreas[0].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
Chart1.ChartAreas[0].BorderWidth = 2;
Chart1.Legends.Add("UnitPrice");
Chart1.Series.Add("UnitPrice");
//Chart1.Series[0].Palette = ChartColorPalette.Bright;
Chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
Chart1.Series[0].Points.DataBindXY(dt.DefaultView, "Date", dt.DefaultView, "Data");
//Chart1.Series[0].IsVisibleInLegend = true;
Chart1.Series[0].IsValueShownAsLabel = true;
Chart1.Series[0].ToolTip = "Data Point Y Value: #VALY{G}";
// Setting Line Width
Chart1.Series[0].BorderWidth = 3;
Chart1.Series[0].Color = Color.Red;
// Setting Line Shadow
//Chart1.Series[0].ShadowOffset = 5;
//Legend Properties
Chart1.Legends[0].LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Table;
Chart1.Legends[0].TableStyle = System.Windows.Forms.DataVisualization.Charting.LegendTableStyle.Wide;
Chart1.Legends[0].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
Chart1.Width = 488;
Chart1.Height = 345;
}
Thanks
A Chart comes with a few default elements, like a ChartArea, a Series and a Legend.
If you want to create a ChartArea of your own, why not clear the default ChartArea first? Chart1.ChartAreas.Clear(); Then (and only then) your will actually access your newly created one with the Chart1.ChartAreas[0] references.
I also suggest to use short and nicely named references to Series, ChartAreas etc..:
ChartArea chtArea = Chart1.ChartAreas.Add("chtArea");
Series unitPrice = Chart1.Series.Add("UnitPrice");
This makes the code more compact and clearer..
issue solved. this line causing problem Chart1.ChartAreas.Add("chtArea"); so i removed this line.
my fresh code
private void show()
{
DataRow dr = null;
DataTable dt = new DataTable();
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("data", typeof(Int32));
dt.Columns.Add("CountryCode", typeof(string));
dr = dt.NewRow();
dr[0] = DateTime.Parse("01/01/2017");
dr[1] = 30;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("02/01/2017");
dr[1] = 09;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("03/01/2017");
dr[1] = 15;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("04/01/2017");
dr[1] = 22;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("05/01/2017");
dr[1] = 13;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("06/01/2017");
dr[1] = 22;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("07/01/2017");
dr[1] = 07;
dr[2] = "GB";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = DateTime.Parse("08/01/2017");
dr[1] = 11;
dr[2] = "GB";
dt.Rows.Add(dr);
//Chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
////Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
//Chart1.BorderlineColor = System.Drawing.Color.FromArgb(26, 59, 105);
//Chart1.BorderlineWidth = 3;
//Chart1.BackColor = Color.NavajoWhite;
//Chart1.ChartAreas.Add("chtArea");
Chart1.ChartAreas[0].AxisX.Title = "NPS Dates";
Chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -60;
Chart1.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
Chart1.ChartAreas[0].AxisY.Title = "NPS Values";
Chart1.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Verdana", 11, System.Drawing.FontStyle.Bold);
Chart1.ChartAreas[0].BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
Chart1.ChartAreas[0].BorderWidth = 2;
//Chart1.Legends.Add("UnitPrice");
//Chart1.Series.Add("UnitPricexxx");
//Chart1.Series[0].Palette = ChartColorPalette.Bright;
Chart1.Series[0].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
Chart1.Series[0].Points.DataBindXY(dt.DefaultView, "Date", dt.DefaultView, "Data");
//Chart1.Series[0].IsVisibleInLegend = true;
Chart1.Series[0].IsValueShownAsLabel = true;
Chart1.Series[0].ToolTip = "Data Point Y Value: #VALY{G}";
// Setting Line Width
Chart1.Series[0].BorderWidth = 3;
Chart1.Series[0].Color = Color.Red;
// Setting Line Shadow
//Chart1.Series[0].ShadowOffset = 5;
//Legend Properties
Chart1.Legends[0].LegendStyle = System.Windows.Forms.DataVisualization.Charting.LegendStyle.Table;
Chart1.Legends[0].TableStyle = System.Windows.Forms.DataVisualization.Charting.LegendTableStyle.Wide;
Chart1.Legends[0].Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
Chart1.Width = 488;
Chart1.Height = 345;
}
I am trying to merge two datarow cells into one. Here is my initializing code:
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col2";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col3";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col4";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.String");
dc.ColumnName = "Col5";
dt.Columns.Add(dc);
This is the code I have for each column in Excel:
dr = dt.NewRow();
dr["Col1"] = "";
dr["Col2"] = "Quarter 1";
dr["Col3"] = "";
dr["Col4"] = "";
dr["Col5"] = "";
dt.Rows.Add(dr);
And it will look like this:
But I want it to look like this:
Any thoughts or ideas on how I can do this? I found it to be rather hard to merge the cells.
I want to add custom label something like that ( Category 1 and Category 2 ) but not getting idea whether I need to add new series or using custom label function.. Any help would be much appreciated.
Here is my Data Table,
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Section";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User2";
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["Question"] = "A";
dr["Section"] = "Category 1";
dr["User1"] = "1";
dr["User2"] = "2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "B";
dr["Section"] = "Category 1";
dr["User1"] = "4";
dr["User2"] = "5";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "C";
dr["Section"] = "Category 1";
dr["User1"] = "2";
dr["User2"] = "5";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "X";
dr["Section"] = "Category 2";
dr["User1"] = "3";
dr["User2"] = "5";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "Y";
dr["Section"] = "Category 2";
dr["User1"] = "5";
dr["User2"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "Z";
dr["Section"] = "Category 2";
dr["User1"] = "6";
dr["User2"] = "5";
dt.Rows.Add(dr);
My Chart plotting code,
Series Series = new Series("Series1");
Chart4.Series.Add("Series1");
Chart4.Series.Add("Series2");
Chart4.ChartAreas.Add("ChartArea1");
// Set series chart type
Chart4.Series["Series1"].ChartType = SeriesChartType.Bar;
Chart4.Series["Series2"].ChartType = SeriesChartType.Bar;
// Draw as 3D Cylinder
Chart4.Series["Series1"]["DrawingStyle"] = "Cylinder";
Chart4.Series["Series2"]["DrawingStyle"] = "Cylinder";
Chart4.DataSource = dt.DefaultView;
Chart4.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Chart4.Series["Series1"].XValueMember = "Question";
Chart4.Series["Series1"].YValueMembers = "User1";
Chart4.Series["Series2"].YValueMembers = "User2";
Chart4.DataBind();
Solution
DataTable dt = new DataTable();
DataColumn dc;
dt.Columns.Add("Id", typeof(int));
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Section";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "User1";
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["Question"] = "AA";
dr["Id"] = 1;
dr["Section"] = "Category1";
dr["User1"] = "6";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "BB";
dr["Id"] = 2;
dr["Section"] = "Category1";
dr["User1"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "XX";
dr["Id"] = 3;
dr["Section"] = "Category2";
dr["User1"] = "3";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "YY";
dr["Id"] = 4;
dr["Section"] = "Category2";
dr["User1"] = "4";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Question"] = "ZZ";
dr["Id"] = 5;
dr["Section"] = "Category2";
dr["User1"] = "2";
dt.Rows.Add(dr);
Series Series = new Series("Series1");
Chart4.Series.Add("Series1");
Chart4.ChartAreas.Add("ChartArea1");
Chart4.Series["Series1"].ChartType = SeriesChartType.Bar;
Chart4.Series["Series1"]["BarLabelStyle"] = "Center";
Chart4.Series["Series1"]["DrawingStyle"] = "Cylinder";
Chart4.Series["Series1"].XValueMember = "Question";
Chart4.Series["Series1"].YValueMembers = "User1";
Chart4.DataSource = dt;
Chart4.DataBind();
foreach (var g in dt.AsEnumerable().GroupBy(x => x.Field<string>("Section")))
{
string section = g.Key;
var questions = g.Select(r => new { Id = r.Field<int>("Id"), Question = r.Field<string>("Question") });
int min = questions.Min(y => y.Id);
int max = questions.Max(y => y.Id);
var sectionlabel=new CustomLabel();
if (min == max)
{
sectionlabel = new CustomLabel(min - 0.5, max + 0.5, section, 1, LabelMarkStyle.LineSideMark);
}
else
{
sectionlabel = new CustomLabel(min, max, section, 1, LabelMarkStyle.LineSideMark);
}
Chart4.ChartAreas[0].AxisX.CustomLabels.Add(sectionlabel);
}
Chart4.ChartAreas["ChartArea1"].AxisX.Interval = 1;
Have you tried this? It will create a group label for items 1-3.
Chart4.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(1, 3, "Category 1", 1, LabelMarkStyle.LineSideMark);
The easiest way is to just add Annotations to your chart
TextAnnotation text = new TextAnnotation();
text.Text = 'Category 1';
text.X = 20;
text.Y = 20;
Chart4.Annotations.Add(text);
You can also draw the line using a LineAnnotation
http://msdn.microsoft.com/en-us/library/dd456725.aspx
I'm trying multi series bar chart based on "Name" but it's throwing an error
The chart area contains incompatible chart types. For example, bar charts and column charts cannot exist in the same chart area.
Here is my code:
Chart1.Series["Series1"].ChartType = SeriesChartType.Bar;
Chart1.Series["Series1"]["DrawingStyle"] = "Emboss";
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
Chart1.Series["Series1"].IsValueShownAsLabel = true;
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Marks";
dt.Columns.Add(dc);
DataRow dr;
dr = dt.NewRow();
dr["Name"] = "Fred";
dr["Question"] = "2D";
dr["Marks"] = "54";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Bill";
dr["Question"] = "3D";
dr["Marks"] = "66";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Rhona";
dr["Question"] = "4D";
dr["Marks"] = "32";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Peter";
dr["Question"] = "5D";
dr["Marks"] = "46";
dt.Rows.Add(dr);
var IEtable = (dt as System.ComponentModel.IListSource).GetList();
Chart1.DataBindTable(IEtable, "Name");
and Mark up is,
<asp:Chart ID="Chart1" runat="server" Width="850px" Height="500px" >
<Series>
<asp:series Name="Series1" ChartArea="ChartArea1"></asp:series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
I'm looking for an output like,
Where color bars represents "Names"
This is definitely possible and you'll be able to produce a chart that looks like this:
The following code will get what you need:
ASPX
<asp:Chart ID="Chart1" runat="server" Palette="SeaGreen" BackColor="LightGray" Width="654px">
<Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" AlignmentOrientation="Horizontal">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
C#
//Build Sample Data DataTable
DataTable dt = new DataTable();
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "Name";
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "Question";
dt.Columns.Add(dc);
dt.Columns.Add("Marks", typeof(int));
string question = "2D";
DataRow dr;
dr = dt.NewRow();
dr["Name"] = "Fred";
dr["Question"] = question;
dr["Marks"] = 54;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Bill";
dr["Question"] = question;
dr["Marks"] = 66;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Rhona";
dr["Question"] = question;
dr["Marks"] = 32;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Peter";
dr["Question"] = question;
dr["Marks"] = 46;
dt.Rows.Add(dr);
question = "4D";
dr = dt.NewRow();
dr["Name"] = "Fred";
dr["Question"] = question;
dr["Marks"] = 89;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Bill";
dr["Question"] = question;
dr["Marks"] = 99;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Rhona";
dr["Question"] = question;
dr["Marks"] = 28;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Peter";
dr["Question"] = question;
dr["Marks"] = 44;
dt.Rows.Add(dr);
question = "3D";
dr = dt.NewRow();
dr["Name"] = "Fred";
dr["Question"] = question;
dr["Marks"] = 26;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Bill";
dr["Question"] = question;
dr["Marks"] = 89;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Rhona";
dr["Question"] = question;
dr["Marks"] = 73;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Name"] = "Peter";
dr["Question"] = question;
dr["Marks"] = 14;
dt.Rows.Add(dr);
//Sort the datatable
DataView dv = dt.DefaultView;
dv.Sort = "Question ASC, Name ASC";
dt = dv.ToTable();
DataTable table = new DataTable();
table.Columns.Add("Question", typeof(string));
foreach (DataRow dr2 in dt.Rows)
{
//Add user Names to DataTable table
if (!table.Columns.Contains(dr2["Name"].ToString())) {
table.Columns.Add(dr2["Name"].ToString(), typeof(int));
}
//Add empty Question rows to DataTable
if (table.AsEnumerable().Where(x => x.Field<string>("Question") == dr2["Question"].ToString()).Count() == 0)
{
table.Rows.Add(dr2["Question"].ToString());
}
}
// Loop through all columns and questions and then calculate the mark
for (int i = 1; i < table.Columns.Count;i++ )
{
for (int j = 0; j < table.Rows.Count; j++)
{
string questionName = table.Rows[j][0].ToString();
for (int k = 0; k < dt.Rows.Count; k++)
{
string userName = table.Columns[i].ColumnName;
table.Rows[j][i] = dt.AsEnumerable().Where(x => x.Field<string>("Name") == userName).Where(y=>y.Field<string>("Question") == questionName).Sum(r => r.Field<int>("Marks"));
}
}
}
Chart1.Series[seriesName].ChartType = SeriesChartType.Bar;
//convert datatable to a IEnumerable form
var IEtable = (table as System.ComponentModel.IListSource).GetList();
Chart1.DataBindTable(IEtable, "Question");