TIP1.TXT 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Many different types of programs can be enhanced by the use of graphical
  2. information. Often, this graphical information can take the form of
  3. horizontal bar graphs.
  4. An easy way to draw horizontal bars in door programs written with
  5. OpenDoors, is to use the od_repeat() function. Not only does od_repeat()
  6. allow you to easily form a bar by repeating a particular character the
  7. specified number of times, but it is also a very efficient way to do so.
  8. od_repeat() will take advantage of terminal emulation optimizations,
  9. when available. For instance, a character can be repeated any number of
  10. times with AVATAR by sending a short 3-byte sequence that specifies the
  11. character and number of times to repeat.
  12. How do you calculate the number of character to use to form a bar in
  13. your graph? The DrawHorizontalBar() function, which is provided below,
  14. will do this calculation for you. Simply provide the value to be
  15. represented by this bar, the minimum and maximum possible values, and
  16. the maximum number of character to use to draw the bar. For example, if
  17. you are graphing percentages (which could range from 0% to 100%), and
  18. wanted the graph to fit in a space of 40 columns, you would use:
  19. DrawHorizontalBar(nPercent, 0, 100, 40);
  20. The included tip1.c is a complete program which demonstrates the
  21. DrawHorizontalBar() function as called from another function that will
  22. create complete horizontal bar graphs. This second function,
  23. DrawGraphOfPercentages(), takes an array of titles, and array of values
  24. corresponding to each title, and draws a complete bar graph from this
  25. information.
  26. /* Function to draw a horizontal bar, given a value, the minimum and maximum */
  27. /* possible values, and the number of characters the horizontal bar should */
  28. /* extended for the maximum value. */
  29. void DrawHorizontalBar(int nValue, int nMinValue, int nMaxValue,
  30. int nMaxChars)
  31. {
  32. /* Determine lenght of bar */
  33. int nBarChars = ((nValue - nMinValue) * nMaxChars) / nMaxValue;
  34. if(od_control.user_ansi || od_control.user_avatar)
  35. {
  36. /* If ANSI or AVATAR graphics are available, assume that IBM extended */
  37. /* ASCII is also available. This function uses character 220 to form */
  38. /* bars that are 1/2 the height of the line. You might also want to */
  39. /* try character 119, which will form bars that are the entire height */
  40. /* of the line. */
  41. od_repeat(220, nBarChars);
  42. }
  43. else
  44. {
  45. /* In ASCII mode, the bar is formed by the '=' character. */
  46. od_repeat('=', nBarChars);
  47. }
  48. }