1. Home
  2. Docs
  3. Bar Chart
  4. Bar Categories and Groups
  5. Configuring groups and categories from script

Configuring groups and categories from script

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

Like any feature of graph and chart, The bar categories and groups are completely configurable from script. 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 bar 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 and Groups

You can add a category to any bar chart in the following way:

Material categoryMaterial;
Color hoverColor,selectedColor;
var bar = GetComponent<BarChart>();
bar.DataSource.AddCategory("newCategory",new ChartDynamicMaterial(categoryMaterial,hoverColor,selectedColor))

You can remove a category in the following way:

bar.DataSource.RemoveCategory("oldCategory");

You can add and remove groups in the following way:

 bar.DataSource.AddGroup("newGroup");
 bar.DataSource.RemoveGroup("oldGroup");

SETTING CATEGORY STYLES FROM SCRIPT

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

Material categoryMaterial;
Color hoverColor,selectedColor;
bar.DataSource.SetMaterial("categoryName", new ChartDynamicMaterial(Material, hover, selected));

RENAMING A CATEGORY or a group

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

bar.DataSource.RenameCategory("oldName","newName") 
bar.DataSource.RenameGroup("oldName","newName") 

removing all categories and groups

You can remove all categories or groups at once . It can be done in the following way:

bar.DataSource.ClearCategories();
bar.DataSource.ClearGroups();

Checking if a category or a group exists in the bar chart

if(bar.DataSource.HasCategory("categoryName"))
{
}
if(bar.DataSource.HasGroup("groupName"))
{
}