增加柱形图的价值与按钮点击 [英] Increase bar chart values with button clicks

查看:140
本文介绍了增加柱形图的价值与按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个图形,将显示一个锻炼的进度。每五个按钮点击蜱应加入的曲线图。这是它应该如何看一个例子。

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.

这解决方案是基于的WinForms &安培; Panel_Paint()您可以尝试添加垂直进度标签和图表的Y轴值标签

This solution is based on WinForms & Panel_Paint(). You may try to add vertical Progress Label and Chart's Y Axis value labeling.

代码:

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");
                }
            }
        }
    }
}

输出方式:

这篇关于增加柱形图的价值与按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