Skip to content

Plotting with DataFrame

Nicolò Santilio edited this page Nov 24, 2020 · 8 revisions

DataFrame is a class implemented by this plugin.
It uses a Matrix to hold chart data in a simple and accessible structure, adding an header (an Array of strings or characters to identify columns) and an index (an Array of strings or characters to identify rows) to plot a chart with contextual information, or just to print it in a presentable way.
DataFrame and Matrix are classes you may use if you want to plot charts from code. If you are just interested in plotting charts using files (.CSV), referr to this other section instead.

Table of contents:

  1. What's a Matrix
  2. What's a DataFrame
  3. (Example) Use a DataFrame to plot a PieChart
  4. (Example) Use a DataFrame to plot a RadarChart

1. What's a Matrix

If you have ever dealt with MatLAB or python ML libraries, you already know where this is going. Otherwise, I'll make things easier for you.

A Matrix (pl. Matrices) is a mathematical entity identified by a rectangular array, arranged in rows and columns.
Matrices are used in different contexts, from geometric and arithmetic operations, to machine learning and clustering.
In our case, we will use matrices to store complex nested Arrays of Arrays.
In gdscript, a Matrix class is nothing else other than an big single Array which contains multiple Arrays: while internal Arrays will be handled as rows, each element of each array will be a column element for its Array. !! add image, from real Matrix to gdscript Matrix Of course, the Matrix class will offer more built-in functionalities to properly work with these objects and to use them in every environment possible.

  • Matrix Class
  • MatrixGenerator Class
  • How to use Matrix and MatrixGenerator

Matrix Class

...

MatrixGenerator Class

...

How to use Matrix and MatrixGenerator

As we said in the incipit, a Matrix basically consists in a big Array arranged in m rows and n columns. These kind of structures are generally represented as m x n , like a 3x4 matrix which is a matrix with 3 rows and 4 columns.
From this perspective, it is also possible to generalize the concept of an Array: an Array is a one-dimension Matrix (which in common programming languages is a 1xn Matrix, but in math and other languages can also be a "column" Array, that is nx1). But why are Matrices so important for plotting charts in this plugin? Well, let's answer to this together.
Even though some charts don't require complex data structers in both dimensions, vertically and horizontally - like PieCharts, where the max complexity is 2xn or mx2 for a DataFrame, and 1xn / mx1 for Matrix - , most of the charts use DataFrames (we will talk about them later) wich may contain complex Matrices. So let's start from a general medium case to simple, specific cases.

Let's assume that I want to plot a RadarChart displaying the stats of characters in a party for an RPG Game. To plot these data I first need to store and manage them in some way.
If I had to store them in a single CSV file it would be really easy to plot it using these instructions, but what if I've got a single file as a Dictionary structure for each character, or stats are hard coded and they are not even ordered in some way?
First, I'd need to order them in different arrays... or if I am sadistic, in one single big Array from the beginning (please don't).

var char_1_stats : Array = [3, 8, 2, 1, 4]
var char_2_stats : Array = [2, 10, 1, 3, 5]
...
var char_n_stats : Array = [9, 11, 1, 1, 3]
var chars_stats : Array = [char_1_stats, char_2_stats, ..., char_n_stats]
or 
var chars_stats : Array = []
chars_stats.append(char_1_stats) ...

then, I could build a DataFrame with few lines of code and plot it as an Array

var radar_chart : RadarChart = Chart.instance(Chart.TYPES.Radar)
add_child(radar_chart)
radar_chart.plot_from_array(chars_stats)

but this way, there are some flaws that cannot be easily handled with nested Arrays:

  1. Most of the time they require nested for cycles to operate over multiple single elements
  2. It's easy to get a single row or a single element, but not to get a single column or operate over it with the same ease
  3. Arrays do not provide built-in identity functions other than Pools to check single element types or to easily implement logic for structures

2. What's a DataFrame

...