You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework下无需数据库直接将电池数据展示到Chart控件

WinForms电池效率计:无需数据库实现Chart控件展示电池百分比

问题背景

我正在开发一个WinForms电池效率计项目,目前已通过以下代码将计时时长与电池百分比(Percentage.Text)添加到ListBox中:

listBox1.Items.Add(num + ".  " + lbl_Time.Text + " =   " + Percentage.Text);

希望不通过数据库,直接将电池百分比数据展示到Chart控件中,询问是否有可行方法。

解决方案

最终通过调用Chart控件的Points.AddXY方法实现需求,核心代码如下:

chart1.Series["battery"].Points.AddXY(num, ps.BatteryLifePercent * 100);

原项目完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace battery_efficiency_meter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            PowerStatus ps = SystemInformation.PowerStatus;

            PercentageBar.Value = (int)(ps.BatteryLifePercent * 100);

            if (ps.BatteryLifeRemaining < 0)
                TimeLabel.Text = "Charging";
            else
                TimeLabel.Text = "Remaining Time = " + new TimeSpan(0 , 0 , ps.BatteryLifeRemaining);

            Percentage.Text = string.Format($"{ps.BatteryLifePercent * 100} %");            
        }

        // Stopwatch
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

        private void Start_But_Click(object sender, EventArgs e)
        {
            Stopwatch_Timer.Start();
            stopwatch.Start();
            //Start_But.Enabled= false;
        }

        private void Stopwatch_Timer_Tick(object sender, EventArgs e)
        {
            TimeSpan Lap = stopwatch.Elapsed;
            lbl_Time.Text = string.Format("{0:00} : {1:00} : {2:00} : {3:00}",
                Math.Floor(Lap.TotalHours), Lap.Minutes, Lap.Seconds, Lap.Milliseconds);
        }

        int num = 0;

        private void Lap_But_Click(object sender, EventArgs e)
        {
            num += 1;
            listBox1.Items.Add(num + ".  " + lbl_Time.Text + " =   " + Percentage.Text);
        }

        private void Stop_But_Click(object sender, EventArgs e)
        {
            Stopwatch_Timer.Stop();
            stopwatch.Stop();
            //Start_But.Enabled= false;
        }

        private void Reset_But_Click(object sender, EventArgs e)
        {
            stopwatch.Reset();
            lbl_Time.Text = "00 : 00 : 00 : 000";
            listBox1.Items.Clear();
            num = 0;
            //Start_But.Enabled= false;
        }
    }
}

修改后的核心实现代码

int num = 0;
int num2 =+ 1;

private void Lap_But_Click(object sender, EventArgs e)
{
    PowerStatus ps = SystemInformation.PowerStatus;
    //num +=1;
    listBox1.Items.Add(num2 + ".  " + lbl_Time.Text + " =   " + Percentage.Text);

    var batteryLifePercent = Convert.ToInt32(ps.BatteryLifePercent);            
    chart1.Series["battery"].Points.AddXY(num,ps.BatteryLifePercent * 100);
}

内容的提问来源于stack exchange,提问作者Irvan_prima

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:15:36