I want to add different line chart one at the time to my chart. So i have a textbox with a button . i input a code and load the series from my database. The problem is that the new line series override the old line series. so i decided to keep tract of the added series into a gridview, i loop through it and fill the chart; same result.
This is my code
void AddSerie(string stockName)
{
try
{
Legend legend = new Legend(stockName);
Chart1.Legends.Add(legend);
Series series = new Series(stockName);
Chart1.Series.Add(series);
string[] _data = new string[3];
_data[0] = stockName;
_data[1] = "2010-01-01";
_data[2] = "2015-03-15";
DataSet ds = DBSelect(_data, "web_price_series");
Chart1.DataSource = ds;
Chart1.Series[stockName].XValueMember = "tdate";
Chart1.Series[stockName].YValueMembers = "value";
Chart1.Series[stockName].ChartType = SeriesChartType.Line;
Chart1.DataBind();
}
catch (Exception)
{
throw;
}
}
this is how i call the method
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
AddSerie(row.Cells[0].Text);
}
}
and i have the legend showing two series but the chart displays only one line.
i added this picture to show the second output of the chart for two companies using MarkerStyle.
This is an example of what i want to achieve .
This is the structure of the table
any help?
Instead of using the databind i used another method:
Chart1.Series[0].Points.AddXY(XValues, Yalues);
void AddSerie(string stockName)
{
try
{
Legend legend = new Legend(stockName);
Chart1.Legends.Add(legend);
Series series = new Series(stockName);
Chart1.Series.Add(series);
string[] _data = new string[3];
_data[0] = stockName;
_data[1] = "2010-01-01";
_data[2] = "2015-03-15";
DataSet ds = DBSelect(_data, "web_price_series");
Chart1.DataSource = ds;
Chart1.Series[stockName].XValueMember = "tdate";
Chart1.Series[stockName].YValueMembers = "value";
Chart1.Series[stockName].ChartType = SeriesChartType.Line;
foreach (DataRow row in ds.Tables[0].Rows)
{
Chart1.Series[stockName].Points.AddXY(row[0], row[1]);
}
// Chart1.DataBind();
}
catch (Exception)
{
throw;
}
}
Related
I am trying to add a drop down list into all cells for a column in a datagridView in my Winforms Application. I have created the view programatically by dragging the dgv into the form, and adding columns upon the creation of the class.
The drop down list appears in my DGV column, however when I click on the list nothing opens up - i am unsure if the ComboBox is populated and not opening, or if it simply does not contain values for user to select. Hovering over the object when adding the comboBox to the cell, i can see the item array populated with my values..
Please advise on how I can allow the user to select values in the cell that are populated from my list
DataGridViewTextBoxColumn diffKey = new DataGridViewTextBoxColumn();
DataGridViewComboBoxColumn matchDesc = new DataGridViewComboBoxColumn();
DataGridViewTextBoxColumn permDiff = new DataGridViewTextBoxColumn();
grdEditAnnot.Columns.Add(diffKey);
grdEditAnnot.Columns.Add(matchDesc);
grdEditAnnot.Columns.Add(permDiff);
DataGridViewComboBoxCell matchDescDropbox = new DataGridViewComboBoxCell();
List<string> lstMatchDesc = new List<string>();
LoadMatchDescriptions(out lstMatchDesc);
foreach (string ddlItem in lstMatchDesc)
{
matchDescDropbox.Items.Add(ddlItem);
}
grdEditAnnot.Rows.Add(2);
matchDescDropbox.DataSource = GetMatchDescDDL(); // Bind combobox with datasource.
matchDescDropbox.ValueMember = "Description";
matchDescDropbox.DisplayMember = "Description";
grdEditAnnot.Rows[0].Cells[1] = matchDescDropbox;
}
private DataTable GetMatchDescDDL()
{
DataTable l_dtDescription = new DataTable();
l_dtDescription.Columns.Add("Description", typeof(string));
List<string> matchDescLst;
if (LoadMatchDescriptions(out matchDescLst) == 0)
{
foreach(string matchDesc in matchDescLst)
{
l_dtDescription.Rows.Add(matchDesc);
}
}
return l_dtDescription;
}
private int LoadMatchDescriptions(out List<string> matchDscLst)
{
//string SQL = SQL USED TO POPULATE MY LIST
//a temporary list of test strings would work to show how to get this to work... ex: list of test1, test2, test3 etc
try
{
matchDscLst = new List<string>();
rdr.ExecuteSQL(SQL);
while ((rec = rdr.FetchNextRecord()) != null)
{
matchDscLst.Add(rec.GetStringVal(0));
}
return 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Loading Match Descriptions", MessageBoxButtons.OK, MessageBoxIcon.Error);
matchDscLst = new List<string>();
return -1;
}
}
This is my first post on StackOverflow; apologies if this is not in best format or if we need more info..
Result of code:
Grid that is created; user cannot open up comboBox or type into textbox columns
use DataGridViewComboBoxCell
look this example :
https://www.aspsnippets.com/Articles/Add-ComboBox-to-DataGridView-in-Windows-Forms-Application-using-C-and-VBNet.aspx
Hello I am searching for a graph that will allow me to make Z Line Graph control now I have X (Payment_Date) & Y (collected) line how to make Z (username) line
I want every username to have his line alone
Can someone help me?
I want to make the program on normal C# win form application
My Form
SqlDataReader reader = sqlcomm.ExecuteReader();
DataTable sd = new DataTable();
sd.Load(reader);
dataGridView1.DataSource = sd;
reader.Close();
chart1.Series["collected"].XValueMember = "Payment_Date";
chart1.Series["collected"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
chart1.Series["collected"].YValueMembers = "collected";
chart1.Series["collected"].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32;
chart1.DataSource = sd;
chart1.DataBind();
Step one: Create a separate BindingSource for each element you want to bind and set an appropriate Filter so it will only display the data of one user each.
Step two: Show and hide the Series you want to.
I have created a DataTable dt with 3 columns:
dt.Columns.Add("User", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Value", typeof(double));
and filled it randomly. Next I pull out the distinct users:
var users = dt.Rows.Cast<DataRow>()
.Select(x => x.Field<string>("User"))
.Distinct()
.OrderBy(x => x)
.ToList();
Most likely you will reverse this and start with a list of users.
Now I create a Series with its own filtered BindingSource for each user:
for (int i = 0; i < users.Count; i++)
{
Series s = chart1.Series.Add(users[i]);
s.ChartType = SeriesChartType.Line;
BindingSource bs = new BindingSource();
bs.DataSource = dt;
bs.Filter = "User='" + users[i] + "'";
s.Points.DataBindXY(bs, "Date", bs, "Value");
}
Now I bind my DGV:
BindingSource bsdgv = new BindingSource();
bsdgv.DataSource = dt;
bsdgv.Filter = "";
dataGridView1.DataSource = bsdgv;
I have hidden the Series by making them Transparent. This way the names still show in the Legend. The trick is to keep the original colors in the Series' Tags..:
void hideAllSeries(Chart chart)
{
chart.ApplyPaletteColors();
foreach (Series s in chart.Series)
{
if (s.Color != Color.Transparent) s.Tag = s.Color;
s.Color = Color.Transparent;
}
}
To show or hide a Series I code the MouseClick event:
private void Chart1_MouseClick(object sender, MouseEventArgs e)
{
var hitt = chart1.HitTest(e.X, e.Y);
if (hitt.ChartElementType == ChartElementType.LegendItem)
{
Series s = hitt.Series;
if (s.Color == Color.Transparent)
{
s.Color = (Color)s.Tag;
}
else
{
s.Tag = s.Color;
s.Color = Color.Transparent;
}
}
}
Let's see it at work:
Instead of this solution you may want to add a User selection Checklist and set the DGV's BindingSource's Filter..
I am building a winform point chart using C# where the data comes from a datatable. I've created the points like this:
chart1.Series.Add("series1");
chart1.Series["series1"].ChartType = SeriesChartType.Point;
chart1.Series["series1"].YValueMembers = "VALUE";
chart1.Series["series1"].XValueMember = "DATE";
chart1.DataSource = dt;
I'd like to be able make a legend of the points on the chart using different colors/symbols based on a third column in the datatable dt called product. I've tried a number of things to get this done but nothing is working. How can I accomplish this?
You can use this code I got from msdn page
// Create a new legend called "Legend2".
chart1.Legends.Add(new Legend("Legend2"));
// Set Docking of the Legend chart to the Default Chart Area.
chart1.Legends["Legend2"].DockToChartArea = "Default";
// Assign the legend to Series1.
chart1.Series["Series1"].Legend = "Legend2";
chart1.Series["Series1"].IsVisibleInLegend = true;
Part of my problem was first understanding how to construct charts in the first place. I realized I needed to create a series for each unique product in my datatable and add those to the chart. I first made a list of unique products in the datatable:
List<string> products = new List<string>();
foreach (DataRow row in dt.Rows)
{
if (!products.Contains(row["product"].ToString()))
{
products.Add(row["product"].ToString());
}
}
Then
foreach (string product in products)
{
string seriesName = product;
chart1.Series.Add(seriesName);
DataTable dtprod = new DataTable();
dtprod = dt.Select("product= '" + product + "'").CopyToDataTable();
chart1.Series[seriesName].ChartType = SeriesChartType.Point;
chart1.Series[seriesName].YValueMembers = "VALUE";
chart1.Series[seriesName].XValueMember = "DATE";
chart1.Series[seriesName].Points.DataBindXY(dtprod.Rows,"DATE", dtprod.Rows, "VALUE");
chart1.Series[seriesName].XValueType = ChartValueType.DateTime;
chart1.Series[seriesName].MarkerSize = 10;
}
This produced a point chart with a unique marker for each product. Thanks for your answers!
Here is an image of my pie chart now
Here is the c# code for it..
string query = string.Format("select TestName,Count (TestName) AS Counts from VExecutionGlobalHistory where Tester <> 'dit2988' AND TestTypeID = 1 group by TestName", ddlTests.SelectedItem.Value);
DataTable dt = GetData(query);
foreach (DataRow row in dt.Rows)
{
SpecificTestsWeb6.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["TestName"].ToString(),
Data = Convert.ToDecimal(row["Counts"]),
}
);
}
SpecificTestsWeb6.ChartTitle = "Specific Tests Run";
I need to align the bottom values to the left so i can see them all. I would also like to be able to manually asign colors so there are no repeats. Thanks.
var random = new Random();
foreach (DataRow row in dt.Rows)
{
var color = String.Format("#{0:X6}", random.Next(0x1000000));
SpecificTestsRapp.PieChartValues.Add(new AjaxControlToolkit.PieChartValue
{
Category = row["TestName"].ToString(),
Data = Convert.ToDecimal(row["Counts"]),
PieChartValueColor = color
});
}
SpecificTestsRapp.ChartTitle = "Specific Tests Run";
Here is my code:
DataSet data = new DataSet();
data.ReadXml("data.xml");
DataGridView grid = new DataGridView();
var genreCboBoxItems = data.Tables[0].AsEnumerable().Select(genre => genre.Field<string>("genre")).Distinct().ToArray();
// TODO: Make is so the 'genre' column in grid is a combo box?
grid.DataSource = data.Tables[0];
grid.Dock = DockStyle.Fill;
this.Controls.Add(grid);
*edit: genreCboBoxItems
Try this: (not tested)
var column = new DataGridViewComboBoxColumn();
column.DataSource = data.Tables[0].AsEnumerable().
Select(genre => new { genre = genre.Field<string>("genre") }).Distinct();
column.DataPropertyName = "genre";
column.DisplayMember = "genre";
column.ValueMember = "genre";
grid.DataSource = data.Tables[0];
// Instead of the below line, You could use grid.Columns["genre"].Visible = false;
grid.Columns.Remove("genre");
grid.Columns.Add(column);
This might help you cast DataGridViewColumn to DataGridViewComboBox.
First create DataGridViewComboBoxColumn using designer with proper name. Then say you have a list of String list and other string values to bind to that datagridview then use this code:
Below code will bind a list to two DataGridViewTextBoxCell and a DataGridViewComboBoxCell. Note AllCriterias is a list with two string values and a list of string. DGVEligibilityCriteria is the grid name.
for (int i = 0; i < AllCriterias.Count; i++)
{
DataGridViewTextBoxCell Cmb1 = (DataGridViewTextBoxCell)DGVEligibilityCriteria.Rows[i].Cells[0];
Cmb1.Value = AllCriterias[i].Name;
DataGridViewTextBoxCell Cmb2 = (DataGridViewTextBoxCell)DGVEligibilityCriteria.Rows[i].Cells[1];
Cmb2.Value = AllCriterias[i].Type;
DataGridViewComboBoxCell Cmb = (DataGridViewComboBoxCell)DGVEligibilityCriteria.Rows[i].Cells[2];
foreach (var filtervalue in AllCriterias[i].FilterValues)
{
Cmb.Items.Add(filtervalue);
}
}
Need to display the fist index as default by setting selectindex property.
Use this code : Here "filterValues" is the name of the DataGridViewComboBoxCell which u created in the datagridview designer.
foreach (DataGridViewRow row in DGVEligibilityCriteria.Rows)
{
row.Cells["filterValues"].Value = (row.Cells["filterValues"] as DataGridViewComboBoxCell).Items[0];
}