带有SQLite数据的C#图表



有很多关于SQL数据的C#图表的问题,但我没有发现任何关于SQLite的问题。

以前,我已经设法使它与SQL数据一起工作,但现在我被SQLite卡住了。

以下是一些链接到组合框的变量,它们起作用。我还创建了几个文本框来查看它们的输出和工作价值。

SQLiteConnection con = new SQLiteConnection(DataAccess.ConString("Default"));
IDbConnection cn = new SQLiteConnection(DataAccess.ConString("Default"));
string eID = "";
string pID = "";
string baseQuery = "SELECT [Date], [Full_Name], [Exercise_Name], [Total_Sets], [Total_Reps], [Average_Reps], [Average_Weight] FROM [Daily Progress] dp, [Person] p, [Exercise Details] ed " +
"WHERE  (dp.[Exercise_ID] = ed.[Exercise_ID] AND dp.[Person_ID] = p.[Person_ID]) ";
string eFilter = " AND ed.[Exercise_ID] = ";
string pFilter = " AND p.[Person ID] = ";
string dateOrder = " ORDER BY [Date]";

这是我添加图表的代码。

private void loadChart()
{
try
{
chart1.Titles.Clear();
chart1.Series.Clear();
SQLiteCommand cmd = new SQLiteCommand(baseQuery + eFilter + eID + pFilter + pID + dateOrder, con);
con.Open();
SQLiteDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
chart1.DataBind();
con.Close();
chart1.DataSource = dt;              

cb_Total_Days.DataSource = dt;
cb_Total_Days.DisplayMember = "Workout_Days";

chart1.Titles.Add(cb_Workout_Name.SelectedText.ToString());
chart1.Titles.Add(cb_Person_Name.SelectedText.ToString());

chart1.Series.Add("Total Sets");
chart1.Series["Total Sets"].XValueMember = "Date";
chart1.Series["Total Sets"].YValueMembers = "Total_Sets";
chart1.Series["Total Sets"].ChartType = SeriesChartType.Column;
chart1.Series["Total Sets"]["PixelPointWidth"] = "20";
chart1.Series["Total Sets"].IsValueShownAsLabel = true;
chart1.Series["Total Sets"].LabelBorderWidth = 3;
chart1.Series["Total Sets"].SmartLabelStyle.Enabled = true;
chart1.Series.Add("Average Reps");
chart1.Series["Average Reps"].XValueMember = "Date";
chart1.Series["Average Reps"].YValueMembers = "Average_Reps";
chart1.Series["Average Reps"].ChartType = SeriesChartType.Spline;
chart1.Series["Average Reps"].BorderWidth = 5;
chart1.Series["Average Reps"].IsValueShownAsLabel = true;
chart1.Series["Average Reps"].LabelBorderWidth = 3;
chart1.Series["Average Reps"].MarkerStyle = MarkerStyle.Cross;
chart1.Series["Average Reps"].MarkerSize = 15;
chart1.Series["Average Reps"].SmartLabelStyle.Enabled = true;
chart1.Series.Add("Average Weight");
chart1.Series["Average Weight"].XValueMember = "Date";
chart1.Series["Average Weight"].YValueMembers = "Average_Weight";
chart1.Series["Average Weight"].ChartType = SeriesChartType.Spline;
chart1.Series["Average Weight"].BorderWidth = 5;
chart1.Series["Average Weight"].IsValueShownAsLabel = true;
chart1.Series["Average Weight"].LabelBorderWidth = 3;
chart1.Series["Average Weight"].MarkerStyle = MarkerStyle.Star10;
chart1.Series["Average Weight"].MarkerSize = 15;
chart1.Series["Average Weight"].SmartLabelStyle.Enabled = true;
chart1.Series.Add("Total Reps");
chart1.Series["Total Reps"].XValueMember = "Date";
chart1.Series["Total Reps"].YValueMembers = "Total_Reps";
chart1.Series["Total Reps"].ChartType = SeriesChartType.Spline;
chart1.Series["Total Reps"].BorderWidth = 5;
chart1.Series["Total Reps"].IsValueShownAsLabel = true;
chart1.Series["Total Reps"].LabelBorderWidth = 3;
chart1.Series["Total Reps"].MarkerStyle = MarkerStyle.Diamond;
chart1.Series["Total Reps"].SmartLabelStyle.Enabled = true;
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "d-MMM";
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
chart1.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dot;

}
catch (Exception) { con.Close(); }
finally { con.Close(); }
}

当我运行图表时,没有出现任何错误,但图表只是保持为空,什么也不会出现。

我添加了几个复选框来实时启用该系列,并开始出现错误。这些是事件处理程序

private void chk_TotalSets_CheckedStateChanged(object sender, EventArgs e)
{
Series ts = chart1.Series["Total Sets"];
ts.Enabled = chk_Total_Sets.Checked;
}
private void chk_TotalReps_CheckStateChanged(object sender, EventArgs e)
{
Series tr = chart1.Series["Total Reps"];
tr.Enabled = chk_Total_Reps.Checked;
}
private void chk_AvgReps_CheckStateChanged(object sender, EventArgs e)
{
Series ar = chart1.Series["Average Reps"];
ar.Enabled = chk_Avg_Reps.Checked;
}
private void chk_AvgWeight_CheckStateChanged(object sender, EventArgs e)
{
Series aw = chart1.Series["Average Weight"];
aw.Enabled = chk_Avg_Weight.Checked;
}

这是错误

System.ArgumentException
HResult=0x80070057
Message=A chart element with the name 'Average Reps' could not be found in the 'SeriesCollection'.
Source=System.Windows.Forms.DataVisualization
StackTrace:
at System.Windows.Forms.DataVisualization.Charting.ChartNamedElementCollection`1.get_Item(String name)
at Workout_Tracker_SQLite.Visualization.chk_AvgReps_CheckStateChanged(Object sender, EventArgs e) in C:GitHubWorkout-Tracker-SQLiteFormsChart_Form.cs:line 188
at System.Windows.Forms.CheckBox.OnCheckStateChanged(EventArgs e)
at System.Windows.Forms.CheckBox.set_CheckState(CheckState value)
at System.Windows.Forms.CheckBox.set_Checked(Boolean value)
at Workout_Tracker_SQLite.Visualization.btn_LoadChart_Click(Object sender, EventArgs e) in C:GitHubWorkout-Tracker-SQLiteFormsChart_Form.cs:line 203
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Workout_Tracker_SQLite.Program.Main() in C:GitHubWorkout-Tracker-SQLiteClassesProgram.cs:line 21
This exception was originally thrown at this call stack:
System.Windows.Forms.DataVisualization.Charting.ChartNamedElementCollection<T>.this[string].get(string)
Workout_Tracker_SQLite.Visualization.chk_AvgReps_CheckStateChanged(object, System.EventArgs) in Chart_Form.cs
System.Windows.Forms.CheckBox.OnCheckStateChanged(System.EventArgs)
System.Windows.Forms.CheckBox.CheckState.set(System.Windows.Forms.CheckState)
System.Windows.Forms.CheckBox.Checked.set(bool)
Workout_Tracker_SQLite.Visualization.btn_LoadChart_Click(object, System.EventArgs) in Chart_Form.cs
System.Windows.Forms.Control.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnClick(System.EventArgs)
System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs)
System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message, System.Windows.Forms.MouseButtons, int)
...
[Call Stack Truncated]

我发现Select查询有语法错误。感谢@TaW的指导。

最新更新