|
[C#]
foreach (ChartSeries series in this.chartControl1.Series)
{
series.SeriesModel = new SQLiteDataBindingModel(connectionString, command, "Year", new string[]{ "Sales" });
}
|
|
[C#]
/// <summary>
/// Custom implementation of IChartSeriesModel to directly bind with SQLite data base
/// </summary>
public class SQLiteDataBindingModel : IChartSeriesModel, IDisposable
{
#region Private variables
DataTable table = null;
string m_connectionString = "";
string m_SelectCommand = "";
#endregion
#region Constructor
/// <summary>
/// Constructor to get the required parameters for database connection
/// </summary>
/// <param name="connectionString">Connection string to connect with the database</param>
/// <param name="command">Command to retrieve data from database</param>
/// <param name="xName">Field name in table corresponding to X value in chart series</param>
/// <param name="yNames">Field name in table corresponding to Y value in chart series</param>
public SQLiteDataBindingModel(string connectionString, string command, string xName, string[] yNames)
{
XName = xName;
YNames = yNames;
ConnectionString = connectionString;
Command = command;
}
#endregion
#region Public Properties
/// <summary>
/// Connection string for the database
/// </summary>
public string ConnectionString {
get {
return m_connectionString;
}
set {
m_connectionString = value;
if (!string.IsNullOrEmpty(m_connectionString) && !string.IsNullOrEmpty(m_SelectCommand))
{
this.DataBind();
}
}
}
/// <summary>
/// Command to be executed on the database
/// </summary>
public string Command
{
get
{
return m_SelectCommand;
}
set
{
m_SelectCommand = value;
if (!string.IsNullOrEmpty(m_connectionString) && !string.IsNullOrEmpty(m_SelectCommand))
{
this.DataBind();
}
}
}
/// <summary>
/// Field name to bind with X values in chart series
/// </summary>
public string XName { get; set; }
/// <summary>
/// Field name to bind with Y values in chart series
/// </summary>
public string[] YNames { get; set; }
#endregion
#region Public Methods
/// <summary>
/// Opens connection, executes command and retrieves the data from SQLite data base
/// </summary>
public void DataBind()
{
try
{
SQLiteDataAdapter adapter = new SQLiteDataAdapter(Command, ConnectionString);
if (table == null)
table = new DataTable();
else
{
table.Clear();
}
adapter.Fill(table);
adapter.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region IChartSeriesModel implementation
/// <summary>
/// Data count
/// </summary>
public int Count
{
get
{
if (table != null)
{
return table.Rows.Count;
}
return 0;
}
}
public event ListChangedEventHandler Changed;
/// <summary>
/// Checks for empty data
/// </summary>
/// <param name="xIndex"></param>
/// <returns></returns>
public bool GetEmpty(int xIndex)
{
//Modify this code if empty data is returned from database
return table != null ? xIndex >= table.Rows.Count : true;
}
/// <summary>
/// Gets the X value in data table and bind with chart series
/// </summary>
/// <param name="xIndex">Index of the data point</param>
/// <returns>X value of the data point</returns>
public double GetX(int xIndex)
{
if (table != null)
{
if (!string.IsNullOrEmpty(XName))
return double.Parse(table.Rows[xIndex][XName].ToString());
else
return xIndex;
}
else
throw new Exception("Call DataBind method before accessing X values");
}
/// <summary>
/// Gets the Y values from data table and bind it with chart series
/// </summary>
/// <param name="yIndex">Index of the data point</param>
/// <returns>Y values of the data point</returns>
public double[] GetY(int yIndex)
{
if (table != null)
{
int count = YNames.Length, index = 0;
if (count > 0)
{
double[] result = new double[count];
foreach (string yName in YNames)
{
try
{
result[index++] = double.Parse(table.Rows[yIndex][yName].ToString());
}
catch (Exception ex)
{
throw ex;
}
}
return result;
}
else
throw new Exception("A field name for Y value must be specified");
}
else
throw new Exception("Call DataBind method before accessing Y values");
}
#endregion
#region IDisposable implementation
/// <summary>
/// Dispose the data table after using it
/// </summary>
public void Dispose()
{
if (table != null)
{
table.Dispose();
table = null;
}
}
#endregion
}
|
Hi Ceniza,We would like to let you know once again that the Windows Forms Chart has in-built support for binding the chart with a BindingSource, DataTable, DataSet, implementations of IEnumerable and IList. Hence, we need to convert our DB to any one of these formats as discussed above.Please check the guidelines described above and check below links for example.Query: There's a comment in the cross point where the temperature reading is above the normal temperatureThis will be achieved through cross hair interaction support or can draw custom view, please check the guidelines below.Regards,Saravanan.
|
//Get the data from database and store it in data table
string connectionString = @"Data Source=ChartData.sdf";
string queryString = "Select * from SimpleTable";
SqlCeConnection connection = new SqlCeConnection(connectionString);
SqlCeCommand command = new SqlCeCommand(queryString, connection);
SqlCeDataAdapter adapter = new SqlCeDataAdapter(command);
DataTable data = new DataTable();
adapter.Fill(data);
#region Chart control for tab 1
//Bind data table with Chart Series
ChartSeries series = new ChartSeries("Series", ChartSeriesType.Column);
series.SeriesModel = new ChartDataBindModel(data) {
//Name of the field in data source to bind with X-values (values along X-axis)
XName = "LineNo",
//Name of the field in data source to bind with Y-values (values along Y-axis)
//Here Y value is an array because series like HiLoOpenClose, Candle, etc.., requires more than 1 Y value
YNames = new string[]{ "StdWt" } };
chart.Series.Add(series); |
Hi Mel Ceniza,We have modified the simple chart sample with SQL server data by removing the Combo box for selected item change the chart data. Please find the modified sample from the below link.Regards,Yuvaraj

|
List<double> yValueList = new List<double>();
foreach (DataRow row in data.Rows)
{
yValueList.Add((double)row.ItemArray[1]);
}
ChartCustomPoint cp = new ChartCustomPoint();
cp.Alignment = ChartTextOrientation.Up;
cp.PointIndex = yValueList.IndexOf(yValueList.Max());
cp.SeriesIndex = 0;
cp.Text = "Highest Point";
cp.CustomType = Syncfusion.Windows.Forms.Chart.ChartCustomPointType.PointFollow;
cp.Symbol.Shape = ChartSymbolShape.Diamond;
cp.Offset = 10;
cp.Font.Bold = true;
cp.Font.Facename = "Verdana";
cp.Font.Size = 10F;
this.chartControl1.CustomPoints.Add(cp);
this.chartControl1.Series.Add(series); |
|
this.chart.PrimaryXAxis.RangePaddingType = ChartAxisRangePaddingType.None;
|

|
var primaryXAxis = this.chartControl1.PrimaryXAxis;
this.chartControl1.PrimaryXAxis.RangeType = ChartAxisRangeType.Set;
List<ChartPoint> chartPoints = new List<ChartPoint>();
for(int i=0; i < series1.Points.Count; i++)
{
chartPoints.Add(series1.Points[i]);
}
var minDate = new DateTime(chartPoints.Min(s => s.DateX.Ticks));
var MaxDate = new DateTime(chartPoints.Max(s => s.DateX.Ticks));
this.chartControl1.PrimaryXAxis.DateTimeRange = new ChartDateTimeRange(minDate, MaxDate, primaryXAxis.DesiredIntervals, primaryXAxis.IntervalType); |

|
chartseries.Style.Interior = new BrushInfo(Color.Blue);
|