Senin, 03 Desember 2018

Program C# Membaca Sensor Suhu : Menggambar Grafik dan Kotak di C#


Berikut ini  contoh membuat  grafik sederhana dgn menggunakan C# smoga bermanfaat




sistim koordinat  image di c#



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;
namespace testgraph
{
public partial class Form1 : Form
{
Random angka_random;
Bitmap surface;
Graphics device;
int y1,y2 = 0;
int x1 = 0;
int x2 = 10;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pb.BackColor = Color.Blue;
angka_random = new Random();
//create graphics device
surface = new Bitmap(this.Size.Width, this.Size.Height);
pb.Image = surface;
device = Graphics.FromImage(surface);
}
private void BtnGaris_Click(object sender, EventArgs e)
{
//make a new font
Font font = new Font(“Times New Roman”, 26, FontStyle.Regular,
GraphicsUnit.Pixel);
Color color = Color.Azure;
//make pen out of color
int width = angka_random.Next(2, 8);
Pen pen = new Pen(color, width);
device.DrawString(“Grafik Temperatur”, font, Brushes.Green , 10, 10 + 10);
//draw the line
//device.DrawLine(pen, x1, y1, x2, y2);
device.DrawLine(pen, 10, 300, 510, 300);   // sumbu X
device.DrawLine(pen, 10, 10, 10, 300);        // sumbu Y
device.DrawLine(pen, 10, 50, 500, 50);      //
//refresh the drawing surface
pb.Image = surface;
}
private void btnKotak_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
device.Dispose();
surface.Dispose();
}
private void timer1_Tick(object sender, EventArgs e)
{
gambar_kotak();
}
private void gambar_kotak()
{
//make a random color
if (x1 > 400) x1 = 0;
if (y1 > 200) y1 = 30;
if (x2 > 400) x2 = 0;
if (y2 > 200) y2 = 30;
x1 = x1 + 40;
x2 = x2 + 40;
y1 = angka_random.Next(1, this.Size.Height);
y2 = angka_random.Next(1, this.Size.Height);
if (y1> 280) y1 = 20;
if (y2> 280) y2 = 20;
Color color1 = Color.White;
Color color2 = Color.Yellow ;
//make pen out of color
Pen pen1 = new Pen(color1, 9);
Pen pen2 = new Pen(color2, 9);
txtdata_x.Text = x1.ToString();
txtdata_y.Text = y1.ToString();
//Rectangle(x,y.widht,hight)
Rectangle rect1 = new Rectangle(x1,y1, 5, 280-y1); //(x,y.widht,hight)
Rectangle rect2 = new Rectangle(x2, y2, 5, 280 – y2); //(x,y.widht,hight)
//draw the rectangle
device.DrawRectangle(pen1, rect1);
device.DrawRectangle(pen2, rect2);
//refresh the drawing surface
pb.Image = surface;
}
private void btnstopTimer_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
}
}
//============
contoh2
//============



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace Charts
{
public partial class Form1 : Form
{
int[] data_graph;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
data_graph = new int[] { 1, 2, 3, 5, 2, 10, 4, 11 };
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawLineChart(e, data_graph);
}
private void DrawLineChart(PaintEventArgs e,int[] data_graph)
{
int banyak_data = data_graph.Length;
int panjang_area = 400;
int tinggi_area = 250;
int topX = 20;
int topY = 40;
int nilai_max = cari_nilai_max(data_graph);
int[] height = new int[banyak_data];
int jumlah_tot_data = cari_jumlah_data(data_graph);
SolidBrush brush = new SolidBrush(Color.Aquamarine);
Pen pen = new Pen(Color.Gray);
Rectangle rec = new Rectangle(topX, topY, panjang_area, tinggi_area);
e.Graphics.DrawRectangle(pen, rec);
pen.Color = Color.Red;
int smallX = topX;
// int smallY = 0;
int smallLength = (panjang_area / (data_graph.Length + 1));
int smallHeight = 0;
Point koordinat1 = new Point();
Point koordinat2 = new Point();
for (int i = 0; i < banyak_data; i++)
{
koordinat1 = koordinat2;
koordinat2.X = koordinat2.X + smallLength;
smallHeight = ((data_graph[i] * tinggi_area) / nilai_max);
koordinat2.Y = topY + tinggi_area – smallHeight;
if (koordinat1.X != 0 && koordinat1.Y != 0)
{
e.Graphics.DrawLine(pen, koordinat1, koordinat2);
}
smallX = smallX + smallLength;
}
}
private static int cari_nilai_max(int[] intArray)
{
int maxVal = intArray[0];
for (int i = 0; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}
private static int cari_jumlah_data(int[] intArray)
{
int sum = 0;
for (int i = 0; i < intArray.Length; i++)
{
sum += intArray[i];
}
return sum;
}
}
}

Tidak ada komentar:

Posting Komentar