Interesting Questions Submitted To Support

  1. Home
  2. Docs
  3. Interesting Questions Submitted To Support
  4. How to rotate a single bar in the bar chart

How to rotate a single bar in the bar chart

One of our users had the requirement of having one of the bars in the 3D bar chart rotate when selected. Graph and Chart allows you to specify your own prefabs , you can customize these prefabs to match your requirements. In this article we will describe how you can customize your chart prefabs. We offered the following solution and it was adopted by the user in order to rotate the bar item.

How prefabs in the bar chart work

When the bar chart is created , the bar prefab is instantiated for each bar. Each instantiated bar item has a BarInfo component attached to it. The bar info component can be used to get information about the current bar. You can then add a controllable behavior to your bar prefab and call a method on it. The method can inspect the bar information and decide what to do accordingly

The solution

The solution we offered involved two monobehaviors. On is attached to the bar chart and the other to the bar prefab.

The first is the MasterBarRotation. It is attached to the bar chart game object. It allows CustomBarRotation objects to register with it so we call them later. Notice that this stage is important because we would only like to control the bar in the under MasterBarRotation . Another approach could be calling FindComponentsInChildren.

List<CustomBarRotation> mRotations =new List< CustomBarRotation>();

Public void Register(CustomBarRotation rotation)
{
mRotations.add(rotation);
}
Public void Unregister(CustomBarRotation rotation)
{
mRotations.remove(rotation);
}
//this method will rotate the bar with the category and group specified
Public void RotateBar(string category,string group)
{
Foreach(CustomBarRotation rot in mRotations)
               Rot.Rotate(category,group);
}

Now . We would like to create the behavior that registers with MasterBarRotation. It is called CustomBarRotation and it’s code is:

void Start()
{
 //register with MasterBarRotation when started
        GetComponentInParent<MasterBarRotation>().Register(this);
}

void OnDestroy()
{
//unregister when destroyed
        GetComponentInParent<MasterBarRotation>(). Unregister (this);
}

// when rotate is called . a check is performed to see if this bar is the bar that should be rotated
void Rotate(string category,string group)
{
// get the bar info of the prefab
var barInfo = GetComponent<BarInfo>();
// check for a category match
if(barInfo.cateogry != category) 
return;
//check for a group match
if(barInfo.group != group)
return;
//we know that this is the bar that should be rotated
//here you add your code that rotates the bar
}

Now we can hook the Selected event in the bar chart and call rotate:

        void BarSelected(BarChart.BarEventArgs args)
        {
              barchart.GetComponent<MasterBarRotation>().Rotate(args.category,args.group);
        }