C# chart with SQLite data

-1

There've been a lot of questions related to C# charts with SQL data but I've found none with SQLite specifically.

Previously, I've managed to make it work with SQL data, but now I'm stuck with 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]
c#
forms
sqlite
charts
asked on Stack Overflow Jul 20, 2020 by Quan Do • edited Jul 20, 2020 by Quan Do

1 Answer

0

I found out there was a Syntax error with the Select query. Thanks @TaW for the guidance.

answered on Stack Overflow Jul 20, 2020 by Quan Do

User contributions licensed under CC BY-SA 3.0