1. Home
  2. Docs
  3. Basic Concepts
  4. Obtaining a chart object for scripting

Obtaining a chart object for scripting

In order to use Graph and Chart from script , we must first obtain the chart object. This can be done easily :

  • have a reference to a Game Object with the chart component in it.
  • Call GetComponent on the gameobject
  • It is best practice to make sure the object you obtained is not null , this will happen if you have the wrong game object or that the game object does not contain the component you are seeking

For example:

BarChart bar = gameobject.GetComponent<BarChart>();

Now the BarChart is an abstract class , that can be used with either a 3D Bar chart or a 2D bar chart . Sometime we would like to use properties which are specific for each of these. So we can get them in the following way :

var canvasBar = gameobject.GetComponent<CanvasBarChart>();

var worldBar = gameobject.GetComponent<WorldSpaceBarChart>();

Make sure to know the different base types , all follow the pattern above except for the graph chart. the graph chart can be obtained in the following way:

var abstractGraphChart = gameobject.GetComponent<GraphChartBase>();

var canvasGraphChart = gameobject.GetComponent<GraphChart>();

var worldGraphChart = gameobject.GetComponent<WorldSpaceGraphChart>();

Handling data in a chart object

as a rule of thumb , when you want to script a chart’s data or control how data is displayed , you will need to access the chart’s data source. It can be done like this:

var graph = gameobject.GetComponent<GraphChartBase>();

graph.DataSource.<some property or method>

Obtaining Axis And Label objects

When we want to program axis or labels from code , we can obtain them in a very similar way :

var horizontalAxis = gameobject.GetComponent<HorizontalAxis>();

var verticalAxis = gameobject.GetComponent<VerticalAxis>();

var ItemLabels = gameobject.GetComponent<ItemLabels>();

var categoryLabels = gameobject.GetComponent<CategoryLabels>();

var groupLabels = gameobject.GetComponent<GroupLabels>();