Practical C# Charts and Graphics (Second Edition)
About this Book
"Practical C# Charts and Graphics (Second Edition)" ports and rebuilds rebuild all the examples in the book using the C# Windows Forms App (.NET Framework) template based on the latest Visual Studio 2019 and .NET Framework 4.7. It presents a complete and comprehensive introduction to C# chart and graphics capabilities, and explains in great detail and depth how to create a variety of C# graphics and charts. It provides all the tools you need to add advanced graphics and charts to your C# applications.
You can read the Introduction for an overview of the book, and the Table of Contents for more detailed information about the scope of the book. From this information, you can get a feeling of the way the book provides original and detailed materials to help you create sophisticated charts and graphics in your .NET applications using C#, GDI+, and Windows Forms.
Examples in the book teach you how to create various popular C# graphics and charts, including a US flag, 2D XY chart, stock chart, contour chart, 3D surface chart, and 4D Slice chart. The book provides more than 50 ready-to-run sample programs that allow you to explore the C# chart and graphics techniques described in the book. You can modify these sample codes or add new features to them to form the basis of your own chart and graphics packages. Some of the example programs are already sophisticated chart and graphics packages that can be used directly in your own real-world C# applications.
The second edition of the book also represents a detailed discussion on how to integrate JavaScript chart and graphics library into C# Windows Forms applications. This chapter is designed specifically for readers who prefer not to create C# chart programs from scratch, and would like to take advantage of client-side chart and graphics applications.
The book also contains a chapter that demonstrates how to embed Gincker Graphics into your C# desktop applications. It demonstrates that with Gincker Graphics, you can create a variety of charts and graphics, ranging from simple to sophisticated, just by typing in a mathematic formula, pasting a dataset, or uploading a data file; you do not need to write a single line of code or rely on any special software package.
Table of Contents
Introduction 1 Why Second Edition? 1 What’s New in Second Edition 1 Example Structure 1 Auto-Implemented Properties 2 System.Numerics 3 Embed JavaScript Chart Library 4 Embed Gincker Graphics 4 Overview 5 What this Book Includes 6 Is this Book for You? 7 What Do You Need to Use this Book? 8 How this Book Is Organized 8 What is Left Out 9 Use Code Examples 9 Customer Support 9 1 C# Graphics Basics 11 1.1 Coordinate Systems 11 1.1.1 Default Coordinates 11 1.1.2 Custom Coordinates 18 1.1.3 Window and Viewport 22 1.1.3.1 Zooming 22 1.1.3.2 Panning 24 1.2 Pen and Brush 28 1.2.1 Pen Class 28 1.2.2 Brush Class 29 1.3 Basic Graphics Shapes 29 1.3.1 Points 30 1.3.2 Lines and Curves 30 1.3.3 Rectangles, Ellipses, and Arcs 32 1.3.4 Polygons 34 1.4 Color 38 1.4.1 System Colors 39 1.4.2 Custom Color Map 42 1.4.3 Color Map with LinearGradientBrush 50 1.4.4 Color Shading 55 2 2D Matrices and Transformations 63 2.1 Basics of Matrices and Transformations 63 2.1.1 Scaling 63 2.1.2 Reflection 64 2.1.3 Rotation 65 2.1.4 Translation 66 2.2 Homogeneous Coordinates 66 2.2.1 Translation in Homogeneous Coordinates 66 2.2.2 Scaling in Homogeneous Coordinates 67 2.2.3 Rotation in in Homogeneous Coordinates 68 2.2.4 Combining Transformations 69 2.3 Matrix Class and Transformation 69 2.3.1 Matrix Definition in C# 70 2.3.2 Matrix Operations in C# 72 2.3.3 Matrix Transformations in C# 75 2.4 Object Transformations in C# 80 2.4.1 Basic Transformations# 81 2.4.2 Combining Transformations in C## 89 2.4.3 Transformation on Multiple Objects in C# 94 2.4.4 Text Transformation 99 2.4.5 Transformations in Graphics Class 102 3 2D Line Charts 107 3.1 Your First Simple Line Chart 107 3.1.1 Basic Elements of 2D Line Charts 107 3.1.2 A Simple 2D Line Chart 108 3.1.3 How It Works 111 3.1.4 Change Chart Position 111 3.2 Create Line Charts Using List 112 3.2.1 Chart Style 112 3.2.2 LineStyle Class 114 3.2.3 Data Series 114 3.2.4 Data Collection 115 3.2.5 Test Example 117 3.3 Gridlines and Labels 119 3.3.1 Modify ChartStyle Class 119 3.3.1.1 Gridlines and Ticks 125 3.3.1.2 Labels and Title 125 3.3.2 Modify Form Class 126 3.3.3 Test Example 127 3.4 Legends 128 3.4.1 Legend Class 128 3.4.2 Legend Layout 132 3.4.3 Test Legend 132 3.5 Symbols 135 3.5.1 Define Symbols 135 3.5.2 Symbol Style Class 136 3.5.3 Modify LineStyle Class 139 3.5.4 Modify DataSeries Class 140 3.5.5 Modify DataCollection Class 141 3.5.6 Modify Legend Class 142 3.5.7 Test Symbols 143 3.6 Line Charts with Two Y-Axes 146 3.6.1 Why Two Y-Axes 146 3.6.2 Modify ChartStyle Class 147 3.6.3 Modify DataSeries Class 153 3.6.4 Modify DataCollection Class 154 3.6.5 Test Y2-Axis 156 3.7 Sub-Charts 159 3.7.1 Layout of Sub-Charts 159 3.7.2 SubChart Class 160 3.7.3 Modify ChartStyle Class 161 3.7.4 Test Sub-Charts 162 4 Specialized 2D Charts 169 4.1 Bar Charts 169 4.1.1 Implementation 169 4.1.1.1 ChartStyle Class 170 4.1.1.2 BarStyle Class 176 4.1.1.3 DataSeries Class 176 4.1.1.4 DataCollection Class 177 4.1.2 Test Bar Charts 181 4.1.3 Group Bar Charts 186 4.1.4 Overlay Bar Charts 188 4.1.5 Stacked Bar Charts 190 4.1.6 Bar Charts with Color Map 191 4.2 Stair-Step Charts 195 4.2.1 Implementation 196 4.2.2 Test Stair-Step Charts 198 4.3 Stem Charts 200 4.3.1 Implementation 201 4.3.2 Test Stem Charts 202 4.4 Charts with Error Bars 204 4.4.1 Implementation 204 4.4.2 Test Error-Bar Charts 206 4.5 Pie Charts 207 4.5.1 Implementation 208 4.5.1.1 ChartStyle Class 208 4.5.1.2 DataSeries Class 209 4.5.1.3 Legend Class 211 4.5.2 Test Pie Charts 213 4.6 Area Charts 216 4.6.1 Implementation 216 4.6.2 Test Area Charts 218 4.7 Polar Charts 220 4.7.1 Implementation 220 4.7.1.1 ChartStyle Class 220 4.7.1.2 DataCollection Class 223 4.7.1.3 Legend Class 224 4.7.2 Test Polar Charts 226 4.8 Stock Charts 230 4.8.1 Stock Data from Yahoo Finance API 230 4.8.2 Implementation 232 4.8.3 Hi-Lo Chart 234 4.8.4 Hi-Lo-Open-Close Chart 237 4.8.5 Candlestick Chart 237 5 3D Matrices and Transformations 239 5.1 Basics of 3D Matrices and Transformations 239 5.1.1 3D Vector and Matrix Operations 239 5.1.1.1 Vector4 240 5.1.1.2 Matrix4x4 240 5.1.2 Scaling 241 5.1.3 Reflection 245 5.1.4 Translation 246 5.1.5 Rotation 247 5.2 Parallel Projections 248 5.2.1 Orthographic Projections 250 5.2.1.1 Multi-view Projections 250 5.2.1.2 Axonometric Projections 251 5.2.1.3 Orthographic Projections in C# 255 5.2.2 Oblique Projections 265 5.2.2.1 Cavalier Projections 266 5.2.2.2 Cabinet Projections 267 5.2.2.3 Oblique Projections in C# 267 5.3 Perspective Projections 272 5.3.1 One-Point Projections 273 5.3.2 Two-Point Projections 274 5.3.3 Three-Point Projections 274 5.3.4 Perspective Projection Matrix 275 5.3.5 Perspective Projection in C# 276 5.4 Special Coordinate Systems in 3D 281 5.4.1 Cylindrical Coordinate System 281 5.4.2 Spherical Coordinates 285 5.4.3 Euler Angles 288 5.4.4 Azimuth-Elevation View 291 6 3D Charts 299 6.1 3D chart Basics 299 6.1.1 3D Chart Style 299 6.1.2 Point3D Method 301 6.1.3 Coordinate Axes 303 6.1.4 Gridlines 308 6.1.5 Labels 311 6.1.6 Test 3D Coordinate System 316 6.2 3D Line Charts 318 6.2.1 Implementation 319 6.2.2 Test 3D Line Chart 321 6.3 3D Chart Package 324 6.3.1 ChartStyle2D Class 324 6.3.2 DataSeries Class 328 6.3.3 ChartFunctions class 330 6.3.4 DrawChart Class 333 6.4 Surface Charts 338 6.4.1 Mesh Charts 338 6.4.2 Curtain Charts 347 6.4.3 Water Fall Charts 349 6.4.4 Surface Charts 353 6.4.5 Heat-Map Charts 360 6.5 Contour Charts 365 6.5.1 Algorithm 365 6.5.2 Implementation 366 6.5.3 Test Contour Charts 369 6.5.4 Filled Contour Charts 371 6.6 Combination Charts 372 6.6.1 Heat-Map Charts in 3D 372 6.6.2 Contour Charts in 3D 373 6.6.3 Mesh-Contour Charts 377 6.6.4 Surface-Contour Charts 378 6.6.5 Surface-Filled Contour Charts 379 6.7 3D Bar Charts 380 6.7.1 Implementation 380 6.7.2 Test 3D Bar Charts 386 6.8 Slice Charts 389 6.8.1 Implementation 390 6.8.2 Test Slice Charts 392 7 Charts and User Controls 395 7.1 User Control Basics 395 7.1.1 Design Time Support 396 7.1.2 Event Handling 399 7.2 User Control for 2D Charts 399 7.2.1 Create User Control 399 7.2.2 Use TypeConverter 400 7.2.3 ChartStyle Class 404 7.2.4 Legend Class 420 7.2.5 Chart2D Class 425 7.2.6 Test User Control 429 7.3 User Control for 3D Charts 438 7.3.1 ChartStyle Class 439 7.3.2 Chart3D Class 461 7.4 Test Chart3D User Control 464 7.4.1 3D Line Charts 464 7.4.2 Surface Charts 465 7.4.3 Contour Charts 468 7.4.4 Combination Charts 469 7.4.5 3D Bar Charts 471 7.4.6 Slice Charts 472 8 Embed JavaScript Charts 474 8.1 Line Charts 474 8.1.1 Simple Line Charts 475 8.1.2 Multiple-Line Charts 478 8.1.3 Line Charts with Two Y-Axes 480 8.2 Specialized 2D Charts 484 8.2.1 Area Charts 484 8.2.2 Bar Charts 488 8.2.3 Pie Charts 493 8.2.4 Polar Charts 497 9 Embed Gincker Graphics 503 9.1 WebView Control 503 9.1.1 Install WebView 503 9.1.2 Use WebView for WinForms 504 9.2 Introduction to Gincker Graphics 505 9.2.1 Main Features 505 9.2.2 Output from Gincker Graphics 506 9.2.3 Templates for Charts and Graphics 506 9.2.4 Embed Gincker Graphics within WinForms 507 9.2.5 Create User Account 509 9.3 Get Started 509 9.3.1 Select Template 509 9.3.2 Layout Window 509 9.3.3 Input Panel 510 9.3.4 Chart Area 512 9.3.5 Customization Panel 512 9.4 2D Charts 515 9.4.1 2D Multiple-Line Charts 515 9.4.2 2D Parametric Charts 517 9.4.3 Polar Charts 519 9.4.4 Contour Charts 521 9.5 3D Charts 524 9.5.1 3D Line Charts 524 9.5.2 3D Parametric Surfaces 525 9.5.3 3D Parametric Shapes 528 9.5.4 3D Extruded Surfaces 530 9.5.5 3D Shapes of Revolution 531 9.5.6 3D Tubes 533 9.6 Complex-Variable Charts 534 9.6.1 3D Complex Charts 534 9.6.2 Complex Heatmap Charts 537 9.6.3 Domain-Coloring Charts 540 9.6.4 Iterated-Function Charts 543 Index 547
Introduction
        Why Second Edition?
        What's nNew in Second Edition
        Overview
        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
        Using Code Examples
