giáo trình lập trình c - đồ họa trong full tiếng anh

571 1.2K 0
giáo trình lập trình c - đồ họa trong full tiếng anh

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

UniCAD Practical C# Application C# Charts and Graphics 1000000101010010110010010001010100101101101100101000101010 01011011001000101010011101100100010101000110010100010101 1000000101010010110010010001010100101101101100101000101010 01011011001000101010011101100100010101000110010100010101 Advanced Chart and Graphics Programming for Real-World NET Applications Jack Xu, Practical C# Charts and Graphics Advanced Chart and Graphics Programming for Real-World NET Applications Practical C# Charts and Graphics Advanced Chart and Graphics Programming for Real-World NET Applications Jack Xu, Ph.D UniCAD Publishing Practical C# Charts and Graphics Copyright © 2007 by Jack Xu, Ph.D Printed and bound in the United States of America Editor: Anna Y Hsu All rights reserved No part of the contents of this book and corresponding example source code may be reproduced or transmitted in any form or by any means without the written permission of the publisher The author and publisher have made every effort in the preparation of this book to ensure the accuracy of the information, however this book is sold without warranty, either express or implied No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained in the book The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and /or custom covers and content particular to your business, training goals, marketing focus, and branding interests For more information, please contact: sales.@unicadinc.com Visit us on the Website: http://publishing.unicadinc.com Published by UniCAD Publishing Phoenix, USA ISBN 978-0-9793725-0-6 Publisher’s Cataloging-in-Publication Data Xu, Jack Practical C# Charts and Graphics – Advanced Chart and Graphics Programming for RealWorld NET Applications / Jack Xu – 1st ed p.cm ISBN 978-0-9793725-0-6 C# programming Charts and Graphics .NET Application I Title II Title III Title: Practical C# Charts and Graphics Contents Introduction xi Overview xi What this Book includes xiii Is This Book for You? xiii What Do You Need to Use This Book? xiv How This Book Is Organized xiv What Is Left Out .xvi Conventions xvi Using Code Examples .xvi Customer Support .xvi C# Graphics Basics Coordinate Systems .1 Default Coordinates Custom Coordinates Window and Viewport .9 Pen and Brush 15 Pen Class 16 Brush Class .16 Basic Graphics Shapes .17 Points 17 Lines and Curves 18 Rectangles, Ellipses, and Arcs 20 Polygons 22 Color 26 System Colors 27 Custom Color Map 30 iii iv Contents Color Shading 37 2D Matrices and Transformations 45 Basics of Matrices and Transformations 45 Scaling 46 Reflection 47 Rotation 47 Translation 48 Homogeneous Coordinates 48 Translation in Homogeneous Coordinates .49 Scaling in Homogeneous Coordinates .49 Rotation in Homogeneous Coordinates 50 Combining Transformations 51 Matrix Class and Transformation in C# 52 Matrix Definition in C# 52 Matrix Operation in C# 54 Basic Matrix Transformations in C# 56 Object Transformations in C# 62 Basic Transformations .62 Combining Transformation in C# 69 Transformation of Multiple Objects in C# .73 Text Transformation .77 Transformations in Graphics Class 79 2D Line Charts 83 Your First Simple Line Chart 83 Basic Elements of 2D Line Charts 84 Creating a Simple 2D Line Chart Using C# 85 How It Works 86 Changing Chart Position 87 Creating Line Charts Using ArrayList .88 Chart Style 88 Data Collection .91 Data Series .93 Line Style 94 Form1 class 95 Testing Project 97 Gridlines and Labels 98 Modifying Form1 Class 98 Contents v Modifying Chart Style 100 Gridlines and Ticks 107 Labels and Title 108 Testing Project .108 Legends 109 Legend Class 109 Legend Layout .114 Modifying Form1 Class 115 Testing Project .115 Symbols 116 Defining Symbols 116 Symbol Style Class 117 Modifying Form1 Class 121 Modifying Data Collection 122 Modifying Line Style .124 Testing Project .125 Line Charts with Two Y Axes 125 Why Two Y Axes 125 Modifying Chart Style 127 Modifying Form1 Class 129 Modifying Data Series 132 Modifying Data Collection 132 Testing Project .134 Sub-Charts .135 Layout of Sub-charts 135 Sub-Chart Class 135 Modifying Chart Style 138 Modifying Form1 Class 140 Testing Project .145 Specialized 2D Charts 147 Creating Bar Charts 147 Implementation 147 Bar Charts .159 Group Bar Charts 163 Overlay Bar Charts .165 Stacked Bar Charts .166 Bar Charts with Color Map 167 Creating Stair Step Charts 173 vi Contents Implementation 173 Testing Project .175 Creating Stem Charts .178 Implementation 178 Testing Project .179 Creating Charts with Error Bars 180 Implementation 180 Testing Project .183 Creating Pie Charts 184 Implementation 185 Testing Project .191 Creating Area Charts .194 Implementation 194 Testing Project .196 Creating Polar Charts .198 Implementation 199 Testing Project .205 Creating Stock Charts 208 Implementation 208 Hi-Lo Chart 214 Hi-Lo-Open-Close Chart 216 Candlestick Chart 216 3D Matrices and Transformations 219 Basics of Matrices and Transformations in 3D .220 3D Point and Matrix Operation in C# 220 Scaling 223 Reflection .225 Translation 226 Rotation 227 Projections .229 Parallel Projections .230 Perspective Projections 252 Special Coordinate Systems in 3D 260 Cylindrical Coordinates 260 Spherical Coordinates 264 Euler Angles 268 Azimuth and Elevation View 270 Excel Charts in C# Applications 537 + 1) * (xarray[i] + 1) yarray[j] * yarray[j]); } } return dataArray; } private void btnClose_Click(object sender, EventArgs e) { xla.DisplayAlerts = false; if (xla != null) { xla.Quit(); xla = null; } this.Close(); } } } In this class, we first create data using a 1D and 2D array and assign them to the Excel worksheet This data must be structured in a certain format The categories (used as the X axis) are in the first column The second row represents the series names (used as the Y axis) Finally, the data values are formed by a 2D data array As long as the data is assigned to the Excel worksheet, you can create a surface chart by selecting the surface chart type: xlChart.ChartType = XlChartType.xlSurface; The rest of the code in the Form1 class is used to customize the axes, add axis labels and a title, and remove the legend from the chart This project produces the output of Figure 9-10 It can be seen that the chart is created by ranges of data values, which is evident by the fact that the colors vary with the values along the vertical (the Z) axis The number of colors on the surface chart is based on the major unit of the Z axis The Excel surface chart has four subtypes: • xlSurface: displays a standard surface chart • xlSurfaceWireFrame: displays a surface chart without colors • xlSurfaceTopView: displaysthe a surface chart viewed from above • xlSurfaceTopViewWireFrame: displays a surface chart without colors, viewed from above You can select a different subtype of surface chart from the above four options according to your application requirements I should point out here that Excel surface charts have some limits: they are not true X-Y-Z charts X and Y are not treated as numeric data, and must consist of regularly defined, evenly spaced categories Exactly one Z value is needed for each X-Y pair Excel surface charts are drawn by connecting Z values at X-Y nodes with straight lines and planar sections A saddle point is not 538 Practical C# Charts and Graphics accurately drawn if it occurs between the X-Y nodes Color-filled surface charts in Excel not allow transparency, meaning that you can not visualize the hidden parts of the surface Figure 9-10 Excel surface chart created in a C# application Color Map When creating Excel surface charts in the previous subsection, we used the default color map In some situations, you may want to use a customized color map for your Excel charts, which can be achieved by using the Excel color indices Excel displays 56 color indices at any given time You can use the following C# code snippet to extract Excel 56 default color indices: Excel Charts in C# Applications Figure 9-11 Default Excel colors with color indices for (int i = 0; i < 14; i++) { string cellString = "A" + (i + 1).ToString(); ws.get_Range(cellString, cellString).Interior.ColorIndex = i + 1; ws.get_Range(cellString, cellString).Value2 = i + 1; cellString = "B" + (i + 1).ToString(); ws.get_Range(cellString, cellString).Interior.ColorIndex = 14 + i + 1; ws.get_Range(cellString, cellString).Value2 = 14 + i + 1; cellString = "C" + (i + 1).ToString(); ws.get_Range(cellString, cellString).Interior.ColorIndex = * 14 + i + 1; ws.get_Range(cellString, cellString).Value2 = * 14 + i + 1; cellString = "D" + (i + 1).ToString(); ws.get_Range(cellString, cellString).Interior.ColorIndex = * 14 + i + 1; ws.get_Range(cellString, cellString).Value2 = * 14 + i + 1; } 539 540 Practical C# Charts and Graphics This generates Excel’s 56 default colors with corresponding color indices, as shown in Figure 9-11 After understanding the definition of the Excel color indices, we can easily create a customized color map Let’s use an example to demonstrate how to create such a customized Excel color map and apply it to a surface chart This example is based on the previous project, Example9_7 Start off with new C# Windows application called Example9_8 Add the Excel Object Library to the reference Add the Form1 class of Example9_7 to the current application, and change the corresponding namespace to Example9_8 Add a new class named ColorMap to the project In the ColorMap class, define a color map using Excel color indices with the following code: using System; namespace Example9_8 { public class ColorMap { public static int[] colorMap() { int[] colorArray = new int[16]; colorArray[0] = 5; colorArray[1] = 8; colorArray[2] = 4; colorArray[3] = 6; colorArray[4] = 44; colorArray[5] = 46; colorArray[6] = 3; colorArray[7] = 29; colorArray[8] = 4; colorArray[9] = 6; colorArray[10] = 36; colorArray[11] = 40; colorArray[12] = 44; colorArray[13] = 45; colorArray[14] = 46; colorArray[15] = 53; return colorArray; } } } In this class, we define a static integer array that holds the Excel color indices You can define your own color map array in the same manner The problem now is how to assign this color map to the Excel charts The trick is to use the Excel’s LegendEntry objects In the Plot button click event handler, add the axis scale parameters to the Z axis: Axis zAxis = (Axis)xlChart.Axes(XlAxisType.xlValue, XlAxisGroup.xlPrimary); zAxis.HasTitle = true; zAxis.AxisTitle.Text = "Z Axis"; zAxis.MinimumScale = -8; Excel Charts in C# Applications 541 Figure 9-12 Surface chart colored with a customized color map zAxis.MaximumScale = 8; zAxis.MajorUnit = 1; Then, add the following code snippet to assign the color map to the LegendEntry: xlChart.HasLegend = true; int[] cm = ColorMap.colorMap(); int numberOfLegendEntries = Convert.ToInt16((zAxis.MaximumScale - zAxis.MinimumScale) / zAxis.MajorUnit); for (int i = 1; i

Ngày đăng: 26/11/2014, 20:42

Mục lục

  • What This Book Includes

  • Is This Book for You?

  • What Do You Need to Use This Book?

  • How This Book Is Organized

  • What Is Left Out

  • Chapter 1 C# Graphics Basics

    • Coordinate Systems

      • Default Coordinates

      • Pen and Brush

        • Pen Class

        • Rectangles, Ellipses, and Arcs

        • Chapter 2 2D Matrices and Transformations

          • Basics of Matrices and Transformations

            • Scaling

            • Homogeneous Coordinates

              • Translation in Homogeneous Coordinates

              • Scaling in Homogeneous Coordinates

              • Rotation in Homogeneous Coordinates

              • Matrix Class and Transformation in C#

                • Matrix Definition in C#

                • Matrix Operation in C#

                • Basic Matrix Transformations in C#

                • Object Transformations in C#

                  • Basic Transformations

                  • Combining Transformation in C#

                  • Transformation of Multiple Objects in C#

                  • Transformations in Graphics Class

                  • Chapter 3 2D Line Charts

                    • Your First Simple Line Chart

                      • Basic Elements of 2D Line Charts

Tài liệu cùng người dùng

Tài liệu liên quan