1. Home
  2. Docs
  3. Advanced Concepts
  4. Using script to add or remove chart components

Using script to add or remove chart components

In this article you will learn how to add categories and visual features from script. This article is based on the scene in
“Extras/Example Scenes/AddRemoveCategoriesFromScript”

Creating a prefab object

In order to add categories and visual features from script, You should create a prefab object for a category or visual feature. You can do that by creating a unity prefab and then attaching it with a chart component.


If you go “Extras/Example Scenes/AddRemoveCategoriesFromScript” you can find an example of two such prefabs.

Adding the prefab object via script

following the example in “Extras/Example Scenes/AddRemoveCategoriesFromScript” . we can now create a new category with a graph line:

      public DataSeriesCategory CategoryPrefab; // set this from the inspector
      public DataSeriesVisualFeature VisualFeaturePrefab; // set this from the inspector
      
.
.
.
void Start()
{
      ...
      seriesChart.DataSource.AddCategoryFromPrefab("newCat", CategoryPrefab);
      var categoryObject = seriesChart.DataSource.GetCategory("newCat");
      categoryObject.AddVisualFeature("NewGraphLine",VisualFeaturePrefab);
      var data = categoryObject.Data;
      ...
}

Removing visual features and categories via script

You can remove categories and visual features by calling the remove methodsnull

    var cat12 = seriesChart.DataSource.GetCategory("cat12");
    cat12.RemoveVisualFeature("Graph Line-0"); // removes the visual property named GraphLine-0

    seriesChart.DataSource.RemoveCategory("cat12");// remove category // removes the category name cat12