C# Application UniCAD C# Practical 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 3 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 9 8 7 6 5 4 3 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: [email protected] 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 Real- World .NET Applications / Jack Xu. – 1st ed. p.cm. ISBN 978-0-9793725-0-6 1. C# programming. 2. Charts and Graphics. 3. .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 1 C# Graphics Basics...........................................................1 Coordinate Systems.....................................................................................1 Default Coordinates...........................................................................1 Custom Coordinates...........................................................................6 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 2 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 3 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 4 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 5 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 Contents vii 6 3D Charts.......................................................................279 3D Chart Basics.......................................................................................280 Point3 and Matrix3 Classes............................................................280 Chart Style in 3D............................................................................282 Coordinate Axes.............................................................................286 Gridlines.........................................................................................291 Labels.............................................................................................294 Testing Project...............................................................................300 3D Line Charts.........................................................................................303 Implementation..............................................................................303 Testing Project...............................................................................305 3D Chart Package....................................................................................308 Chart Style......................................................................................308 Point4 Class....................................................................................313 Data Series.....................................................................................314 Chart Functions..............................................................................317 DrawChart Class............................................................................321 Surface Charts..........................................................................................327 Mesh Charts...................................................................................329 Curtain Charts................................................................................334 Water Fall Charts...........................................................................337 Surface Charts................................................................................339 Color Charts on X-Y Plane............................................................345 Contour Charts.........................................................................................348 Algorithm.......................................................................................348 Implementation..............................................................................349 Testing Contour Charts..................................................................353 Filled Contour Charts.....................................................................356 Combination Charts.................................................................................356 X-Y Charts in 3D...........................................................................357 Contour Charts in 3D.....................................................................358 Mesh-Contour Charts.....................................................................362 Surface-Contour Charts..................................................................363 Surface-Filled-Contour Charts.......................................................363 3D Bar Charts..........................................................................................364 Implementation..............................................................................365 Testing 3D Bar Charts....................................................................371 Slice Charts..............................................................................................373 Implementation..............................................................................373