简体   繁体   English

C# 图表与 SQLite 数据

[英]C# chart with SQLite data

There've been a lot of questions related to C# charts with SQL data but I've found none with SQLite specifically.有很多与 C# 图表相关的问题与 SQL 数据,但我没有发现 SQLite 数据。

Previously, I've managed to make it work with SQL data, but now I'm stuck with SQLite.以前,我设法使它与 SQL 数据一起工作,但现在我被 SQLite 困住了。

Here are some variables that are linked to combo boxes, which work.以下是一些链接到组合框的变量,它们有效。 I've also created a couple of textboxes to see what value they ouput and they work.我还创建了几个文本框来查看它们输出的值和它们的工作原理。

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]";

Here's my code to add the chart.这是我添加图表的代码。

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(); }
        }

When I ran the chart, no error show up but the chart simply stayed empty and nothing would appear.当我运行图表时,没有出现错误,但图表只是保持空白,什么也不会出现。

I added a few checkboxes to enable the series in real time and started to get errors.我添加了几个复选框以实时启用该系列并开始出现错误。 These are the event handlers这些是事件处理程序

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;
        }

And here is the error这是错误

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:\GitHub\Workout-Tracker-SQLite\Forms\Chart_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:\GitHub\Workout-Tracker-SQLite\Forms\Chart_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:\GitHub\Workout-Tracker-SQLite\Classes\Program.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]

I found out there was a Syntax error with the Select query.我发现 Select 查询存在语法错误。 Thanks @TaW for the guidance.感谢@TaW 的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM