Reputation: 25
I want to fill the chart from database with code.(please do not suggest me choose chart and click Add datasource) I have tried some codes.but I cannot receive any values and any data.here is the codes ;
public DataTable Rating() {
SqlConnection conn = new SqlConnection("Data Source=MEVLUT\\SQLEXPRESS;Initial Catalog=Chart;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter("Select * from ChannelRatings", conn);
DataTable ds = new DataTable();
conn.Open();
da.Fill(ds);
return ds;
}
private void frmMain_Load(object sender, EventArgs e) {
ChartControl ch = chartcontrolRatings;
Series series = new Series("Ratings",ViewType.Bar);
ch.Series.Add(series);
series.DataSource = Rating();
this.Controls.Add(ch);
}
2 columns ChannelName and RatingValue and they have values.
Upvotes: 2
Views: 1601
Reputation: 763
The first thing I think of is that ExecuteReader did not execute. I will post some example
SqlCommand cmd = new SqlCommand();
cmd.CommandTimeout = 600;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from ChannelRatings";
cmd.Connection = dbConnection;
SqlDataReader dr= null;
using (dr= cmd.ExecuteReader())
{
DataTable dtData = new DataTable(storedProcedureName);
//First create Columns for new DataTable
object[] values = new object[dr.FieldCount];
Type[] columnTypes = new Type[dr.FieldCount];
for (int i = 0; i < dr.FieldCount; i++)
{
Type columnType = dr.GetFieldType(i);
string columnName = dr.GetName(i);
if (columnType != null)
{
dtData.Columns.Add(columnName, columnType);
columnTypes[i] = columnType;
}
}
t1 = DateTime.Now.Subtract(d1);
while (dr.Read())
{//Fill DataTable by reading values by type...
int columnsRead = dr.GetValues(values);
DataRow newRow = dtData.NewRow();
for (int i = 0; i < dtData.Columns.Count; i++)
{
if (columnTypes[i].Name == "Int32") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToInt32(values[i]) : Base.BussinesLayer.Utils.Int32NullValue;
else if (columnTypes[i].Name == "Int16") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToInt16(values[i]) : Base.BussinesLayer.Utils.Int16NullValue;
else if (columnTypes[i].Name == "String") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToString(values[i]) : Base.BussinesLayer.Utils.StringNullValue;
else if (columnTypes[i].Name == "Decimal") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToDecimal(values[i]) : Base.BussinesLayer.Utils.DecimalNullValue;
else if (columnTypes[i].Name == "DateTime") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToDateTime(values[i]) : Base.BussinesLayer.Utils.DateTimeNullValue;
else if (columnTypes[i].Name == "TimeSpan") newRow[i] = values[i] != null && values[i] != DBNull.Value ? EvotechUtils.ConvertToTimeSpan(values[i]) : Base.BussinesLayer.Utils.DateTimeNullValue.TimeOfDay;
else if (columnTypes[i].Name == "Int64") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToInt64(values[i]) : Base.BussinesLayer.Utils.Int64NullValue;
else if (columnTypes[i].Name == "Float") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToInt64(values[i]) : Base.BussinesLayer.Utils.FloatNullValue;
else if (columnTypes[i].Name == "Double") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToInt64(values[i]) : Base.BussinesLayer.Utils.DoubleNullValue;
else if (columnTypes[i].Name == "Byte") newRow[i] = values[i] != null && values[i] != DBNull.Value ? Convert.ToInt64(values[i]) : Base.BussinesLayer.Utils.ByteNullValue;
else newRow[i] = DBNull.Value;
}
dtData.Rows.Add(newRow);
}
dr.Close();
}
Chart example :
chcGraph.BeginInit();
chcGraph.BackColor = System.Drawing.Color.Transparent;
chcGraph.Series.Add("GraphSeriesMain", ViewType.Pie);
chcGraph.Series.Swap(chcGraph.Series[0], chcGraph.Series["GraphSeriesMain"]);
chcGraph.Series["GraphSeriesMain"].DataSource = dataTableData;
chcGraph.Series["GraphSeriesMain"].ArgumentDataMember = "ChannelName";
chcGraph.Series["GraphSeriesMain"].ArgumentScaleType = ScaleType.Qualitative;
chcGraph.Series["GraphSeriesMain"].ValueDataMembers.AddRange(new string[] { "RatingValue" });
chcGraph.EndInit();
chcGraph.Refresh();
Upvotes: 1
Reputation:
Do you use the Winforms or WPF version of this components?
I only have experience witch the WPF version and I am not sure how much the WinFormas version differ.
But at the WPF Version I have to set the Values ArgumentDataMember
and ValueDataMember
of my series(template) to specify what columns of my DataTable contain the Argument and Value Data.
So if it is the WPF Chart you have to add something like:
series.ArgumentDataMember = "ChanelName";
series.ValueDataMember = "Rating";
regards
Upvotes: 0
Reputation: 16397
One way to accomplish this is to bind your data to a Pivot Grid, and then bind the pivot grid to the Chart. In the BindingSource
for your chart, you actually select the drop-down and pick the Pivot Grid.
This has the advantage of allowing you to work with the pivot grid at design time (and even run-time) to modify the chart to your exact liking.
Upvotes: 1