How To Create Chart Series Array Objects in Asp.Net Charts C# - c#

i want to create series dynamically for that i created series array objects like this
Series[] series = new Series[10];
series[0].Name = "Result Chart";
but while execution of program it showing object reference null error
how to solve this problem

Series[] series = new Series[10];
series[0] = new Series();// this line is the one you missed
series[0].Name = "Result Chart";

Series[] series = new Series[10];
This line create an array. The new is just for creating array of series.
This line puts the value at the 0th index
series[0].Name = "Result Chart";
The problem is that you have not instantiated the object at 0th index.
So you need to instantiated every index which you want to use.
Like if you want to use the 0th index then you will have to use
series[0] = new Series();
or just create a loop to instantiated every index as follows
for(int i=0;i<series.Count;i++)
{
series[i] = new Series();
}

Related

Having 2 graphs on the same chart when they have different data sizes. Any ideas?

I have:
List<string> dates1 = new List<string>();
List<string> values1 = new List<string>();
List<string> dates2 = new List<string>();
List<string> values2 = new List<string>();
That will accept my data ranges from a datasource. Dates1 and Values1 have 128 values in each list.
The dates1 data looks like this {"5/29/2015 11:02",....,"5/30/2015 11:02",....,"5/31/2015 11:02",....,"6/1/2015 11:02",....,"6/2/2015 11:02",....,"6/3/2015 11:02"} with the in between data points being different times within that day for a total 128 values.
The dates2 data looks like this {"5/29/2015 9:05","5/30/2015 9:05","5/31/2015 9:05","6/1/2015 9:05","6/2/2015 9:05","6/3/2015 9:05"} with 7 total values.
The two graphs separately look correct and like this:
The problem is getting both graphs on the same chart correctly.
If I create 2 series on the chart I will get this:
I have seen some advice to use Chart1.AlignDataPointsByAxisLabel(); and this is what I get:
The two graph should be spread out evenly over each other but the x-axis for the 2nd series is being disrupted for some reason. Is this even possible to do? I would greatly appreciate any help you can spare.
Here is the c# code if interested:
List<string> dates = new List<string>();
List<string> values = new List<string>();
List<string> dates2 = new List<string>();
List<string> values2 = new List<string>();
//*****SQL connection and code that populates the Lists is here*****
Chart1.Series.Add(new Series());
Chart1.Series[0].Points.DataBindXY(dates, values);
Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.StepLine;
Chart1.Series[0].Color = Color.Red;
Chart1.Series[0].BorderWidth = 1;
Chart1.Series[0].ToolTip = "#VALY, #VALX";
//BIND THE DATA TO THE CHART
Chart1.Series.Add(new Series());
Chart1.Series[1].Points.DataBindXY(dates2, values2);
Chart1.Series[1].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.StepLine;
Chart1.Series[1].Color = Color.Green;
Chart1.Series[1].BorderWidth = 2;
Chart1.Series[1].ToolTip = "#VALY, #VALX";
//SET THE IMAGE OUTPUT TYPE TO BE JPEG
Chart1.ImageType = System.Web.UI.DataVisualization.Charting.ChartImageType.Jpeg;
//ADD A PLACE HOLDER CHART AREA TO THE CHART
Chart1.ChartAreas.Add(new ChartArea());
Chart1.AlignDataPointsByAxisLabel(); // THIS IS THE COMBINING OF AXES
Chart1.ChartAreas[0].Area3DStyle.Enable3D = false;
Chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.FromArgb(50, 200, 200, 200);
Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(50, 200, 200, 200);
//ADD A PLACE HOLDER LEGEND TO THE CHART
//SHOW THE LEGEND
Chart1.Legends.Add(new Legend());
Chart1.Legends[0].Enabled = true;
Edit: having dates, in the 2nd dataset, that match up perfectly to dates in the first seems to fix it. The issue is the dates will never match because they are taken at different times and has a time precision. Surely, there is a way to do this that I'm missing.
You can achieve your requirement using Syncfusion chart by setting columnIndex property for each axis. columnIndex is used to split the chart horizontally so that we can place series without overlapping.
Following Screenshots illustrate this
We have prepared a sample based on your requirements. Please find the sample from below link.
Sample Link:
http://jsfiddle.net/jr8x19vz/55/
Syncfusion also offers ASP.NET wrappers powered by Essential Studio for Javascript, providing client-side rendering of HTML 5-Javascript controls. You can get more information about Syncfusion’s ASP.Net suite from the following location,
http://www.syncfusion.com/products/aspnet
You can view the demos from the following link,
http://js.syncfusion.com/demos/web
Also take a look at our community license which provides free access to our entire products,
http://www.syncfusion.com/products/communitylicense
Please let me know, if you have any changes.
Thanks,
Jayavigneshwaran
I finally figure it out. One issue I had was pulling in my date as a string instead of DateTime. I switched to DateTime and it fixed the issue while using a data table and binding with Chart1.DataBindCrossTable.
var dt = new System.Data.DataTable();
dt.Columns.Add("Series", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Value", typeof(string));
//looping through SQL datasource for first series
while(){
dt.Rows.Add("Today", date, value);
}
//looping through SQL datasource for second series
while(){
dt.Rows.Add("Yesterday", date, value);
}
Chart1.Series.Clear();
Chart1.DataBindCrossTable(dt.Rows, "Series", "Date", "Value", "");

Microsoft Chart Control Axis Titles not displaying properly

I am having problems getting the XAxis to display the correct values in my chart control. Below is how I am generating each series to be displayed. I would like the XAxis to display the DataType value from s.DataType:
foreach (SummaryData s in summaryData)
{
System.Web.UI.DataVisualization.Charting.Series series = new System.Web.UI.DataVisualization.Charting.Series(s.DataType);
series.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
DataPoint dp = new DataPoint();
dp.Name = s.DataType;
dp.SetValueY(s.Total);
dp.SetCustomProperty("DataType", s.DataType);
series.XValueMember = "DataType";
series.Points.Add(dp);
msBarVertLegRight.Series.Add(series);
}
msBarVertLegRight.DataBind();
The corect value is displayed and the correct name in the Legend, but I'm not exactly sure how to set the XAxis value.
JH
Looks like you're trying to use databinding (which is what the XValueMember is for) and add points one by one. You can do one or the other. You could use databinding like so (approximately):
series.XValueMember = "DataType";
series.YValueMember = "ThePropertyWithY";
series.DataSource = s;
series.DataBind();
or you could set each point individually:
System.Web.UI.DataVisualization.Charting.Series series = new System.Web.UI.DataVisualization.Charting.Series("mySeries");
series.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
foreach (SummaryData s in summaryData)
{
series.AddXY(s.DataType, s.Total);
}
I'm not sure why you had each SummaryData.DataType being its own series; if that is needed, add that back in, but from the code you posted it didn't seem necessary.
When databinding, any changes to your underlying data (the SummaryData objects) will be reflected in your chart automatically; if you add points individually, you need to handle updates to your chart manually.

Retaining Indentation of the list after adding to PdfCell

I am adding list inside pdfcell.But the identation of this list is not coming properly.
its coming like this:
• One
• Two
• Three
But the same list if add directly to the document,the identation is coming properly.Like below:
• One
• Two
• Three
here is the code :
list = new List(false, 14.4F);
list.ListSymbol = new Chunk("\u2022", FontFactory.GetFont(FontFactory.HELVETICA, 10,iTextSharp.text.Font.BOLD));
ListItem listItem;
listItem = new ListItem(lstrBullets.Trim(), FontFactory.GetFont(FontFactory.HELVETICA, 10, iTextSharp.text.Font.NORMAL));
list.IndentationLeft = lftBulletIndent;
listItem.SetLeading(10.0F, 1.0F);
listItem.Alignment = Element.ALIGN_JUSTIFIED;
list.Add(listItem);
PdfCell cell = new PdfCell();
cell.AddElement(list);
pobjTable.AddCell(cell);
where lftBulletIndent gives the indentation values for list.Please help what i am missing here.How can i retain the indentaion inside a cell?
This how I solved this. I know this is not a proper one.But it worked for me.
I am adding a table with variable width in the first cell of the parent table i.e here pobjTable
PdfPTable DummyTable = new PdfPTable(2);
//Here the floatSpace value changes according to the lftBulletIndent values
float[] headerwidths = { 2f + floatSpace, 98f - floatSpace};
DummyTable.SetWidths(headerwidths);
Pcell = new PdfPCell();
Pcell.Border = Rectangle.NO_BORDER;
DummyTable.AddCell(Pcell);
Pcell = new PdfPCell();
Pcell.AddElement(list);
Pcell.Border = Rectangle.NO_BORDER;
DummyTable.AddCell(Pcell);
pobjTable.AddCell(DummyTable);//Inserting a new table here
pobjTable.AddCell("");

Get gridcontrol records as an array

How can I get gridcontrol records as an array?
I set an array as a datasource for a gridcontrol (devExpress component).
PersonFamily4grid[] tmpPersonFamily = new PersonFamily4grid[PersonFamiliesCOUNT];
for (int i = 0; i < PersonFamiliesCOUNT; i++)
{
tmpPersonFamily[i] = new PersonFamily4grid();
tmpPersonFamily[i].BirthDate = PersonFamilies[i].BirthDate;
tmpPersonFamily[i].Job = PersonFamilies[i].Job;
tmpPersonFamily[i].CodeMelli = PersonFamilies[i].CodeMelli;
tmpPersonFamily[i].NameFamily = PersonFamilies[i].NameFamily;
tmpPersonFamily[i].Nesbat = FamilyInfo_cbe_Nesbat.Properties.Items[PersonFamilies[i].Nesbat].ToString();
tmpPersonFamily[i].Taahol = FamilyInfo_cbe_Taahol.Properties.Items[Convert.ToInt32(PersonFamilies[i].Taahol)].ToString();
}
grid_Family.DataSource = tmpPersonFamily;
Now when the user changes data in gridcontrol, I want to get changes from the grid and affect my base array.
When user changes data in gridcontrol i want get changes from grid and
affect my base array.
Why are you creating temporary array if you want to reflect changes to main array PersonFamilies. just simply assign PersonFamilies to gridControl's data source and it will automatically reflect changes to PersonFamilies.
If you know that class object are reference type, so their reference will not change if you assign the array directly to the gridControls data source as:
grid_Family.DataSource = PersonFamilies;
After making some changes to your data in grid view, check the object of PersonFamilies array that they are updated or not. It will surely update the array of objects.
If you want to work on some customized Data that contained in your PersonFamilies array then you can get the Iterate your temporary array tmpPersonFamily without getting it through the DataSource property of GridControl and it all depends on you how you will manipulate or reflect changes to your main array PersonFamilies.
e.g.
for (int i = 0; i < PersonFamiliesCOUNT; i++)
{
PersonFamilies[i].BirthDate = tmpPersonFamily[i].BirthDate;
PersonFamilies[i].Job = tmpPersonFamily[i].Job;
PersonFamilies[i].CodeMelli = tmpPersonFamily[i].CodeMelli;
PersonFamilies[i].NameFamily = tmpPersonFamily[i].NameFamily;
}
Hope this help..
Try:
PersonFamily4grid[] personFamily = (PersonFamily4grid[])grid_Family.DataSource;

How to change the view angle and label value of a chart .NET C#

Short Description
I am using charts for a specific application where i need to change the view angle of the rendered 3D Pie chart and value of automatic labels from pie label names to corresponding pie values.
This how the chart looks:
Initialization
This is how i initialize it:
Dictionary<string, decimal> secondPersonsWithValues = HistoryModel.getSecondPersonWithValues();
decimal[] yValues = new decimal[secondPersonsWithValues.Values.Count]; //VALUES
string[] xValues = new string[secondPersonsWithValues.Keys.Count]; //LABELS
secondPersonsWithValues.Keys.CopyTo(xValues, 0);
secondPersonsWithValues.Values.CopyTo(yValues, 0);
incomeExpenseChart.Series["Default"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
incomeExpenseChart.Series["Default"].Points.DataBindXY(xValues, yValues);
incomeExpenseChart.ChartAreas["Default"].Area3DStyle.Enable3D = true;
incomeExpenseChart.Series["Default"].CustomProperties = "PieLabelStyle=Outside";
incomeExpenseChart.Legends["Default"].Enabled = true;
incomeExpenseChart.ChartAreas["Default"].Area3DStyle.LightStyle = System.Windows.Forms.DataVisualization.Charting.LightStyle.Realistic;
incomeExpenseChart.Series["Default"]["PieDrawingStyle"] = "SoftEdge";
Basically i am querying data from database using the HistoryModel.getSecondPersonWithValues(); to get pairs as Dictionary<string, decimal> where key is the person and value is ammount.
Problem #1
What i need is to be able to change the marked labels from person names to the ammounts or add another label of ammounts with the same colors (See Image).
Problem #2
Another problem is that i need to change the view angle of 3D Pie chart. Maybe it's very simple and I just don't know the needed property or maybe i need to override some paint event. Either ways any kind of ways would be appriciated.
Thanks in advance George.
Solution for Problem #1
Adding new Label and filling with custom values helped.
In addition i changed the Series.IsValueShownAsLabel = true;
Solution for Problem #2
I should have set the ChartArea.Area3DStyle.IsClustered = true; and then set the ChartArea.Area3DStyle.Inclination;
Use xlhart.Rotation and xlchart.Elevation.

Categories

Resources