how to bind DataTable to legend in mschart - c#

I use excel to paint a pic as follow:
please pay attention to the area hightlight in red
my question is:
how to bind the datatable to legend in mschart?
so , except the legend color, also people can see the detail data from the legend.
or, you guys can tell is it feasible to bind in mschart?
Thanks in advance!

As far as I know there is nothing in the API that allows you to simply "bind" your DataTable to a legend.
But you should still be able to manage something like that with some code:
var l = chart1.Legends[0];
l.LegendStyle = LegendStyle.Table;
l.TableStyle = LegendTableStyle.Tall;
l.BorderColor = Color.OrangeRed;
l.Docking = Docking.Bottom;
l.LegendStyle = LegendStyle.Table;
l.HeaderSeparator = LegendSeparatorStyle.DashLine;
l.HeaderSeparatorColor = Color.Red;
var firstColumn = new LegendCellColumn();
l.ColumnType = LegendCellColumnType.SeriesSymbol;
l.CellColumns.Add(firstColumn);
var secondColumn = new LegendCellColumn();
l.ColumnType = LegendCellColumnType.Text;
secondColumn.Text = "#SER";
l.CellColumns.Add(secondColumn);
foreach (DataRow row in dt.Rows)
{
var column = new LegendCellColumn();
column.ColumnType = LegendCellColumnType.Text;
column.HeaderText = row["x"].ToString();
column.Text = "#VALY";
l.CellColumns.Add(column);
}
However, my suggestion would be to include the data in a separate control, rather than as part of the chart itself. It would be much easier to work with if you kept it in one of the .net controls that's meant for tabular data, whether you're in winforms or webforms.

Related

Devexpress - how to add columns to multilevel gridView

I'm trying to make two-level grid control. It has some package with its elements. I want to add one more column to gridView3 and hide three columns. I'm sending data to this gridView in code (dataset - 2 tables with one relation).
I've tried to send data to this gridcontrol and then operate on gridviews
gridControl2.DataSource = value.Tables[0];
gridView3.PopulateColumns(value.Tables[1]);
gridView3.Columns["RpkeId"].Visible = false;
gridView3.Columns["SourceLueId"].Visible = false;
gridView3.Columns["NewLueId"].Visible = false;
and then I'm trying to add column:
var test = new List<int> { 1, 12, 123, 1234 };
RepositoryItemComboBox riComboBox = new RepositoryItemComboBox();
riComboBox.Items.AddRange(test);
gridControl2.RepositoryItems.Add(riComboBox);
GridColumn columnGIDNumer = new GridColumn();
columnGIDNumer.FieldName = "asdfasdfa";
columnGIDNumer.ColumnEdit = riComboBox;
columnGIDNumer.OptionsColumn.AllowEdit = true;
gridView3.Columns.Add(columnGIDNumer);
None of these seem to work:
Is there any event I need to invoke or something? It seems that it'd work in normal, one-level gridcontrol.
Thanks in advance!

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", "");

Listviews in C#

string title = HardwareInfo.GetComputerName().ToString();
TabPage myTabPage = new TabPage(title);
// tabControl1.TabPages.Add(myTabPage);
// Create Column Headers
ListView listView2 = new ListView();
ColumnHeader columnA = new ColumnHeader();
columnA.Text = "adsasd";
columnA.Width = 185;
columnA.TextAlign = HorizontalAlignment.Left;
ColumnHeader columnB = new ColumnHeader();
columnB.Text = "asd";
columnB.Width = 185;
columnB.TextAlign = HorizontalAlignment.Left;
ColumnHeader columnC = new ColumnHeader();
columnC.Text = "asdasd";
columnC.Width = 185;
columnC.TextAlign = HorizontalAlignment.Left;
ColumnHeader columnD = new ColumnHeader();
columnD.Text = "xx";
columnD.Width = 185;
columnD.TextAlign = HorizontalAlignment.Left;
// Add columns to the ListView:
listView2.Columns.Add(columnA);
listView2.Columns.Add(columnB);
listView2.Columns.Add(columnC);
listView2.Columns.Add(columnD);
listView2.Size = new Size(800, 300);
listView2.Location = new Point(0, 0);
listView2.GridLines = true;
listView2.View = View.Details;
Here I have a copy of some of my Code, and what I am looking to do is get a list of computers on my next work, then create tabs for each computer. I have that part done perfectly fine, but the issue I am having is that, it creates the listviews with the same NAME and that is causing an obvious problem when I try and add information to those specific list views. I was wondering, how would I go about giving each listview a name of the computer for example. As you can see for my tabs I can do that, but when it comes to the list views, if i try and do the same type of assign a string title to where it says Listview listview2 It wont let me compile. I'm new to programming and I apologize if this is obvious. Thank you.
It sounds like you want to create a List<ListView> and add your listviews to it.
Depending on how you use it, you may want a dictionary instead.
If i understand the question what you want is the name variable, in this case
listView2.name = <name of listview2>
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview_members(v=vs.71)
But i think you should look into using functions with a returntype of columns for those column constructor parts.
You want the to make the variable that stores the listview part of a dictionary, this way you can look up the different computers by their name, or whatever string you desire
Dictionary<string, ListView>
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx
You could use a List if you don't need the lookup portion of the dictionary, but is fine with using integers as with an array
List<ListView>
http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

