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.Windows.Forms.DataVisualization.Charting;
using app.KeyBoards.Models;
using app.Lib;
using System.IO;
namespace app.DebugForms
{
public partial class BatteryDetect : Form
{
private string _fileName;
public BatteryDetect()
{
InitializeComponent();
_fileName = DateTime.Now.Hour +
"-" + DateTime.Now.Minute +
"-" +
DateTime.Now.Second;
}
private readonly Queue<
double> _dataQueue =
new Queue<
double>(
240);
/// <summary>
/// 初始化图表
/// </summary>
private void InitChart()
{
//定义图表区域
chart1.ChartAreas.Clear();
ChartArea chartArea1 =
new ChartArea(
"C1");
chart1.ChartAreas.Add(chartArea1);
//定义存储和显示点的容器
chart1.Series.Clear();
Series series1 =
new Series(
"S1");
series1.ChartArea =
"C1";
chart1.Series.Add(series1);
//设置图表显示样式
chart1.ChartAreas[
0].AxisY.Minimum =
0;
chart1.ChartAreas[0].AxisY.Maximum =
100;
chart1.ChartAreas[0].AxisY.Interval =
2;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor =
Color.MistyRose;
chart1.ChartAreas[0].AxisX.Minimum =
0;
chart1.ChartAreas[0].AxisX.Maximum =
120;
chart1.ChartAreas[0].AxisX.Interval =
2;
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor =
Color.MistyRose;
//设置标题
chart1.Titles.Clear();
chart1.Titles.Add("S01");
chart1.Titles[0].Text =
"电量显示";
chart1.Titles[0].ForeColor =
Color.RoyalBlue;
chart1.Titles[0].Font =
new Font(
"宋体", 15F);
chart1.Titles[0].Text =
string.Format(
"电池 {0} 显示",
"电量");
//设置图表显示样式
chart1.Series[
0].Color =
Color.Blue;
chart1.Series[0].MarkerSize =
5;
chart1.Series[0].ChartType =
SeriesChartType.Spline;
chart1.Series[0].MarkerStyle =
MarkerStyle.Circle;
//chart1.Series[0].Label = "#VAL"; //设置显示X Y的值
chart1.Series[
0].ToolTip =
"(#VALX,#VAL)";
//鼠标移动到对应点显示数值
chart1.Series[0].Points.Clear();
}
private void timer1_Tick_1(
object sender, EventArgs e)
{
UpdateQueueValue();
chart1.Series[0].Points.Clear();
for (
int i =
0; i < _dataQueue.Count; i++
)
{
chart1.Series[0].Points.AddXY((i), _dataQueue.ElementAt(i));
}
WriteFile();
}
private void btnStart_Click_1(
object sender, EventArgs e)
{
timer1.Start();
}
private void btnStop_Click_1(
object sender, EventArgs e)
{
timer1.Stop();
}
private int num =
1;
//每次删除增加几个点
private int page =
1;
//每次删除增加几个点
//更新队列中的值
private void UpdateQueueValue()
{
if (_dataQueue.Count >
120)
{
////先出列
//for (int i = 0; i < num; i++)
//{
// _dataQueue.Dequeue();
//}
_dataQueue.Clear();
label1.Text =
string.Format(
"Page: {0}", (++
page));
}
for (
int i =
0; i < num; i++
)
{
if (SingleChipVersion.SingleChip.BatteryPowerPercent!=
null)
_dataQueue.Enqueue(int.Parse(SingleChipVersion.SingleChip.BatteryPowerPercent));
}
}
private void BatteryDetect_Load(
object sender, EventArgs e)
{
InitChart();
//_dataQueue.Enqueue(10);
if (SingleChipVersion.SingleChip.BatteryPowerPercent!=
null)
{
_dataQueue.Enqueue(int.Parse(SingleChipVersion.SingleChip.BatteryPowerPercent));
chart1.Series[0].Points.Clear();
for (
int i =
0; i < _dataQueue.Count; i++
)
{
chart1.Series[0].Points.AddXY(
0, _dataQueue.ElementAt(
0));
}
}
}
/// <summary>
/// 写、读文件
/// </summary>
///
void WriteFile()
{
if (_dataQueue.Count >
0)
{
string pathUrl = Application.StartupPath +
"\\BatteryLog\\";
string filePath = pathUrl + _fileName +
".txt";
string contentInString = DateTime.Now.ToString(
"T") +
"电量:" + _dataQueue.ElementAt(_dataQueue.Count -
1);
Logger.Log("_dataQueue.Count - 1 :" + _dataQueue.ElementAt(_dataQueue.Count -
1),Level.Info);
try
{
if (!
Directory.Exists(pathUrl))
{
Directory.CreateDirectory(pathUrl);
}
FileStream file =
new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
using (StreamWriter writer1 =
new StreamWriter(file))
{
file.Position =
file.Length;
writer1.WriteLine(contentInString);
writer1.Close();
file.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message +
ex.StackTrace);
}
}
}
}
}
View Code
转载于:https://www.cnblogs.com/fengbaobao/p/11405558.html
相关资源:JAVA上百实例源码以及开源项目