In this tutorial, we will learn about how to do data visualizations using Delphi’s TChart or TeeChart.
TChart is the 100% Native Data-Aware Charting Component Library for Delphi and C++ Builder (v5 and later) and all RAD Studio versions.
Table of Contents
1. Generate the Dummy Data with Python
To create dummy data in Python, you can use pandas or the Faker library.
Here is the easiest way to create dummy data using pandas, and export them to CSV:
1 2 3 4 5 6 |
import pandas as pd # Create dummy data pandasDummy = pd.util.testing.makeDataFrame() # Export the data to .CSV file pandasDummy.to_csv('pandasExample.csv') |
By default, it will create 30 rows with 4 columns called A, B, C, and D and the index alpha-numeric. Run the code above in your favorite Python IDE (for me, it’s PyScripter, because it’s lightweight), you will get the following result:
The result:
Or you can try Faker, to generate more sophisticated data, with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import pandas from faker import Factory import random from datetime import datetime faker = Factory.create() status = 'created,delivered,returned'.split(',') def date_between(d1, d2): f = '%b%d-%Y' return faker.date_time_between_dates(datetime.strptime(d1, f), datetime.strptime(d2, f)) def fakerecord(): return {'awb': faker.numerify('######'), # random number eg:235533 'destination_city': faker.city(), # random cities 'product': 'random_product', # different products 'product_category': 'random_category', # different categories 'origin_city': faker.city(), # random metro cities 'logistics_provider_id': faker.numerify('##'), # id's eg:1,20,28,27 'dispatch_date': date_between('mar01-2015', 'mar15-2015'), # datetime between mar01-2015 to mar15-2015 'final_delivery_status': random.choice(status), # created,delivered,returned 'actual_delivery_date': date_between('mar16-2015', 'mar30-2015'), # datetime between mar16-2015 to mar30-2015 'promised_delivery_date': date_between('mar25-2015', 'apr06-2015'), # datetime between mar25-2015 to Apr6-2015 } example_dummy_data = pandas.DataFrame([fakerecord() for _ in range(1000)]) # Export to CSV example_dummy_data.to_csv('fakerExample.csv', index=False) |
The code above will generate dummy data with various Python data types and 1000 rows of observations in total:
2. How can we use TChart?
The TChart component is the basic building block for ‘non-database-aware’ Charts.
First, start an empty VCL app, by navigating to File -> New -> Windows VCL Application – Delphi:
Select the TChart component from the Delphi palette and simply drag it onto your form to include a Chart in your application:
This is how TChart will look like in your empty VCL:
You can edit the chart by double-click the TChart component. For example, you can add new data series, by navigating to Series > Add > Choose the chart as you need:
You can easily create enterprise-grade plots using the Import option or TDataImport class. Using this functionality, you can seamlessly plot any data from any sources, like .CSV files, SQLs, JSON, the dataset from URLs, and many more. Unfortunately, this feature only available in TChart Pro, so I strongly recommend you to try TChart Pro which offers hundreds of Graph styles in 2D and 3D for data visualization, 56 mathematical, statistical, and financial Functions for you to choose from together with an unlimited number of axes and 30 Palette components. Visit this link, this tutorial, or this video for more information.
So, for this tutorial, we need to input our data, and create our data series manually. In Series, go to Data Source by double-clicking the Series, and change Random to Manual.
Now, you can input our dummy data generated by Pandas. In this example, I input 4 data series with 10 rows of observations:
This is how our data visualized in TChart’s Bar Chart:
This is how our data visualized in TChart’s Line Chart:
Congratulations! You have learn how to visualize Python’s data using Delphi’s TChart!
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.