Why Second Edition?
I have received plenty of feedback from readers since the first edition of my book “Practical C# Charts and Graphics” was published a decade ago. Many readers asked for an updated edition. I realize that .NET technology has advanced and changed a lot in the past decade and there is a need to incorporate these new developments in .NET Framework into the book and examples contained therein.
In the first edition, the example projects were built and tested on Visual Studio .NET 2005 under Windows 2000 or XP operating system. In this new edition, I decide to port and rebuild all the examples in the book using the C# Windows Forms App (.NET Framework) template based on the latest Visual Studio 2019 and .NET Framework 4.7.
What's New in Second Edition
In addition to port the example code to the latest .NET technology, the second edition also makes some structural changes and replacement of some chapters with the updated content.
Example Structure
In the first edition, each example was built as a separated project, which results in over 60 independent projects. As several readers pointed out, there are some issues with this approach:
- Inconvenient when switch from one example to another – you have to close the current example project and open another one in Visual Studio.
- Duplicated code – Since each example is an independent project, it is impossible to share the common code among different example projects. You have to copy and paste a lot of duplicated code from one example to another.
To avoid these problems, in the second edition, I decide to restructure the example project – build all the examples within a single project in Visual Studio. This way, different examples can share common code or use class inheritance to avoid code duplication. I also add the navigation button links on the MainForm window, which allows you to easily switch from one example to another by simply clicking the corresponding navigation link.
Auto-Implemented Properties
In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic required in the property accessors. For example, in the first edition, the LineStyle class in Exmaple3_2 has the following code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace Example3_2
{
public class LineStyle
{
private DashStyle linePattern = DashStyle.Solid;
private Color lineColor = Color.Black;
private float LineThickness = 1.0f;
private bool isVisible = true;
public bool IsVisible
{
get { return isVisible; }
set { isVisible = value; }
}
virtual public DashStyle Pattern
{
get { return linePattern; }
set { linePattern = value; }
}
public float Thickness
{
get { return LineThickness; }
set { LineThickness = value; }
}
virtual public Color LineColor
{
get { return lineColor; }
set { lineColor = value; }
}
}
}
While in the second edition, with the auto-implemented properties, the above code reduces to the following:
using System.Drawing;
using System.Drawing.Drawing2D;
namespace CsharpChart.Chapter03.Example3_2
{
public class LineStyle
{
public bool IsVisible { get; set; } = true;
public float LineThickness { get; set; } = 1.0f;
virtual public DashStyle LinePattern { get; set; } = DashStyle.Solid;
virtual public Color LineColor { get; set; } = Color.Black;
}
}
When you declare a property as shown in the above example, the compiler creates a private, anonymous backing field internally that can only be accessed through the property’s get and set accessors.
Note that we also add initializers to auto properties, which is a new feature introduced in C# 6. Prior to C# 6, you need to set the property value in a constructor. The auto-property initializer allows you to set the value of a property at the same time you declare it in a class.
The example code in the second edition uses auto-implemented properties with initializers, which greatly simplifies implementation and makes coding faster.
System.Numerics
.NET Framework 4.6 and later provides a SIMD (Single Instruction Multiple Data) enabled types through the System.Numerice namespace. SIMD operations can be parallelized at the hardware level. That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics applications.
The .NET SIMD-enabled types include the following:
- The Vector2, Vector3, and Vector4 types, which represent vectors with 2, 3, and 4 single values.
- Two matrix types. Matrix3x2, which represents a 3x2 matrix, and Matrix4x4, which represents a 4x4 matrix.
- The Plane type, which represents a plane in 3D space.
- The Quaternion type, which represents a vector that is used to encode 3D physical rotations.
In this edition, we will use the vectors and matrices defined in the System.Numerics namespace, while in the first edition, we used the matrix in the System.Drawing.Drawing2D namespace and Point3 created by our own for 3D matrix operations.
Embed JavaScript Chart Library
In the second edition, we have replaced Chapter 8 “DataGridView and Chart User Controls” in the first edition by the new Chapter 8 “Embed JavaScript Charts”. The reason for doing this is that the DataGridView control provides a way to display data in a tabular format, but it does not add any unique feature to charts and graphics. Another reason is the rapid advancement in client-side chart and graphics applications. Instead of creating your own charting package from scratch, I want to show you another way to create charts and graphics – embed a client-side JavaScript charting library called ECharts into your Windows-Forms desktop applications via WebBrowser control. There are many client-side JavaScript charting libraries, including HighCharts, Chartist, Chart.js, Google Charts API, etc. Any of these packages will work as long as it provides features you need in your applications.
Embed Gincker Graphics
The second edition also uses the new Chapter 9 “Embed Gincker Graphics” to replace the old Chapter 9 “Excel Charts in C# Applications” in the first edition. Gincker Graphics is a much more powerful chart and graphics platform than Excel.
With Gincker Graphics, you can create a variety of charts and graphics, ranging from simple to sophisticated, just by typing in a mathematic formula, pasting a dataset, or uploading a data file; you do not need to write a single line of code or rely on any special software package. It converts different chart and graphics applications into templates and implements a common interface and standardized input/output format by encapsulating all the programming details internally. Gincker Graphics allows you to save your work into gincker – a unique URL link, which can be used to regenerate, manipulate, modify, and customize charts and graphics.
Furthermore, you can embed the Gincker Graphics into your WinForms applications using the WebView control. This way, you can create advanced charts and graphics within your desktop application without the need to write any code on chart and graphics programming.
The following is the updated content, which is basically similar to that in the first edition.
Overview
Welcome to Practical C# Charts and Graphics. This book is intended for C# .NET developers who want to add professional graphics and charts to their applications. My hope is to write the ultimate C# chart and graphics programming guide, useful to C# application programmers of all skill levels.
We have all heard the saying "a picture is worth a thousand words". Creating charts and graphics plays a very important role in every Windows application. Charts and graphics can make data easier to understand, can add interest to a report, and can have wide applications in our daily life. For instance, in the scientific, engineering, and mathematics community, there is always the need to present data and results graphically. Microsoft Visual C# programming language is one of the best of the few available development tools that provide the computational capability to both generate data as a simulation engine and display it in a variety of graphical representations based on its Graphical Device Interface (GDI+).
The power of the C# programming language, combined with the simplicity of implementing Windows Form applications in Visual Studio .NET, makes real-world Windows program development faster and easier than ever before. Visual C# is a versatile and flexible tool that allows users with even the most elementary programming abilities to produce sophisticated charts, graphics, and graphical user interfaces (GUIs). The level of complexity and sophistication of the graphics and charting applications you create is limited only by your needs, curiosity, and imagination.
As you may have already noticed, most bookstores offer hundreds of C# programming books. The vast majority of these books are general-purpose user guides and tutorials that explain the basics of the C# tool and how to use it to implement simple C# applications. Some of these books contain a chapter or two covering graphics and charts. None, however, provide the level of detail that you will find in this book.
This book is written with the intent of providing you with a complete and comprehensive explanation of the C# graphics and chart capability, and pays special attention to creating charts that can be used directly in your own real world C# Applications. Much of this book contains original work based on my own programming experience in developing commercial Computer Aided Design (CAD) packages. Without C# and the .NET framework, development of advanced graphics and charts can be a difficult and time-consuming task. Often, adding even simple charts or graphs to your applications requires you to waste either effort creating a chart program, or money buying commercial graphics and chart add-on packages.
Using third-party graphics and chart add-on products in your applications has several drawbacks, however:
- It is not cost effective - it might cost hundreds or thousands of dollars for a sophisticated graphics and chart package.
- Compatibility is an issue - these third-party graphics and chart add-on tools are usually provided as DLL or COM components, which often leads to unexpected interface exceptions and unstable operations.
- There is little flexibility - these packages resemble black boxes because the source code is usually not provided, making it hard for you to add or modify any of their functionalities. You may often find that these third-party products lack the special features that you want to use in your applications, even though they may provide many other functionalities that you will never use.
- The coding is inefficient - these third-party add-on tools are often very large packages that contain far more functionalities than your applications require. Even a simple program made using these add-ons can end up having a huge final release. This is very inefficient for both coding management and distribution.
- License royalty is another issue - some third-party add-ons require not only the developing license, but also the distributed license royalty, resulting in an unnecessary increase of the development cost.
- Finally, maintenance is a problem - in many cases, third-party tools use a different programming language than the one you use to develop your applications, meaning you have to maintain the codes in an unmanaged manner.
Visual C# and its powerful GDI+ class make it possible to easily implement your own professional graphics and chart package using entirely managed C# codes. However, Visual C# provides no tools for creating three-dimensional (3D) graphics objects. Even a 3D point, the simplest 3D graphics object, must first be defined in a suitable 3D coordinate system before it can be used as a 3D graphics object.
Practical C# Charts and Graphics provides everything you need to create advanced charts and graphics in your .NET applications. In this book I will show you how to create a variety of graphics and charts that range from simple two-dimensional (2D) X-Y plots to complicated three-dimensional (3D) surface graphs using managed C# code. I try my best to introduce readers to the C# graphics program in a simple way--simple enough to be easily followed by C# beginners who have never had experience developing C# graphics and chart applications. You will learn from this book how to create a full range of color graphics applications and how to use C# controls to create impressive graphic and chart effects without having to buy expensive third-party add-ons.
Practical C# Charts and Graphics is not just a book, but a powerful 2D and 3D chart and graphics package in its own right. You may find that some of the examples in this book can be immediately used in solving your real-world problems, and that some may give you inspiration for adding advanced graphical and sophisticated chart capabilities to your own applications.
What This Book Includes
This book and its sample code listings, which are available for download at this website, provide you with:
- Complete, in-depth instruction on practical chart and graphics programming in Visual C# and GDI+. After reading this book and running the example programs, you will be able to add various sophisticated charts and graphics in your C# applications.
- Ready-to-run example programs that allow you to explore the chart and graphics techniques described in the book. You can use these examples to better understand how the chart and graphics algorithms work. You can also modify the code or add new features to it to form the basis of your own programs. Some of the example code listings provided in this book are already sophisticated chart and graphics packages, and can be used directly in your own real-world applications.
- Many C# classes in the sample code listings that you will find useful in your chart and graphics programming. These classes include matrix manipulation, coordinate transformation, color maps, 2D and 3D chart user controls, and other useful utility classes. You can extract these classes and plug them into your own applications.
- A chapter that contains a detailed discussion on how to integrate JavaScript chart and graphics library into C# Windows Forms applications. This chapter is designed specifically for readers who prefer not to create C# chart programs from scratch, and would like to take advantage of client-side chart and graphics applications.
- A chapter that contains how to embed Gincker Graphics into your C# desktop applications. It demonstrates that with Gincker Graphics, you can create a variety of charts and graphics, ranging from simple to sophisticated, just by typing in a mathematic formula, pasting a dataset, or uploading a data file; you do not need to write a single line of code or rely on any special software package.
Is This Book for You
You do not have to be an experienced C# developer or expert to use this book. I designed this book to be useful to people of all levels of C# programming experience. In fact, I believe if you have some experience with programming languages other than C#, you will be able to sit down in front of your computer, start up Microsoft Visual Studio .NET and C#, follow the examples provided in this book, and quickly become familiar with C# graphics programming. For those of you who are already experienced C# developers, I believe this book has much to offer you as well. There is much information in this book about graphics and chart programming that is not available in any other C# tutorial and reference book. In addition, most of the example programs provided in this book can be used directly in your real-world application development. This book will provide you with a level of detail, explanation, instruction, and sample program code that will enable you to do just about anything graphics and charts-related using Visual C#.
Perhaps you are a scientist, engineer, mathematician, student, or teacher instead of a professional programmer: this book is still a good bet for you. In fact, my own background is in theoretical physics, a field involving extensive numerical calculations, as well as graphical and charting representations of calculated data. I have dedicated my effort to this field for many years, starting from my undergraduate up to my Ph.D. My first computer experience was with FORTRAN. Later on, I had programming experience with Basic, C, C++, and MATLAB. I still remember how hard it was in the early days to present computational results graphically. I often spent hours creating a publication-quality chart by hand, using a ruler, graph paper, and rub-off lettering. A year later, our group bought a graphics and chart package. However, I still needed to prepare my data in a proper format in order to process the data with this package. During that time, I started paying attention to various development tools that could be used to create integrated applications. I tried to find an ideal development tool that would easily allow me to not only generate data (computation capability) but also represent data graphically (graphics and chart power). The C# and Microsoft Visual Studio .NET development environment made it possible to develop such integrated applications. Ever since Microsoft .NET 1.0 came out, I have been in love with the C# language, and have been able to use this tool to successfully create powerful graphics and chart applications, including commercial CAD packages.
The majority of the example programs in this book can be routinely used by C# developers and technical professionals. Throughout this book, I will emphasize the usefulness of C# chart and graphics programming to real-world applications. If you follow this book closely, you will be able to easily develop various practical graphics and chart applications, from simple 2D x-y plots to sophisticated 4D slice graphs. At the same time, I will not spend too much time discussing program style, execution speed, and code optimization, because there is already a plethora of books out there dealing with these topics. Most of the example programs in this book omit error handlings. This makes the code easier to understand by focusing on the key concepts.
What Do You Need to Use This Book
To make the best use of this book and understand the algorithm, you will need no special equipment. To run and modify the sample programs, you need a computer that is capable of running Windows 10 or Windows 7 operating system. The software installed on your computer should include Visual Studio 2019 or 2017 (Community version is fine).
All of the example programs in this book were created and tested in the Visual Studio 2019 Community Version under Windows 10, so it is better to run the example project on the same platform.
How This Book Is Organized
This book is organized into nine chapters, each of which focuses on a different topic concerning creating C# graphics and chart solutions. The following summaries of each chapter will give you an overview of this book contents:
Chapter 1, C# Graphics Basics
This chapter reviews some of the fundamental aspects of C# graphics programming. If you are an experienced
C# programmer, some of this material may already be familiar to you. It includes discussions of various coordinate
systems; basic graphics shapes in the GDI+ class; the color system; and advanced custom color maps used in C#
applications.
Chapter 2, 2D Matrices and Transformations
This chapter covers the mathematical basics of 2D graphics programming. 2D matrices and transformations in
homogeneous space are discussed, including translation, scaling, reflection, and rotation. These 2D matrices and
transformations allow a C# application to perform a wide variety of graphical operations on graphics objects in a
simple and consistent manner.
Chapter 3, 2D Line Charts
This chapter contains instructions of how to create elementary 2D X-Y line charts. It introduces basic chart
elements including chart area, plot area, axes, title, labels, ticks, symbols, and legend. These basic chart elements
are common in other types of charts as well.
Chapter 4, Specialized 2D Charts
This chapter covers the specialized charts often found in commercial chart packages and spreadsheet
applications. These specialized charts include bar charts, stair-step charts, stem charts, charts with error bars,
pie charts, area charts, polar charts, and stock charts.
Chapter 5, 3D Matrices and Transformations
This chapter extends the concepts described in Chapter 2 into the third dimension. It explains how to define 3D
graphics objects, and how to translate, scale, reflect, and rotate these 3D objects. It also describes the transformation
matrices that represent projections and transformations that allow you to view 3D graphics objects on a 2D screen.
Chapter 6, 3D Charts
This extensive chapter begins with a description of the coordinate system used in 3D charts and graphics,
and shows you how to create 3D coordinate axes, tick marks, axis labels, and grid lines. It then explains
techniques for creating a wide variety of 3D charts, including 3D line charts, 3D mesh and surface charts,
contour charts, 3D bar charts, 4D slice charts, and 3D combination charts. In creating these charts, a few specialized
techniques, such as Z-order, are used to manipulate the data displayed on your 2D computer screen.
Chapter 7, Charts and User Controls
This chapter shows you how to put 2D and 3D chart applications in a custom user control, and how to use such a
control in your C# applications. It begins by explaining the basics of the custom user controls in a C# Windows
application, including how to provide design-time support to the controls. Then, it describes in detail the
procedure of creating custom user controls for 2D and 3D chart applications, and demonstrates how to use
these controls in real-world C# applications.
Chapter 8, Embed JavaScript Charts
This chapter illustrates how to embed a JavaScript charting library into a Windows Forms application using a WebBrowser
control. It shows how to create various charts and graphics using the client-side charting library.
Chapter 9, Embed Gincker Graphics
This chapter explains how the Gincker Graphics platform can be embedded into a C# application through a WebView control.
It shows how to create various charts and graphics in C# Windows Forms applications using Gincker Graphics without
the need to write a single line of code.
What Is Left Out
This book provides an in-depth description of C# chart and graphics programming for real-world .NET applications. The background material concerning C# graphics was selected for inclusion in the book specifically according to the graphical needs of creating C# chart applications. Thus, this book does not cover image processing, such as the technique for manipulating bitmapped images and image animation. Advanced ray traced images that display reflective, shadowed, transparent, and textured objects are beyond the scope of this book and are not addressed.
Using Code Examples
You may use the code in this book in your applications and documentation. You do not need to contact me or the publisher for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing the example code listings does require permission. Incorporating a significant amount of example code from this book into your applications and documentation does require permission. Integrating the example code from this book into your commercial products is not allowed without the written permission from the author.