如何实时更新饼图? [英] How Do You Update A Pie Chart In Real Time?

查看:169
本文介绍了如何实时更新饼图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个饼图,该饼图在其所连接的数据库的值更改时都会更新.但是到目前为止,我所做的一切都没有实现.关于我应该做什么的任何建议?


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?


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)
        {
            int maintenance = Convert.ToInt32(CalorieCalculator.intake);
            int fatloss = maintenance - 500;
            int gain = maintenance + 500;

            lblIntake.Text = "BMR: " + CalorieCalculator.intake.ToString();
            lblMaintenance.Text = "Maintenance: " + maintenance.ToString();
            lblFatloss.Text = "Fat Loss: " + fatloss.ToString();
            lblGain.Text = "Weight Gain: " + gain.ToString();

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

推荐答案

在WPF中,您可以通过观察数据的每次更改自动反映在UI中来观察集合>:)
In WPF you have observable collection by using that each change on data reflects in UI automatically >:)


这篇关于如何实时更新饼图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