TIP1.C 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Includes */
  2. #include "opendoor.h"
  3. /* Function prototypes. */
  4. void DrawHorizontalBar(int nValue, int nMinValue, int nMaxValue,
  5. int nMaxChars);
  6. void DrawGraphOfPercentages(int nItems, int *panPercentages,
  7. char **papszTitles, char bTitleColor, char bGraphColor,
  8. int nTitleWidth, int nGraphWidth);
  9. /* Main function - program execution begins here. */
  10. main()
  11. {
  12. char *apszTitles[7] = {"Sunday", "Monday", "Tuesday", "Wednesday",
  13. "Thursday", "Friday", "Saturday"};
  14. int anValues[7] = {50, 75, 100, 25, 83, 0, 43};
  15. od_printf("`bright green`Test graph:\n\r");
  16. DrawGraphOfPercentages(7, anValues, apszTitles, 0x02, 0x0f, 20, 50);
  17. od_get_key(TRUE);
  18. return(0);
  19. }
  20. /* Function to draw horizontal graph of percentages with titles, to */
  21. /* demonstrate the use of the DrawHorizontalBar() function. */
  22. /* No titles should have more than nTitleWidth characters. */
  23. void DrawGraphOfPercentages(int nItems, int *panPercentages,
  24. char **papszTitles, char bTitleColor, char bGraphColor,
  25. int nTitleWidth, int nGraphWidth)
  26. {
  27. int nCurrentItem;
  28. /* Loop for each item (line) in the graph. */
  29. for(nCurrentItem = 0; nCurrentItem < nItems; ++nCurrentItem)
  30. {
  31. /* Set display color for title text. */
  32. od_set_attrib(bTitleColor);
  33. /* Add spaces to right-align all titles. */
  34. od_repeat(' ', nTitleWidth - strlen(papszTitles[nCurrentItem]));
  35. /* Display the title. */
  36. od_disp_str(papszTitles[nCurrentItem]);
  37. /* Add space between title and graph. */
  38. od_printf(" ");
  39. /* Set display color for graph. */
  40. od_set_attrib(bGraphColor);
  41. /* Draw bar graph for this line. */
  42. DrawHorizontalBar(panPercentages[nCurrentItem], 0, 100, nGraphWidth);
  43. /* Move to the next line. */
  44. od_printf("\n\r");
  45. }
  46. }
  47. /* Function to draw a horizontal bar, given a value, the minimum and maximum */
  48. /* possible values, and the number of characters the horizontal bar should */
  49. /* extended for the maximum value. */
  50. void DrawHorizontalBar(int nValue, int nMinValue, int nMaxValue,
  51. int nMaxChars)
  52. {
  53. /* Determine lenght of bar */
  54. int nBarChars = ((nValue - nMinValue) * nMaxChars) / nMaxValue;
  55. if(od_control.user_ansi || od_control.user_avatar)
  56. {
  57. /* If ANSI or AVATAR graphics are available, assume that IBM extended */
  58. /* ASCII is also available. This function uses character 220 to form */
  59. /* bars that are 1/2 the height of the line. You might also want to */
  60. /* try character 119, which will form bars that are the entire height */
  61. /* of the line. */
  62. od_repeat(220, nBarChars);
  63. }
  64. else
  65. {
  66. /* In ASCII mode, the bar is formed by the '=' character. */
  67. od_repeat('=', nBarChars);
  68. }
  69. }