Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase bar chart values with button clicks

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.

1

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

like image 608
user973671 Avatar asked Dec 22 '12 15:12

user973671


People also ask

How do I add values to a bar chart in Excel?

You can use cell values as data labels for your chart. Right-click the data series or data label to display more data for, and then click Format Data Labels. Click Label Options and under Label Contains, select the Values From Cells checkbox.

How do you customize a bar graph?

Use the Chart Styles button to quickly change the color or style of the chart. Click the chart you want to change. In the upper-right corner, next to the chart, click Chart Styles. Click Color and pick the color scheme you want, or click Style and pick the option you want.

How do you make a bar graph smaller for higher numbers?

The workaround: Convert the chart to a vertical column chart, make the "Categories in reverse order" and "Crosses: at maximum category" selections in the Format Axis pop-up, then convert back to a horizontal bar chart.


2 Answers

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:

enter image description here

like image 170
bonCodigo Avatar answered Sep 29 '22 07:09

bonCodigo


You can determine if you have a multiple of five with

bool drawTickMark = clicks % 5 == 0;

% is the modulo operator which returns the remainder of an integer division. E.g. 13 % 5 = 3 and 13 / 5 = 2 because 2 * 5 + 3 = 13.

clicks % 5 will be zero for clicks = 0, 5, 10, 15, ...

like image 25
Olivier Jacot-Descombes Avatar answered Sep 29 '22 07:09

Olivier Jacot-Descombes