Spreadsheets have a lot of different features that make manipulating data very intuitive and easy. One of the main features that helps with visualization of that data is charts. Having the ability to give the power of data visualizations to users and customers can be an invaluable resource for developers. In this blog, I will show you how to create a simple spreadsheet with the ability to add different types of charts based on the data in the spreadsheet. In this project, there are only two forms: the MainForm and the GenerateChart form. The MainForm contains the Spread component and is the main point of action for the program. The GenerateChart form has logic for handling the interface between user input and chart creation. I started off by creating the MainForm and adding the Spread component to it: The main form. The “Create Chart” button is what opens up the GenerateChart form, using the following code:
The MainForm is passed to the GenerateChart form as the parent form. Whenever the user chooses to create a new chart, a form like the following opens up: The GenerateChart form. This form allows the user to choose a chart type from a set list using the drop down menu under “Chart Type”. The user can then add a name to the chart and select the series to use for the chart. Once the information is filled in and “Create Chart” is clicked, a chart with the specified properties is created within the MainForm:
The MainForm with a chart loaded into it. In my implementation, I wanted to have the chart displayed neatly within the bounds of the rows and columns that comprised the sheet, so I wrote code to calculate the location and size of the chart plot area:
In order to provide support for different types of charts, I wrote a function for each type of series. The series methods all pretty much have the same format as the one below:
A series of a specific type is created out of the range provided by the parameters. The names are added in order to have labels in the charts. Once a series has been created, a chart can be made using this function:
This function is used to create an initial chart with properties that are changed once cell ranges are created. This can be seen with the button click function in the GenerateChart form:
These particular charts can handle at most 4 series (the 4 categories in the spreadsheet). Every chart created can have less than 4 series, and the other series could be passed in as null values. Both creating a chart and updating a chart utilized the updateChart function, which gives the chart all of its labels and legend and ensure that the data is displayed correctly:
Now whenever a chart is created, a user can edit the values that the chart is based on and click the “Update Chart” button to have the changes reflected in the chart. In this blog, I have shown how to create a Windows Form application that utilizes Spread’s charting capabilities. The creation of charts is a simple and intuitive process that makes it easy for developers to add charts to their spreadsheet applications. Different kinds of charts can be created with very simple code that is generalized across most of the chart types. With this functionality, Spread can be a powerful tool for data visualization. The download link below contains the project files for this sample. WinForms Charting