|
|
|
winApp C# pictruebox / Graphics.DrawLine ผมอยากได้ประมาณนี้ครับ แต่งงๆไม่รู้จะเขียนโค้ดยังไง |
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : ฺBundit เมื่อวันที่ 2018-05-10 09:12:17
รายละเอียดของการตอบ ::
ขอบคุณมากครับ
เดี๋ยวลองดูก่อนนะครับ
โค้ดผมหลักๆจะมาณนี้ครับ (ใช้สำหรับทำ control chart เพื่อควบคุมงาน)
Code (C#)
namespace TORServices.FormsTor
{
public class DataControlChart
{
public string _Man; public DateTime _date; public double _value;
public DataControlChart(DateTime d, string m, double v)
{
_Man = m; _date = d; _value = v;
}
}
public partial class controlChart : UserControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.panel1 = new System.Windows.Forms.Panel();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Black;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(621, 300);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Black;
this.pictureBox2.Dock = System.Windows.Forms.DockStyle.Left;
this.pictureBox2.Location = new System.Drawing.Point(0, 0);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(129, 346);
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(129, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(747, 346);
this.panel1.TabIndex = 1;
//
// controlChart
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panel1);
this.Controls.Add(this.pictureBox2);
this.Name = "controlChart";
this.Size = new System.Drawing.Size(876, 346);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
// Set value
double _avg = 0;
double _sd = 0;
private PictureBox pictureBox1;
private PictureBox pictureBox2;
private Panel panel1;
private int SizeColumn = 50;
public controlChart()
{
InitializeComponent();
}
#region _Property
private int _digi = 4;
[System.ComponentModel.Browsable(true),
System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always),
System.ComponentModel.Category("Behavior")]
public int Decimal
{
get { return _digi; }
set { _digi = value; RefreshChart(); }
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
[System.ComponentModel.Category("Behavior")]
public double ValueMean
{
get { return _avg; }
set { _avg = value; RefreshChart(); }
}
[System.ComponentModel.Browsable(true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)]
[System.ComponentModel.Category("Behavior")]
public double ValueSD
{
get { return _sd; }
set { _sd = value; RefreshChart(); }
}
#endregion
public List<DataControlChart> dataControlChart;
public void AddData(DataControlChart value)
{
if (dataControlChart == null) dataControlChart = new List<DataControlChart>();
dataControlChart.Add((DataControlChart)value);
// RefreshChart();
}
public void AddRangeData(List<DataControlChart> value)
{
if (dataControlChart == null) dataControlChart = new List<DataControlChart>();
dataControlChart.AddRange((List<DataControlChart>)value);
// RefreshChart();
}
public void RemoveData(int item)
{
if (dataControlChart == null) return;
dataControlChart.RemoveAt(item);
RefreshChart();
}
public void RefreshChart()
{
if (dataControlChart == null || dataControlChart.Count < 1) return;
// Set PictureBox
pictureBox1.Image = null;
pictureBox1.Invalidate();
// Set value
double vUp3SD = _avg + (3 * _sd);
double vUp2SD = _avg + (2 * _sd);
double vDwn2SD = _avg - (2 * _sd);
double vDwn3SD = _avg - (3 * _sd);
int lntMain = Convert.ToInt32(pictureBox1.Height / 2);
int lntUp3SD = lntMain - Convert.ToInt32(lntMain / 1.2);
int lntUp2SD = lntMain - Convert.ToInt32(lntMain / 2);
int lntDwn2SD = lntMain + Convert.ToInt32(lntMain / 2);
int lntDwn3SD = lntMain + Convert.ToInt32(lntMain / 1.2);
double ddd = (lntDwn3SD - lntUp3SD) / (vUp3SD - vDwn3SD);
pictureBox1.Width= ((dataControlChart.Count + 2) * SizeColumn <= this.panel1.Width) ?
this.panel1.Width : (dataControlChart.Count + 2) * SizeColumn;
this.pictureBox1.Height = this.panel1.Height - 20;
this.pictureBox2.Height = this.panel1.Height - 20;
Bitmap b = new Bitmap(pictureBox1.Width, this.pictureBox1.Height);
Bitmap b1 = new Bitmap(pictureBox2.Size.Width, pictureBox2.Size.Height);
using (Graphics g = Graphics.FromImage(b1))
{
g.Clear(pictureBox1.BackColor);
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, b.Width, b.Height));
System.Drawing.Pen p;
g.DrawString(" Main--" + _avg.ToString("N" + _digi), this.Font, Brushes.Azure, 5, lntMain - 5);
g.DrawString(" Main - 2SD--" + vDwn2SD.ToString("N" + _digi), this.Font, Brushes.Azure, 5, lntDwn2SD - 5);
g.DrawString(" Main + 2SD--" + vUp2SD.ToString("N" + _digi), this.Font, Brushes.Azure, 5, lntUp2SD - 5);
g.DrawString(" Main -3SD--" + vDwn3SD.ToString("N" + _digi), this.Font, Brushes.Azure, 5, lntDwn3SD - 5);
g.DrawString(" Main + 3SD--" + vUp3SD.ToString("N" + _digi), this.Font, Brushes.Azure, 5, lntUp3SD - 5);
}
pictureBox2.BackgroundImage = b1;
using (Graphics g = Graphics.FromImage(b))
{
g.Clear(pictureBox1.BackColor);
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, b.Width, b.Height));
System.Drawing.Pen p;
g.DrawLine( new System.Drawing.Pen(Brushes.Pink, 3), 0, lntMain, b.Width, lntMain);
g.DrawLine(new System.Drawing.Pen(Brushes.Yellow, 2), 0, lntDwn2SD, b.Width, lntDwn2SD);
g.DrawLine(new System.Drawing.Pen(Brushes.Yellow, 2), 0, lntUp2SD, b.Width, lntUp2SD);
g.DrawLine( new System.Drawing.Pen(Brushes.Red, 2), 0, lntDwn3SD, b.Width, lntDwn3SD);
g.DrawLine(new System.Drawing.Pen(Brushes.Red, 2), 0, lntUp3SD, b.Width, lntUp3SD);
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
x1 = 0;
x2 = SizeColumn + Convert.ToInt32(SizeColumn / 2.2);
y1 = lntMain;
y2 = 0;
p = new System.Drawing.Pen(Brushes.Blue, 5);
p.EndCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
p.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
double c = (lntDwn3SD - lntUp3SD) / (vUp3SD - vDwn3SD);
if (dataControlChart != null && dataControlChart.Count > 0)
{
for (int i = 0; i <= dataControlChart.Count - 1; i++)
{
if (i > 0)
{
x2 = x2 + SizeColumn;
}
if (!string.IsNullOrEmpty("" + dataControlChart[i]._value))
{
try
{
// MessageBox.Show(ddd +"\n "+ vUp3SD + "\n "+ (double)dataControlChart[i]._value + "\n"+ lntUp3SD);
y2 = (int)((ddd * (vUp3SD - (double)dataControlChart[i]._value)) + lntUp3SD);
// if(y2>pictureBox1.Height)
g.DrawLine(p, x1, y1, x2, y2);
g.DrawString(((double)dataControlChart[i]._value).ToString("N" + _digi) + Environment.NewLine +
dataControlChart[i]._date.ToString("dd/MM/yyyy"),
this.Font, Brushes.White, x2 + 5, y2);
x1 = x2;
y1 = y2;
}
catch { }
}
}
}
}
pictureBox1.BackgroundImage = b;
}
private void controlChart_Resize(object sender, EventArgs e)
{
RefreshChart();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
pictureBox1.Height = this.panel1.Height;
}
}
}
ใช้งาน
Code (C#)
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
double d =Convert.ToDouble(new Random().Next(250, 301))/Convert.ToDouble(10000);
mainSDPanel1.Add(d);
System.Threading.Thread.Sleep(50);
}
mainSDPanel1.RefreshData();
}
เผื่อท่านใดมองโค้ดแล้วดูว่าควรแก้ไขตรงไหนนะครับ
|
|
|
|
|
Date :
2018-05-11 09:25:58 |
By :
lamaka.tor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|