DataBinding a DataGridview rows BackColor

I have a DataGridView that uses databinding, with manually created columns, and this works fine.
However I want the rows' BackColor to be databound as well, and so far my attempts have hit errors.
This is my latest attempt:
dataGridFileTransfer.RowHeadersVisible = false;
dataGridFileTransfer.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridFileTransfer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridFileTransfer.MultiSelect = false;
dataGridFileTransfer.ReadOnly = true;
var files = GetReceivedFiles(false).Union(FileDetails.Select(FileStatus.FailedVerification)).ToList();
dataGridFileTransfer.AutoGenerateColumns = false;
string[] displayHeaders = new string[] { COL_NAME, COL_TYPE, COL_CREATED, COL_SIZE, COL_STATUS };
string[] displayProps = new string[] { "Filename", "FileTypeDisplayName", "Created", "Size", "FileStatus" };
for (int i = 0; i < displayHeaders.Length; i++)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = displayHeaders[i];
col.DataPropertyName = displayProps[i];
if (displayHeaders[i] == COL_CREATED)
col.DefaultCellStyle.Format = Constants.DDMMYYYHHMMSS;
dataGridFileTransfer.Columns.Add(col);
}
Binding bi = new Binding("DefaultCellStyle.BackColor", files, "DisplayColor");
dataGridFileTransfer.DataBindings.Add(bi);
dataGridFileTransfer.DataSource = files;
Which is generating an ArguementException:
"Cannot bind to the property
"DefaultCellStyle.BackColor' on the
target control. Parameter name:
PropertyName"
Is it the value of PropertyName that is wrong, or should I binding to an object other than the DataGridView? (i.e. a column?)
Or is the problem that PropertyName cannot be in the form X.Y? I thought I had seen/used this syntax before, but maybe it only works for DataMember?
Any help is much appreciated
I think the problem is files.DisplayColor. files is a collection an has no property DisplayColor but each item of the collection has. So you are trying to bind a not existing property. Further binding collection DataGridView.DataBindings allows you to data bind properties of the control, not of its rows. There is only one DataGridView.DefaultCellStyle.BackColor for all rows. So I believe you end up needing to bind the DefaultCellStyle of each row to the coresponding item from files and I am not sure if this is possible. It might be that the DataGridView creates and deletes rows as required - for example if you perform filtering - and this will destroy the data binding, too.
So, I am not sure if row coloring can be done with data binding, but I personaly doubt it. This would require some really smart logic recognicing 'bind the property DisplayColor of the object data bound to this row to the property DefaultCellStyle.BackColor of this row.'
You could surly implement such an smart data binding. While it would be a great thing, it will be quite complex, too. As a simple solution you could just use the RowPrepaint event to set the correct color for the row.

What is the DataGrid MappingName for a non DataTable DataSource?

I am able to Bind my DataGrid in .NET 3.5 CF to a List() but I am unable to format the columns by specifying their width. Below is the code that looks like it should work but does not. I am pretty sure that I am not setting the MappingName correctly as all tutorials tell you to set it to the name of your DataTable but I am not binding to a DataTable so I am not quiet sure what to do.
grdBatch.DataSource = InventoryItems;
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = InventoryItems.ToString();
DataGridTextBoxColumn tbcName = new DataGridTextBoxColumn();
tbcName.Width = 400;
tbcName.MappingName = "SERIAL_ID";
tbcName.HeaderText = "SERIAL_ID";
tableStyle.GridColumnStyles.Add(tbcName);
grdBatch.TableStyles.Clear();
grdBatch.TableStyles.Add(tableStyle);
grdBatch is a DataGrid and InventoryItems is a List of POCOS(Plain old C# Objects).
Change:
tableStyle.MappingName = InventoryItems.ToString();
to
tableStyle.MappingName = InventoryItems.GetType().Name;

Categories

Resources