通过按钮点击增加条形图值 [英] Increase bar chart values with button clicks
问题描述
我想创建一个图表,显示锻炼的进度。每五个按钮点击一个刻度应该添加到图表。这是一个应该如何看的例子。
为了演示的目的,我使用了一个按钮点击,在生产中点击将是每轮20转的轮子。
private int counter = 0;
private void button1_Click(object sender,EventArgs e)
{
counter ++;
//代码将在这里
}
>
您可以使用 Bitmap Buffer
或面板
来绘制。这是一个头条:只是一个示例。
参考资料。 p>
此解决方案基于 WinForms
& Panel_Paint()
。 您可以尝试添加垂直进度标签和图表的Y轴值标签。
代码:
使用系统;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
命名空间WindowsFormsApplication1
{
public partial class Form1:Form
{
public Form1(){
InitializeComponent
}
private int counter = 0;
private int px = 10;
private int py = 180;
private int total5Clicks = 0;
private void button1_Click(object sender,EventArgs e)
{
counter ++;
label1.Text =Total Clicks =+ counter.ToString();
if(Math.Abs(counter%5)== 0){
if(Math.Abs(counter / 5)> 0){
total5Clicks = total5Clicks + 1;
PaintOnChartPanel(total5Clicks);}
}
}
private void panel1_Paint(object sender,PaintEventArgs e){
}
private void PaintOnChartPanel(int total5Times)
{
//添加一个新的Panel Paint EventHandler
panel1.Paint + = new PaintEventHandler(panel1_Paint);
使用(Graphics g = this.panel1.CreateGraphics())
{
刷刷= new SolidBrush(Color.Green);
g.FillRectangle(brush,px,py,20,20);
pen pen = new Pen(new SolidBrush(Color.White));
g.DrawRectangle(pen,px,py,20,20);
//添加每个total5点击进入图块
g.DrawString((total5Times).ToString(),new Font(Arial,7),
new SolidBrush .AntiqueWhite),
px + 1,py + 8,StringFormat.GenericDefault);
pen.Dispose();}
if(py> 20){
py = py - 20;}
else {
MessageBox。显示(到达面板的顶部);
if(px <200){
px = px + 20;
py = 180;}
else {
MessageBox.Show(Reached Right of the Panel);
}
}
}
}
}
$ b b
输出表单:
I am trying to create a graph that will show the progress of a workout. Every five button clicks a tick should be added to the graph. This is a example of how it should look.
For demonstration purposes I am using a button click, In production the clicks will be every twenty revolutions of a wheel.
private int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
counter++;
// code will go here
}
Thanks in advance
You can use either a Bitmap Buffer
or a panel
to draw on. Here is a headstart: Just a sample.
reference.
This solution is based on WinForms
& Panel_Paint()
. You may try to add vertical Progress Label and Chart's Y Axis value labeling.
Code:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1(){
InitializeComponent();
}
private int counter = 0;
private int px = 10;
private int py = 180;
private int total5Clicks = 0;
private void button1_Click(object sender, EventArgs e)
{
counter++;
label1.Text = "Total Clicks = " + counter.ToString();
if (Math.Abs(counter % 5) == 0){
if (Math.Abs(counter / 5) > 0){
total5Clicks = total5Clicks + 1;
PaintOnChartPanel(total5Clicks);}
}
}
private void panel1_Paint(object sender, PaintEventArgs e){
}
private void PaintOnChartPanel(int total5Times)
{
//Add a new Panel Paint EventHandler
panel1.Paint += new PaintEventHandler(panel1_Paint);
using (Graphics g = this.panel1.CreateGraphics())
{
Brush brush = new SolidBrush(Color.Green);
g.FillRectangle(brush, px, py, 20, 20);
Pen pen = new Pen(new SolidBrush(Color.White));
g.DrawRectangle(pen, px, py, 20, 20);
//add each total5Click into chart block
g.DrawString((total5Times).ToString(), new Font("Arial", 7),
new SolidBrush(Color.AntiqueWhite),
px + 1, py+8, StringFormat.GenericDefault);
pen.Dispose();}
if (py > 20){
py = py - 20;}
else{
MessageBox.Show("Reached Top of the Panel");
if (px < 200){
px = px + 20;
py = 180;}
else{
MessageBox.Show("Reached Right of the Panel");
}
}
}
}
}
Output Form:
这篇关于通过按钮点击增加条形图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!