19
Introduction to Programming with Vi sual Basic 6.0 by McKeown and Pierc y 1 Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Metho d Line Method Line Charts Bar Charts Pie Charts Circle Meth od Scatter Dia grams Chapter 11: Using Visual Basic to Create Graphics

Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Embed Size (px)

Citation preview

Page 1: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

1Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Chapter 11:Using Visual Basic to Create

Graphics

Page 2: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

2Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Types of Graphics• Graphics are very useful for handling data or

information visually. • Engineers use graphics to design machines and

structures using CAD (computer-aided design) graphics.

• Artists use graphics to create images for both business and artistic purposes.

• We have already used clipart graphics created by others in some of our projects.

• A graphical representation of business data is referred to as analysis or business graphics.

Page 3: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

3Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Types of Business Graphics

• Business graphics include such types as line, bar, and pie charts.

• Line charts are often used to show trends over time. The incline or decline of each line segment demonstrates the change in values on the vertical axis for each unit change on the horizontal axis.

• Bar charts can be used to compare quantities using vertical or horizontal bars, where the length or height of the bar represents each quantity.

• Pie charts can be used to compare parts of some quantity (budget, income, and so on) using a circular diagram.

• Scatter diagrams can be used to look for patterns in data.

Page 4: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

4Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Creating Business Graphics with VB

•To plot mathematical equations or create business graphics, follow a six-step approach:–Decide on a type of control to use in creating the graphic.

–Set up the coordinate system for the control.–Add horizontal and vertical axes to the control.–Add division marks and labels to the axes.–Draw the graphic on the control.–Add titles and legends to the graphic.

Page 5: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

5Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Methods for Drawing Axes• The picture box is one of a variety of controls that

can be used for drawing graphics.• The coordinate axis is set using the Scale method

or the ScaleTop, ScaleLeft, ScaleHeight, and ScaleWidth properties.

• Straight lines can be drawn using the Line method, in which the beginning and ending values of the line are specified.

• The Line method is often used to draw axes for a graph.

• The AutoRedraw property must be used in the Form_Load event procedure to display the axes.

Page 6: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

6Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

The Scale Method

Scale(HUL, VUL) - (HLR, VLR)where

• HUL = horizontal measure of the upper left-hand corner

• VUL = vertical measure of the upper left-hand corner

• HLR = horizontal measure of the lower right-hand corner

• VLR = vertical measure of the lower right-hand corner

Page 7: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

7Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

The Line Method

Object.line (HUL, VUL) - (HLR, VLR)

where•HUL = horizontal measure of the upper left-hand corner•VUL = vertical measure of the upper left-hand corner•HLR = horizontal measure of the lower right-hand corner•VLR = vertical measure of the lower right-hand corner

Page 8: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

8Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Properties for Axes and Labels

• The CurrentX and CurrentY properties are used to determine the location of division marks and labels on the graph. The division marks are added to the axes with the Line control.

• The labels are added to the division marks with the Print method. The TextHeight and TextWidth properties are used to position the labels.

• The actual graph is plotted using the PSet method within a For-Next Loop. The increments in the loop must be kept small to avoid gaps in the graph.

Page 9: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

9Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Adding Division Marks and Labels

• Determine the width of the label with the TextWidth property.

• Divide the label width by two and subtract the result from the current location to determine the value of CurrentX where the label should be added.

• Set the CurrentY for the label to the value at the bottom of the division marks.

• Add the label to the graph or chart with the picture box Print method.

Page 10: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

10Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Charts for Vintage Videos

•Five different charts are going to be created for Vintage Videos:–a line chart for total rentals–line charts for the three types of rentals–a bar chart for total rentals–bar charts for the three types of rentals–a pie chart for distribution of total rentals

Page 11: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

11Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Steps for Creating Vintage Videos Charts

• The first step in creating a chart is to design the chart showing the axes and labels.

• The second step is to set the coordinate axes. This often involves using different scales for the vertical and horizontal axes and placing the origin off-center.

• The third step is to add the division marks and labels on the vertical and horizontal axes.

Page 12: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

12Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Creating Line Charts

• Line charts are drawn using the Line method to connect the points on the chart.

• Titles are added to a line chart in a manner similar to that used to add labels.

• We can draw multiple lines on a chart by using the DrawStyle property to distinguish between the various lines.

• For multiple-line charts, a legend is also needed to show the purpose of each line.

Page 13: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

13Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

DrawStyle Property Constants and Values

Line Style VB Constant Value

Solid vbSolid 0

Dash vbDash 1

Dot vbDot 2

Dash Dot vbDashDot 3

Dash Dot Dot vbDashDotDot 4

Transparent vbTransparent 5

Page 14: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

14Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Creating Bar Charts

• Bar charts are drawn using the B (box) option with the Line method.

• The parameters for the Line method give the coordinates of the corners of the box.

• Multiple bars can be distinguished using the FillStyle property.

• A legend is added for multiple bars in much the same way as for multiple lines.

Page 15: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

15Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

FillStyle Property Options

Fill Style VB Constant Value

Solid vbFSSolid 0

Transparent vbFSTransparent 1

Horizontal Lines vbHorizontalLine 2

Vertical Lines vbVerticalLine 3

Upward Diagonal vbUpwardDiagonal 4

Downward Diagonal vbDownwardDiagonal 5

Page 16: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

16Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Creating Pie Charts• Pie charts are drawn using the Circle method, where the

center of the circle and the radius determine the location and size of the circle.

• To draw the segments of the pie chart, we add parameters to the Circle method that will draw sectors rather than complete circles. These sectors are measured in radians.

• For the center of the circle to be connected to the sectors, the sector parameters must be negative.

• In a manner similar to that used for bar charts, the sectors can be filled using the FillStyle property and a legend can be added.

Page 17: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

17Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

The Circle Method

object.Circle (x, y), radius, color, start, end

where– x, y = the center of the circle– radius = the radius of the circle– color = the color for the circle– start = the starting point in radians for any

sector of the circle– end = the ending point in radians for any sector

of the circle

Page 18: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

18Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Three Steps to Create Pie Chart for Vintage Videos

• Compute cumulative fractions for each of three types of videos.

• Set the coordinate system for the form.• Draw pie chart using Circle method.

Page 19: Copyright © 2001 by Wiley. All rights reserved. Chapter 11: Using Visual Basic to Create Graphics Scale Method Line Method Line Charts Bar Charts Pie Charts

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

19Copyright © 2001 by Wiley. All rights reserved.

Chapter 11: Using Visual Basic to Create Graphics

Scale Method

Line Method

Line Charts

Bar Charts

Pie Charts

Circle Method

Scatter Diagrams

Creating Scatter Diagrams

• Scatter diagrams help the user look for patterns in data.

• A scatter diagram can be drawn in Visual Basic by using the FillStyle property and the Circle method to draw circles.

• The circles are plotted for pairs of values, with the radius of each circle being set to a small fraction of the horizontal axis scale.