I am trying to create a StackedColumn chart in my asp.net application. So I have a datatable like this -
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Phase", typeof(string));
dtTemp.Columns.Add("Status", typeof(string));
dtTemp.Columns.Add("Count", typeof(int));
dtTemp.Rows.Add("Initiate", "Pending", 10
dtTemp.Rows.Add("Initiate", "OnHold", 20);
dtTemp.Rows.Add("Initiate", "Rejected", 3);
dtTemp.Rows.Add("Initiate", "Cancelled", 5);
dtTemp.Rows.Add("Initiate", "Pending IT", 2);
dtTemp.Rows.Add("Setup", "Setup", 25);
Now I want the chart to display 2 column, first column will show the Initiate phase data with different status data. And 2nd column will show setup phase data. I have tried like this -
foreach (DataRow row in dtTemp.Rows)
{
string seriesName = row["Status"].ToString();
chart_ProjectStatus.Series.Add(seriesName);
chart_ProjectStatus.Series[seriesName].ChartType = SeriesChartType.StackedColumn;
chart_ProjectStatus.Series[seriesName].ChartArea = "ChartArea1";
chart_ProjectStatus.Series[seriesName].CustomProperties = "DrawingStyle=Cylinder, MaxPixelPointWidth=50";
chart_ProjectStatus.Series[seriesName].Points.AddXY(row["Phase"].ToString(), Convert.ToInt32(row["Count"].ToString()));
}
But I am getting only one column -
Please some one help me.
Thanks in advance
Gulrej
<b> Hope this will Help you </b>
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("Phase", typeof(string));
dtTemp.Columns.Add("Status", typeof(string));
dtTemp.Columns.Add("Count", typeof(int));
dtTemp.Columns.Add("StackGroupName", typeof(string));
dtTemp.Rows.Add("Initiate", "Pending", 10, "Group1");
dtTemp.Rows.Add("Initiate", "OnHold", 20, "Group1");
dtTemp.Rows.Add("Initiate", "Rejected", 3, "Group1");
dtTemp.Rows.Add("Initiate", "Cancelled", 5, "Group1");
dtTemp.Rows.Add("Initiate", "PendingIT", 2, "Group1");
dtTemp.Rows.Add("Setup", "Setup", 25, "Group2");
dtTemp.Rows.Add("Setup", "INSTALLED", 55, "Group2");
dtTemp.AcceptChanges();
Series ss;
for (int i = 0; i < dtTemp.Rows.Count; i++)
{
ss = new Series();
ss.Name = dtTemp.Rows[i][1].ToString();
ss.ChartType = SeriesChartType.StackedColumn;
ss["StackedGroupName"] = dtTemp.Rows[i]["StackGroupName"].ToString();
ss["DrawingStyle"] = "Cylinder";
ss.Points.AddXY(Convert.ToString(dtTemp.Rows[i]["Phase"]), Convert.ToInt32(dtTemp.Rows[i]["Count"]));
ss.IsValueShownAsLabel = true;
ChartSideBySideStacked.Series.Add(ss);
}
ChartArea chartAread = new ChartArea();
chartAread.Area3DStyle.Enable3D = true;
chartAread.Area3DStyle.Rotation = 5;
chartAread.Area3DStyle.Inclination = 10;
chartAread.Area3DStyle.IsRightAngleAxes = false;
ChartSideBySideStacked.ChartAreas.Add(chartAread);
ChartSideBySideStacked.Legends.Add(new Legend("CHartLEgend"));
ChartSideBySideStacked.AlignDataPointsByAxisLabel(); // Chart Object Name : ChartSideBySideStacked
Enable 3D
U must Provide Stacked Group Name for Each Series
Set Rotation Angle
Set Inclination and IsRightAngleAxes
this one is working fine ... please note i am using VS 2010.. Asp.Net
Related
I'm creating an MVC controller action where JSON data passed in to the method is to be written to an excel file. Right now, I'm testing out the functionality by using hardcoded data from a data table based on the example from this blog post.
Here is the code I have:
[HttpPost]
public ActionResult ExportData()
{
Microsoft.Office.Interop.Excel.Application excel;
Microsoft.Office.Interop.Excel.Workbook worKbooK;
Microsoft.Office.Interop.Excel.Worksheet worKsheeT;
Microsoft.Office.Interop.Excel.Range celLrangE;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
excel.DisplayAlerts = false;
worKbooK = excel.Workbooks.Add(Type.Missing);
worKsheeT = (Microsoft.Office.Interop.Excel.Worksheet)worKbooK.ActiveSheet;
worKsheeT.Name = "StudentRepoertCard";
worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[1, 8]].Merge();
worKsheeT.Cells[1, 1] = "Student Report Card";
worKsheeT.Cells.Font.Size = 15;
int rowcount = 2;
foreach (DataRow datarow in ExportToExcel().Rows)
{
rowcount += 1;
for (int i = 1; i <= ExportToExcel().Columns.Count; i++)
{
if (rowcount == 3)
{
worKsheeT.Cells[2, i] = ExportToExcel().Columns[i - 1].ColumnName;
worKsheeT.Cells.Font.Color = System.Drawing.Color.Black;
}
worKsheeT.Cells[rowcount, i] = datarow[i - 1].ToString();
if (rowcount > 3)
{
if (i == ExportToExcel().Columns.Count)
{
if (rowcount % 2 == 0)
{
celLrangE = worKsheeT.Range[worKsheeT.Cells[rowcount, 1], worKsheeT.Cells[rowcount, ExportToExcel().Columns.Count]];
}
}
}
}
}
celLrangE = worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[rowcount, ExportToExcel().Columns.Count]];
celLrangE.EntireColumn.AutoFit();
Microsoft.Office.Interop.Excel.Borders border = celLrangE.Borders;
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border.Weight = 2d;
celLrangE = worKsheeT.Range[worKsheeT.Cells[1, 1], worKsheeT.Cells[2, ExportToExcel().Columns.Count]];
worKbooK.SaveAs("\\root\\test.xlsx");
worKbooK.Close();
excel.Quit();
}
catch (Exception ex)
{
return Json(new { saveSuccess = false }, JsonRequestBehavior.AllowGet);
}
finally
{
worKsheeT = null;
celLrangE = null;
worKbooK = null;
}
return Json(new { saveSuccess = true }, JsonRequestBehavior.AllowGet);
}
//private void Form1_Load(object sender, EventArgs e)
//{
// dataGridView1.DataSource = ExportToExcel();
//}
public System.Data.DataTable ExportToExcel()
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Sex", typeof(string));
table.Columns.Add("Subject1", typeof(int));
table.Columns.Add("Subject2", typeof(int));
table.Columns.Add("Subject3", typeof(int));
table.Columns.Add("Subject4", typeof(int));
table.Columns.Add("Subject5", typeof(int));
table.Columns.Add("Subject6", typeof(int));
table.Rows.Add(1, "Amar", "M", 78, 59, 72, 95, 83, 77);
table.Rows.Add(2, "Mohit", "M", 76, 65, 85, 87, 72, 90);
table.Rows.Add(3, "Garima", "F", 77, 73, 83, 64, 86, 63);
table.Rows.Add(4, "jyoti", "F", 55, 77, 85, 69, 70, 86);
table.Rows.Add(5, "Avinash", "M", 87, 73, 69, 75, 67, 81);
table.Rows.Add(6, "Devesh", "M", 92, 87, 78, 73, 75, 72);
return table;
}
Right now, the code works up until the point where the save happens. For some reason, the file location is not found. I was assuming that the name of the file had to be listed at the end of the path after the containing folder in order to provide the name, but maybe this isn't the correct way to specify the file path.
What I actually need to do is to allow the user to choose the file location in file explorer, provide a name, and then save the file. Since this is the case, the file path would have to be provided dynamically. I've looked at many SO posts and articles but I haven't seen a clear example of how to do this.
How should the code be modified for the user to be able to specify the file name and path?
You cannot choose to save the file from their browser. You need to serve up the file and let them download it and save it where they like.
Also, the server you want a production ASP.NET application deployed to probably doesn't have a copy of Excel installed (and even if it does interop gets a little messy IMHO) so you probably want to use a openXml library such as EPPlus instead.
This would let you do something like this:
public IActionResult ExportData()
{
using (var excel = new ExcelPackage())
{
var wks = excel.Workbook.Worksheets.Add("StudentReportCard");
wks.Cells[1,1].LoadFromCollection(GetStudentRecords(), PrintHeaders:true);
return File(excel.GetAsByteArray(),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "export.xlsx");
};
}
private static IEnumerable<StudentRecord> GetStudentRecords()
{
yield return new StudentRecord
{
Id = 1,
Name = "John",
Subject = "Maths",
Score = 77.9
};
yield return new StudentRecord
{
Id = 2,
Name = "Jane",
Subject = "Maths",
Score = 78.9
};
yield return new StudentRecord
{
Id = 3,
Name = "Jo",
Subject = "Maths",
Score = 99.9
};
}
Which sends a file like this named 'export.xlsx' for the user to save from their browser:
I want to select and re-assign the datatable after spesific hour. I use below code;
dtMasterPivot = dtMasterPivot.AsEnumerable().Where(x => x.Field<DateTime>("SAMPLE_TIME").Hour >= 4).CopyToDataTable();
Like above what i want is only select the data after 04:00. However, it doesn't work. It still brings the before 04:00.
This works as advertised:
class Program
{
static void Main(string[] args)
{
var dT = new DataTable();
dT.Columns.Add("Id", typeof(int));
dT.Columns.Add("Sample_Time", typeof(DateTime));
dT.Columns.Add("Misc", typeof(string));
var row = dT.NewRow();
row[0] = 1;
row[1] = new DateTime(2017, 8, 3, 15, 15, 0);
row[2] = "3:15 PM";
dT.Rows.Add(row);
row = dT.NewRow();
row[0] = 2;
row[1] = new DateTime(2017, 8, 3, 3, 59, 0);
row[2] = "3:59 AM";
dT.Rows.Add(row);
row = dT.NewRow();
row[0] = 3;
row[1] = new DateTime(2017, 8, 3, 12, 0, 0);
row[2] = "Noon";
dT.Rows.Add(row);
dT = dT.AsEnumerable().Where(x => x.Field<DateTime>("Sample_Time").Hour >= 4).CopyToDataTable();
for (int i = 0; i < dT.Rows.Count; i++)
{
Console.WriteLine((string)dT.Rows[i][2]);
}
Console.ReadKey();
}
}
Silly question really, but are you sure about your field names? And also whether you are using 12/24 hour clock?
Code:
// Datatable DB
DataTable CashBillingsArticles_DB = new DataTable();
var CashBillingsArticles_DB_Primary = CashBillingsArticles_DB.Columns.Add("article_id", typeof(Int32));
CashBillingsArticles_DB.Columns.Add("article_quantity", typeof(Double));
CashBillingsArticles_DB.Columns.Add("article_sellprice", typeof(Double));
CashBillingsArticles_DB.Columns.Add("article_discount", typeof(Double));
CashBillingsArticles_DB.Columns.Add("article_isv", typeof(Double));
CashBillingsArticles_DB.Columns.Add("article_total", typeof(Double));
CashBillingsArticles_DB.PrimaryKey = new DataColumn[] { CashBillingsArticles_DB_Primary };
// DB Data
CashBillingsArticles_DB.Rows.Add(1, 1, 5, 0, 0, 5);
CashBillingsArticles_DB.Rows.Add(2, 2, 6, 0, 0, 12);
CashBillingsArticles_DB.Rows.Add(4, 1, 10, 0, 0, 10);
CashBillingsArticles_DB.Rows.Add(7, 2, 3, 0, 0.9, 6.9);
// DataTable Grid
DataTable CashBillingsArticles_Grid = new DataTable();
var CashBillingsArticles_Primary = CashBillingsArticles_Grid.Columns.Add("article_id", typeof(Int32));
CashBillingsArticles_Grid.Columns.Add("article_quantity", typeof(Double));
CashBillingsArticles_Grid.Columns.Add("article_sellprice", typeof(Double));
CashBillingsArticles_Grid.Columns.Add("article_discount", typeof(Double));
CashBillingsArticles_Grid.Columns.Add("article_isv", typeof(Double));
CashBillingsArticles_Grid.Columns.Add("article_total", typeof(Double));
CashBillingsArticles_Grid.PrimaryKey = new DataColumn[] { CashBillingsArticles_Primary };
// Grid Data
CashBillingsArticles_Grid.Rows.Add(2, 1, 6, 0, 0, 6);
CashBillingsArticles_Grid.Rows.Add(3, 1, 1, 0, 0.15, 1.15);
CashBillingsArticles_Grid.Rows.Add(4, 3, 10, 0, 0, 30);
CashBillingsArticles_Grid.Rows.Add(7, 4, 3, 0, 1.8, 13.8);
I need to show this result comparing the article_id column in the tables and always show the article_sellprice value of the datatable CashBillingsArticles_Grid:
id qty price desc isv total
1 -1 5 0 0 -5
2 -1 6 0 0 -6
3 1 1 0 0.15 1.15
4 2 10 0 0 20
7 2 3 0 0.9 6.9
Graphic Illustration:
In the database i have:
The user make some modifications to the invoice and the final result is:
I need to get the differences between the database and the user grid modification, like this: (I need this output)
I have Data table values like bellow now I want to group these values into One Row Based on the Second column Value and able to calculate totals like bellow . Any idea can be shared .
Required Format Data like Bellow
1KT049014L1(F),L2(F),R1(F),R2(F) Ghoousunnisa (F) 9999999999 Nellore 2200 220 1980 APIEasyBus Agent
Actual Data Format
1 KT049014 L1 Ghoousunnisa (F) 9999999999 Nellore 550 55 495 API EasyBus Agent
2 KT049014 L2 Ghoousunnisa (F) 9999999999 Nellore 550 55 495 API EasyBus Agent
3 KT049014 R1 Ghoousunnisa (F) 9999999999 Nellore 550 55 495 API EasyBus Agent
4 KT049014 R2 Ghoousunnisa (F) 9999999999 Nellore 550 55 495 API EasyBus Agent
static void Main(string[] args)
{
var dt = new DataTable();
dt.Columns.Add("A", typeof(int));
dt.Columns.Add("B", typeof(string));
dt.Columns.Add("C", typeof(string));
dt.Columns.Add("D", typeof(string));
dt.Columns.Add("E", typeof(string));
dt.Columns.Add("F", typeof(string));
dt.Columns.Add("G", typeof(string));
dt.Columns.Add("H", typeof(decimal));
dt.Columns.Add("I", typeof(decimal));
dt.Columns.Add("J", typeof(decimal));
dt.Columns.Add("K", typeof(string));
dt.Columns.Add("L", typeof(string));
dt.Columns.Add("M", typeof(string));
dt.Rows.Add(1, "KT049014", "L1", "Ghoousunnisa", "(F)", "9999999999", "Nellore", 550, 55, 495, "API", "EasyBus", "Agent");
dt.Rows.Add(2, "KT049014", "L2", "Ghoousunnisa", "(F)", "9999999999", "Nellore", 550, 55, 495, "API", "EasyBus", "Agent");
dt.Rows.Add(3, "KT049014", "R1", "Ghoousunnisa", "(F)", "9999999999", "Nellore", 550, 55, 495, "API", "EasyBus", "Agent");
dt.Rows.Add(4, "KT049014", "R2", "Ghoousunnisa", "(F)", "9999999999", "Nellore", 550, 55, 495, "API", "EasyBus", "Agent");
var datas =
dt.AsEnumerable()
.GroupBy(row => row[1])
.Select((group, i) => new {A = i + 1, B = group.Key, Others = combine(dt.Columns, group)});
var result = dt.Clone();
foreach (var d in datas)
{
var row = result.Rows.Add(d.A, d.B);
for (int i = 0; i < d.Others.Count(); i++)
row[i + 2] = d.Others.ElementAt(i);
}
}
private static IEnumerable<object> combine(DataColumnCollection columns, IGrouping<object, DataRow> group)
{
for (int i = 2; i < columns.Count; i++)
{
if (columns[i].DataType == typeof(string))
yield return string.Join(",", group.Select(r => r.Field<string>(i)).Distinct());
else
yield return group.Sum(r => r.Field<decimal>(i));
}
}
}
L1(F),L2(F),R1(F),R2(F) :
string.Join(",", group.Select(r=>string.Concat(r[2], r[4])))
Ghoousunnisa (F) 9999999999 Nellore APIEasyBus Agent:
(I don't know if they are always the same in the group)
string.Join(",", group.Select(r=>r.Field<string>("D")))
string.Join(",", group.Select(r=>.Field<string>("D")).Distinct())
group.Select(r=>r.Field<string>("D")).First()
2200 220 1980:
group.Sum(r=>r.Field<decimal>("I"))
I'm using DevExpress for WinForms (the free one) and I'm using a 3D pie chart.
I already have a chart using the Windows version and all I do is pass four variables as the values the chart needs in the series.
Here's the code I use currently.
double[] yValues = { bottom, bmid, tmid, top};
string[] xNames = { "Greater than 200", "Between 200-100", "Between 100-50", "Below 50" };
chart1.Series[0].Points.DataBindXY(xNames, yValues);
Now, I've made a DevExpress chart and tried to use:
Devchart1.series[0].points
but points.databind does not exist.
Does anyone know how I bind the data like I have using WinForms?
UPDATE
Here's some more things I tried (commented out).
double[] yValues = { bottom, bmid, tmid, top};
string[] xNames = { "Greater than 200", "Between 200-100", "Between 100-50", "Below 50" };
chart1.Series[0].Points.DataBindXY(xNames, yValues);
DataTable chartTable = new DataTable("Table1");
// Add two columns to the table.
chartTable.Columns.Add("Names", typeof(string));
chartTable.Columns.Add("Value", typeof(Int32));
chartTable.Rows.Add("Below 50", top);
chartTable.Rows.Add("Between 50-100", tmid);
chartTable.Rows.Add("Between 100-200", bmid);
chartTable.Rows.Add("Greater than 200", top);
Series series1 = new Series("Series1", ViewType.Pie3D);
chartControl2.Series.Add(series1);
series1.DataSource = chartTable;
series1.ArgumentScaleType = ScaleType.Qualitative;
series1.ArgumentDataMember = "names";
series1.ValueScaleType = ScaleType.Numerical;
series1.ValueDataMembers.AddRange(new string[] { "Value" });
//((Pie3DSeriesView)series1.View). = true;
//((pie)chartControl2.Diagram).AxisY.Visible = false;
chartControl2.Legend.Visible = false;
// Dock the chart into its parent and add it to the current form.
chart1.Dock = DockStyle.Fill;
::UPDATE2::
heres what happens with this code with values 101, 22, 20 and 15.
DevExpress Series has DataSource property for binding.
Check this article. Hope it helps
UPDATE:
I use your code and it seems to work fine
DataTable chartTable = new DataTable("Table1");
// Add two columns to the table.
chartTable.Columns.Add("Names", typeof(string));
chartTable.Columns.Add("Value", typeof(Int32));
chartTable.Rows.Add("Below 50", 10);
chartTable.Rows.Add("Between 50-100", 10);
chartTable.Rows.Add("Between 100-200", 10);
chartTable.Rows.Add("Greater than 200", 10);
Series series1 = new Series("Series1", ViewType.Pie3D);
//chartControl1.Series.Clear();
chartControl2.Series.Add(series1);
series1.DataSource = chartTable;
series1.ArgumentScaleType = ScaleType.Qualitative;
series1.ArgumentDataMember = "names";
series1.ValueScaleType = ScaleType.Numerical;
series1.ValueDataMembers.AddRange(new string[] { "Value" });
//((Pie3DSeriesView)series1.View). = true;
//((pie)chartControl2.Diagram).AxisY.Visible = false;
chartControl2.Legend.Visible = false;