1. Home
  2. Docs
  3. Prefabs
  4. Storing and restoring category prefabs

Storing and restoring category prefabs

As we you can see in the various chapters of this documentation , You can use DataSource.AddCategory on different charts in order to add new categories to charts. However the down side of this approach is that these categories cannot be configured in the inspector. There is an alternative approach to creating categories dynamically with graph and chart. This tutorial is explains how you can store categories in premade chart prefabs and then copy them to your own charts. This is explained using the graph chart , However the same concept can be done with pie charts as well. Check out the last section for Bar Charts

Create a chart prefab with category styles

Create a chart prefab with the comfort of the unity inspector. Name the categories with their number starting from 1. You can also duplicate one of the ready made prefabs in Resources/Chart And Graph.

For example the DefualtGraphCategoryStyle2D contains 10 preconfigured category styles:

Load the category styles from your own monobehviour

Create a monobehviour that can accept a chart prefab:

public GraphChartBase categoryPrefab;

Drag the prefab into ‘categoryPrefab’

Add an object array that will hold the visual styles of the categories

private object[] mCategoryVisualStyle;

Extract the visual Styles from the prefab

Now you can extract the visual styles from the prefab by using the following call:

mCategoryVisualStyle = prefab.DataSource.StoreAllCategoriesinOrder();

Create a category with a stored visual style

firstly , create a category and pass null or 0 to all arguments.

graph.DataSource.AddCategory("prefabCategory", null, 0, new MaterialTiling(), null, false, null, 0);

Now you can call restoreCategory in order to apply the visaul style to it

// set the visual style of the category to the one in the prefab
graph.DataSource.RestoreCategory("prefabCategory", mCategoryVisualStyle[3]);    

each index in the array mCategoryVisualStyle corresponds with a preconfigured category.

And what about bar charts ?

Bar chart categories contain only a ChartDynamicMaterial. Therefore there is no need to create a prefab. You can simply create a mono behavior with an array ChartDynamicMaterial. for example:

// these can be defined in the inspector
public ChartDynamicMaterial[] categoryStyles;

// you can add a bar category like this:

bar.AddCategroy("styleCategory",categoryStyles[3]);