Implement horizontal bar chart animation using C graphics
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main() {
char str[64];
/* request auto detection */
int gdriver = DETECT, gmode, err;
int i, midx, midy, tmp1, tmp2;
int v[4][2], w[4][2], x[4][2], y[4][2], z[4][2];
char depart[5][32] = {"R&D", "ERS", "N/W",
"HR","DEV"};
/* initialize graphic mode */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
err = graphresult();
if (err != grOk) {
/* error occurred */
printf("Graphics Error: %s",
grapherrormsg(err));
return 0;
}
/* mid position at x and y-axis */
midx = getmaxx() / 2;
midy = getmaxy() / 2;
/* y and x-axis */
line(70, 0, 70, getmaxy() - 50);
line(70, getmaxy() - 50, getmaxx(), getmaxy() - 50);
        /* departments at y axis */
        for (i = 0; i < 5; i++) {
                settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
                settextjustify(CENTER_TEXT, CENTER_TEXT);
                moveto(40, getmaxy() - 100 - i * 90);
                outtext(depart[i]);
        }
        /* y axis represents departments */
        moveto(5, midy);
        settextstyle(TRIPLEX_FONT, VERT_DIR, 1);
        settextjustify(CENTER_TEXT, CENTER_TEXT);
        sprintf(str, "%s", "DEPARTMENTS");
        outtext(str);
        /* x-axis represents revenue */
        moveto(midx, getmaxy() - 10);
        settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);
        sprintf(str, "%s", "REVENUE");
        outtext(str);
        tmp1 = getmaxy() - 40;
        tmp2 = 135;
        /* units at y-axis */
        for (i = 0; i < 5; i++) {
                moveto(tmp2, tmp1);
                sprintf(str, "%d", 20 + i * 20);
                outtext(str);
                tmp2 = tmp2 + 110;
        }
        /* initial positions for horizontal bars */
        v[0][0] = 71, v[0][1] = getmaxy() - 70;
        v[1][0] = 71, v[1][1] = getmaxy() - 130;
        w[0][0] = 71, w[0][1] = getmaxy() - 160;
        w[1][0] = 71, w[1][1] = getmaxy() - 220;
        x[0][0] = 71, x[0][1] = getmaxy() - 250;
        x[1][0] = 71, x[1][1] = getmaxy() - 310;
        y[0][0] = 71, y[0][1] = getmaxy() - 340;
        y[1][0] = 71, y[1][1] = getmaxy() - 400;
        z[0][0] = 71, z[0][1] = getmaxy() - 430;
        z[1][0] = 71, z[1][1] = getmaxy() - 490;
        /* animate horizontal bars in bar graph */
        for (i = 0; i < getmaxx() - 150; i++) {
                /* horizontal bar 1- R&D */
                if (i <= getmaxx() - 180) {
                        setcolor(BLUE);
                        line(v[0][0], v[0][1], v[1][0], v[1][1]);
                        v[0][0] = v[0][0] + 1;
                        v[1][0] = v[1][0] + 1;
                }
                /* horizontal bar 2- ERS */
                if (i <= getmaxx() - 250) {
                        setcolor(LIGHTRED);
                        line(w[0][0], w[0][1], w[1][0], w[1][1]);
                        w[0][0] = w[0][0] + 1;
                        w[1][0] = w[1][0] + 1;
                }
                /* horizontal bar 3 - N/W */
                if (i <= getmaxx() - 350) {
                        setcolor(LIGHTGREEN);
                        line(x[0][0], x[0][1], x[1][0], x[1][1]);
                        x[0][0] = x[0][0] + 1;
                        x[1][0] = x[1][0] + 1;
                }
                /* horizontal bar 4 - HR */
                if (i <= getmaxx() - 470) {
                        setcolor(DARKGRAY);
                        line(y[0][0], y[0][1], y[1][0], y[1][1]);
                        y[0][0] = y[0][0] + 1;
                        y[1][0] = y[1][0] + 1;
                }
                /* horizontal bar 5 - DEV */
                if (i <= getmaxx() - 550) {
                        setcolor(YELLOW);
                        line(z[0][0], z[0][1], z[1][0], z[1][1]);
                        z[0][0] = z[0][0] + 1;
                        z[1][0] = z[1][0] + 1;
                }
                /* sleep for 25 milliseconds */
                delay(25);
        }
        getch();
        /* deallocate memory allocated for graphic screen */
        closegraph();
        return 0;
  }
Output: (Animate horizontal bar chart using C graphics)
No comments:
Post a Comment