Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
How do you update a pie chart in real time in C#?
Posted on: 25 Nov 2012
Hi, I am trying to create a pie chart that updates whenever the values of the database it is connected to are changed. But everything I tried so far is not making it happen. Any suggestions on what I should do? Here is the code (I know the code is probably a bit messy but I am still a novice):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace HCI_Application
{
    public partial class Nutritional : Form
    {
        int carb = 0;
        int protein = 0;
        int fat = 0;
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Event Driven Programming\\HCI Application\\Nutrients.accdb");
        public Nutritional()
        {
            InitializeComponent();
        }

        private void chart1_Click(object sender, EventArgs e)
        {
        }

        private void Nutritional_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nutrientsDataSet.Nutrients' table. You can move, or remove it, as needed.
            this.nutrientsTableAdapter.Fill(this.nutrientsDataSet.Nutrients);
            cmbDiet.Items.Add("Standard Diet (60-30-10)");
            cmbDiet.Items.Add("Ketogenic Diet (10-45-45)");
            cmbDiet.Items.Add("Bodybuilding Diet (50-30-20)");
            cmbDiet.Items.Add("High Protein Diet (25-50-25)");
            cmbDiet.Items.Add("Zone Diet (40-30-30)");
            cmbDiet.SelectedIndex = 0;    
        }

        private void cmbDiet_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbDiet.SelectedIndex)
            {
                case 0:
                    carb = 60;
                    protein = 30;
                    fat = 10;
                    break;
                case 1:
                    carb = 10;
                    protein = 45;
                    fat = 45;
                    break;
                case 2:
                    carb = 50;
                    protein = 30;
                    fat = 20;
                    break;
                case 3:
                    carb = 25;
                    protein = 50;
                    fat = 25;
                    break;
                case 4:
                    carb = 40;
                    protein = 30;
                    fat = 30;
                    break;  
            }

            connection.Open();

            OleDbCommand updateValues = new OleDbCommand("UPDATE Nutrients SET Percentage='" + carb + "' WHERE Nutrient = 'Carbohydrates'", connection);
            updateValues.ExecuteNonQuery();

            updateValues.CommandText = "UPDATE Nutrients SET Percentage='" + protein + "' WHERE Nutrient = 'Protein'";
            updateValues.ExecuteNonQuery();

            updateValues.CommandText= "UPDATE Nutrients SET Percentage='" + fat + "' WHERE Nutrient = 'Fat'";
            updateValues.ExecuteNonQuery();

            DrawChart();
            connection.Close();
        }

        private void DrawChart()
        {
            DietChart.Series["NutrientSeries"].XValueMember = "Nutrient";
            DietChart.Series["NutrientSeries"].YValueMembers = "Percentage";
        }
    }
}

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.