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.Windows.Forms.DataVisualization.Charting;
namespace MySerial
{
public partial class FormChart : Form
{
private List<Chart> charts;
private Queue<int> pmDataQueue1 = new Queue<int>(100);
private Queue<int> pmDataQueue2 = new Queue<int>(100);
private Queue<int> pmDataQueue3 = new Queue<int>(100);
private List<Queue<int>> pmDataQueues;
public FormChart()
{
InitializeComponent();
}
private void Setting_MyChartUI_YMaxMin(int max, int min, Chart thischart)
{
thischart.ChartAreas[0].AxisY.Maximum = max * 1.09;
//thischart.ChartAreas[0].AxisY.Minimum = min * 0.91;
thischart.ChartAreas[0].AxisY.Minimum = 0;
}
private void Setting_MyChart_MakingLine(int lineMax, Chart thisChart)
{
#region 添加标识线
StripLine stripLine = new StripLine();
stripLine.Text = lineMax.ToString();//展示文本
stripLine.BackColor = Color.Red;//背景色
stripLine.Interval = 0;//间隔
stripLine.IntervalOffset = lineMax;//偏移量
stripLine.StripWidth = 0.003;//线宽
stripLine.ForeColor = Color.White;//前景色
stripLine.TextAlignment = StringAlignment.Near;//文本对齐方式
thisChart.ChartAreas[0].AxisY.StripLines.Add(stripLine);
#endregion
}
private void Setting_MyChartUI(Chart thisChart)
{
#region 图表内部颜色
thisChart.ChartAreas[0].BackColor = Color.FromArgb(33, 99, 106);
#endregion
#region x,y轴字体设置
thisChart.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.FromArgb(205, 255, 255);
thisChart.ChartAreas[0].AxisX.LabelStyle.Font = new Font("思源黑体 CN Bold", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
#endregion
#region 折线图横线颜色
thisChart.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(205, 255, 255);
#endregion
#region 坐标轴箭头样式
thisChart.ChartAreas[0].AxisX.ArrowStyle = AxisArrowStyle.Triangle;
thisChart.ChartAreas[0].AxisY.ArrowStyle = AxisArrowStyle.Triangle;
#endregion
#region 坐标轴颜色
thisChart.ChartAreas[0].AxisX.LineColor = Color.White;
thisChart.ChartAreas[0].AxisY.LineColor = Color.White;
#endregion
#region x轴相邻间隔大小
thisChart.ChartAreas[0].AxisX.Interval = 1;
#endregion
#region 线条的颜色
thisChart.Series[0].Color = Color.FromArgb(100, 46, 199, 201);
#endregion
#region 点上的颜色
thisChart.Series[0].MarkerColor = Color.Purple;
#endregion
#region 每一个点上数字的颜色
thisChart.Series[0].LabelForeColor = Color.FromArgb(255, 179, 102);
#endregion
#region 在点上显示数字
thisChart.Series[0].IsValueShownAsLabel = true;
#endregion
#region 点上的图案
thisChart.Series[0].MarkerStyle = MarkerStyle.Diamond;
#endregion
#region 包含范围的折线图类型 范围内和线条的颜色一致
thisChart.Series[0].ChartType = SeriesChartType.SplineRange;
#endregion
}
private void timer1_Tick(object sender, EventArgs e)
{
PMdata pMdata = PMdata.GetInstance();
for (int i = 0; i < charts.Count(); i++)
{
Setting_MyChartUI(charts[i]);
}
for (int i = 0; i < charts.Count(); i++)
{
charts[i].ChartAreas[0].AxisY.StripLines.Clear();//清除上一根线
Setting_MyChartUI_YMaxMin(pMdata.pmMax[i], 0, charts[i]);
Setting_MyChart_MakingLine(pMdata.pmMax[i], charts[i]);
}
for (int i = 0; i < charts.Count(); i++)
{
if (pmDataQueues[i].Count() < 10)
{
pmDataQueues[i].Enqueue(int.Parse(pMdata.pmDic[pMdata.pmType[i]]));
}
else
{
pmDataQueues[i].Dequeue();
}
}
#region 折线图刷新
for (int i = 0; i < charts.Count(); i++)
{
charts[i].Series[0].Points.Clear();
}
#endregion
#region 折线图数据刷新
for (int i = 0; i < charts.Count(); i++)
{
for (int j = 0; j < pmDataQueues[i].Count(); j++)
{
charts[i].Series[0].Points.AddXY(j + 2, pmDataQueues[i].ElementAt(j));
if (PointIsOk(pmDataQueues[i].ElementAt(j),i))
{
charts[i].Series[0].Color = Color.FromArgb(100, 46, 199, 201);
}
else
{
charts[i].Series[0].Color = Color.FromArgb(100, 252, 86, 51); ;
}
}
}
#endregion
#region 关闭X轴名称
for (int i = 0; i < charts.Count(); i++)
{
charts[i].ChartAreas[0].AxisX.Enabled = AxisEnabled.False;
}
#endregion
}
private bool PointIsOk(int value,int i)
{
PMdata pMdata = PMdata.GetInstance();
bool isMValueOK = (value < pMdata.pmMax[i]);
if (isMValueOK)
{
return true;
}
return false;
}
private void FormChart_Load(object sender, EventArgs e)
{
charts = new List<Chart> { chart1, chart2, chart3 };
pmDataQueues = new List<Queue<int>>(10) { pmDataQueue1, pmDataQueue2, pmDataQueue3 };
timer1.Enabled = true;
this.TopMost = true;
}
}
}