home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / basic / pgbar.bas < prev    next >
Encoding:
BASIC Source File  |  1989-11-09  |  4.4 KB  |  115 lines

  1. ' PGBAR.BAS:  Create sample bar chart
  2.  
  3. DEFINT A-Z
  4. ' $INCLUDE: 'CHRTB.BI'
  5. CONST FALSE = 0, TRUE = NOT FALSE, MONTHS = 12
  6. CONST HIGHESTMODE = 13, TEXTONLY = 0
  7.  
  8. DIM Env AS ChartEnvironment                 ' See CHRTB.BI for declaration of                     ' the ChartEnvironment type
  9. DIM MonthCategories(1 TO MONTHS) AS STRING  ' Array for categories (used for
  10.                                                                                         ' Pie, Column and Bar Charts)
  11. DIM OJvalues(1 TO MONTHS) AS SINGLE         ' Array for 1st data series
  12.  
  13. DECLARE FUNCTION BestMode ()
  14.  
  15. ' Initialize the data arrays
  16. FOR index = 1 TO MONTHS: READ OJvalues(index): NEXT index
  17. FOR index = 1 TO MONTHS: READ MonthCategories$(index): NEXT index
  18.  
  19. ' Pass the value returned by the BestMode function to the Presentation
  20. ' Graphics routine ChartScreen to set the graphics mode for charting
  21.  
  22. ChartScreen (BestMode)      ' Even if SCREEN is already set to an acceptable
  23.                                                         ' mode, you still have to set it with ChartScreen
  24. IF ChartErr = cBadScreen THEN   ' Check to make sure ChartScreen succeeded
  25.     PRINT "Sorry --- There is a screen-mode problem in the Charting library"
  26.     END
  27. END IF
  28. ' Initialize a default pie chart
  29.                                                                         ' Pass Env (the environment variable),
  30. DefaultChart Env, cBar, cPlain      ' the constant cBar (for Bar Chart) and
  31.                                                                         ' cPlain
  32.  
  33. ' Add Titles and some chart options. These assignments modify some default
  34. ' values set in the variable Env (of type ChartEnvironment) by DefaultChart
  35.  
  36. Env.MainTitle.Title = "Good Neighbor Grocery" ' Specifies text of chart title
  37. Env.MainTitle.TitleColor = 15                 ' Specifies color of title text
  38. Env.MainTitle.Justify = cRight                ' How to align of title text
  39. Env.SubTitle.Title = "Orange Juice Sales"     ' Text of chart subtitle
  40. Env.SubTitle.TitleColor = 15                  ' Color of subtitle text
  41. Env.SubTitle.Justify = cRight                 ' How to align of subtitle text
  42. Env.ChartWindow.Border = cNo                  ' Specifies chart has no border
  43.  
  44. ' The next 2 assignments label the x-axis and y-axis
  45. Env.XAxis.AxisTitle.Title = "Quantity (cases)"
  46. Env.YAxis.AxisTitle.Title = "Months"
  47.  
  48. ' Call the bar-charting routine --- Arguments for call to Chart are:
  49. ' Env                 - Environment variable
  50. ' MonthCategories()   - Array containing Category labels
  51. ' OJvalues()          - Array containing Data values to chart
  52. ' MONTHS              - Tells number of data values to chart
  53.  
  54.     Chart Env, MonthCategories(), OJvalues(), MONTHS
  55.     SLEEP
  56.     '  If the rest of your program isn't graphic, reset original mode here
  57. END
  58.  
  59. ' Simulate data generation for chart values and category labels
  60. DATA 33,27,42,64,106,157,182,217,128,62,43,36
  61. DATA "Jan","Feb","Mar","Apr","May","Jun","Jly","Aug","Sep","Oct","Nov","Dec",
  62.  
  63. '============= Function to determine and set highest resolution ========
  64. ' The BestMode function uses a local error trap to check available modes,
  65. ' then assigns the integer representing the best mode for charting to its
  66. ' name so it is returned to the caller. The function terminate execution if
  67. ' the hardware doesn't support a mode appropriate for Presentation Graphics
  68. '========================================================================
  69. FUNCTION BestMode
  70.  
  71. ' Set a trap for an expected local error --- handled within the function
  72. ON LOCAL ERROR GOTO ScreenError
  73.  
  74. FOR TestValue = HIGHESTMODE TO 0 STEP -1
  75.     DisplayError = FALSE
  76.     SCREEN TestValue
  77.     IF DisplayError = FALSE THEN
  78.         SELECT CASE TestValue
  79.             CASE 12, 13
  80.                 BestMode = 12
  81.             CASE 9, 10, 11
  82.                 BestMode = 9
  83.             CASE 8, 4, 3
  84.                 BestMode = TestValue
  85.             CASE 2, 7
  86.                 BestMode = 2
  87.             CASE 1
  88.                 BestMode = 1
  89.             CASE ELSE
  90.                 PRINT "Sorry, you need graphics to display charts"
  91.                 END
  92.         END SELECT
  93.         EXIT FUNCTION
  94.     END IF
  95. NEXT TestValue
  96. ' Note there is no need to turn off the local error handler. It is turned off
  97. ' automatically when control passes out of the function
  98.  
  99. EXIT FUNCTION
  100. '==================== | Local error handler code |=======================
  101. ' The ScreenError label identifies a local error handler relied in the
  102. ' BestMode function. Invalid SCREEN values generate Error # 5 (Illegal
  103. ' function call) --- so if that is not the error reset ERROR to the ERR
  104. ' value that was generated so the error can be passed to other, possibly
  105. ' more appropriate errors.
  106. ScreenError:
  107.     IF ERR = 5 THEN
  108.         DisplayError = TRUE
  109.         RESUME NEXT
  110.     ELSE
  111.         ERROR ERR
  112.     END IF
  113. END FUNCTION
  114.  
  115.