1. Home
  2. Docs
  3. Graph Chart
  4. Graph Categories
  5. Configuring categories from script

Configuring categories from script

This article is about adding , removing,enabling ,disabling and changing the visual features of categories from script. If you want to lean how to fill categories with data , see the Graph Chart Tutorial and Filling the graph chart with data

Like any feature of graph and chart, The graph categories are completely configurable from script as well. You may want to take a look at Obtaining a chart object for scripting before proceeding with this article. For an alternative way of configuring categories with Graph and Chart , check out Storing and restoring category prefabs

Also , you may want to look at Category Settings for a quick overview of the visual properties of graph categories

How to load material and Prefabs into your script

in order to configure a category you may need to pass materials and prefabs as parameters in your script. You can obtain these parameters in the very same way you would with any unity resources. Check out these two links for more information :

Adding and removing categories

The parameters in this section are explained in more detail in Category Settings . You can pass a null material if you want to disable a visual feature.

You can add categories to a 2D Graph chart in the following way :

public Material lineMaterial,pointMaterai,fillMaterial; 
public double lineThickness = 2.0 ,pointSize =5.0;
public bool stertchFill = false;

graph = GetComponent<GraphChartBase>();
//add a category to a 2D graph
var lineTiling = new MaterialTiling(true,20); // see the article about material tiling
graph.DataSource.AddCategory("NewCategory",lineMaterial,lineThickness,lineTiling,fillMaterial,stertchFill,pointMaterial,pointSize)

You can add categories to a 3D Graph chart in the following way , you can find ready made prefabs in Prefabs/3D/Graph:

public Material lineMaterial,pointMaterai,fillMaterial; 
public PathGenerator linePrefab;
public FillPathGeneartor pathPrefab;
public GameObject pointPrefab;
public double lineThickness = 2.0 ,pointSize =5.0;
public bool stretchFill = false;
public bool isCurve = false;
public int SegmentsPerCurve;
graph = GetComponent<GraphChartBase>();
//add a category to a 2D graph
var lineTiling = new MaterialTiling(true,20); // see the article about material tiling

graph.DataSource.AddCategory3DGraph("NewCategory", linePrefab, lineMaterial, lineThickness, lineTiling, fillPrefab, fillMaterial, stretchFill, pointPrefab, pointMaterial, pointSize, depth, isCurve, SegmentsPerCurve);

You can remove a category by calling the following method:

graph.DataSource.RemoveCategory("oldCategory");

Setting category Styles from script

You can control the style of a category from script in the following way :

public Material lineMaterial,pointMaterai,fillMaterial; 
public double lineThickness = 2.0 ,pointSize = 5.0;
public bool stertchFill = false;
public Material lineMaterial,pointMaterai,fillMaterial; 
public double lineThickness = 2.0 ,pointSize = 5.0;
public bool stertchFill = false;

graph.DataSource.SetCategoryLine("category", lineMaterial, lineThickness, lineTiling);
graph.DataSource.SetCategoryFill("category", fillMaterial, stretchFill);
graph.DataSource.SetCategoryPoint("category", pointMaterial, pointsIze);

You can set the prefabs of a 2D category in the following way:

public ChartItemEffect lineHover,pointHover;
graph.DataSource.Set2DCategoryPrefabs("category", lineHover, pointHover);

You can set the depth and prefabs of 3D category with the following calls:

public PathGeneration linePrefab;
public FillPathGeneartion pathPrefab;
public GameObject pointPrefab;
public double depth;

graph.DataSource.Set3DCategoryPrefabs("category", linePrefab, fillPrefab pointPrefab);
graph.DataSource.Set3DCategoryDepth("category", depth);

Enabling and Disabling Categories from Script

In order to enable or disable a category , while maintaining it’s data within the chart. You can make the following call:

public bool isEnabled;

graph.DataSource.SetCategoryEnabled("category", isEnabled);

Renaming a category

you can rename a category in the following way , make sure the new name does not exist in the chart

graph.DataSource.RenameCategory("prevName", "newName");

Removing all the categories from the graph chart

graph.DataSource.Clear()

Checking if a category exists in a graph chart

if(graph.DataSource.HasCategory("categoryName"))
{
}