# vtkCubeAxesActor2D, setting label format for individual axis

**URL:** https://discourse.vtk.org/t/vtkcubeaxesactor2d-setting-label-format-for-individual-axis/1436
**Category:** Support
**Created:** [July 29, 2019, 1:43pm UTC](https://discourse.vtk.org/t/vtkcubeaxesactor2d-setting-label-format-for-individual-axis/1436 "2019-07-29T13:43:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![francek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/francek/32/10505_2.png) [@francek](https://discourse.vtk.org/u/francek)
#### Post date: [July 29, 2019, 1:43pm UTC](https://discourse.vtk.org/t/vtkcubeaxesactor2d-setting-label-format-for-individual-axis/1436/1 "2019-07-29T13:43:50Z")

</div>

Hi,

I fail to set different label format for each axis in vtkCubeAxesActor2D.

When I run [CubeAxesActor2D](https://lorensen.github.io/VTKExamples/site/Cxx/Visualization/CubeAxesActor2D/) example and replace line  
axes2-\>SetLabelFormat("%6.4g");  
by  
axes2-\>GetXAxisActor2D()-\>SetLabelFormat("%3.1f");  
format for the X axis is not changed but all axes share the same default format i.e. “6.3g”.

Any idea why/how to change label format per axis?

Thank you.

---

<div class="post-metadata">

### Author: ![lplavorante](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lplavorante/32/272_2.png) [@lplavorante](https://discourse.vtk.org/u/lplavorante)
#### Post date: [July 31, 2019, 4:01pm UTC](https://discourse.vtk.org/t/vtkcubeaxesactor2d-setting-label-format-for-individual-axis/1436/2 "2019-07-31T16:01:10Z")

</div>

Hi Francek!

I had the same problem and resorted to subclassing vtkCubeAxisActor2D. This is the code I used to obtain a specific label format only for the Z axis. This was back with VTK 7.0 and I can’t say if the current version provides an easier way to achieve this.

// Extends vtkCubeAxesActor2D to allow separated adjustment in the number of decimal places along the Z axis.  
class vtkFormatZLabelCubeAxesActor2D : public vtkCubeAxesActor2D  
{  
public:  
static vtkFormatZLabelCubeAxesActor2D \*New()  
{  
return new vtkFormatZLabelCubeAxesActor2D;  
}

```
void SetZLabelFormat(const char *format)
{
    snprintf(m_zLabelFormat, s_zLabelFormatSize - 1, "%s", format);
}

const char *GetZLabelFormat() const
{
    return m_zLabelFormat;
}

int RenderOpaqueGeometry(vtkViewport *viewport) override {
    const int ret = vtkCubeAxesActor2D::RenderOpaqueGeometry(viewport);
    ZAxis->SetLabelFormat(m_zLabelFormat);
    if (ZAxisVisibility)
    {
        ZAxis->RenderOpaqueGeometry(viewport);
    }

    return ret;
}

void ShallowCopy(vtkFormatZLabelCubeAxesActor2D *actor)
{
    vtkCubeAxesActor2D::ShallowCopy(actor);
    SetZLabelFormat(actor->GetLabelFormat());
}

```

protected:

```
const size_t s_zLabelFormatSize = 16;
char m_zLabelFormat[s_zLabelFormatSize];

vtkFormatZLabelCubeAxesActor2D()
    : vtkCubeAxesActor2D()
{
    snprintf(m_zLabelFormat, s_zLabelFormatSize - 1 , "%s", LabelFormat);
}

virtual ~vtkFormatZLabelCubeAxesActor2D()
{
}

```

};

This was several months ago and I was in a hurry. Maybe the code is not 100% ok , but it works.

```
Greetings,

   Luca
```

---

<div class="post-metadata">

### Author: ![francek](https://discourse.vtk.org/user_avatar/discourse.vtk.org/francek/32/10505_2.png) [@francek](https://discourse.vtk.org/u/francek)
#### Post date: [August 1, 2019, 1:39pm UTC](https://discourse.vtk.org/t/vtkcubeaxesactor2d-setting-label-format-for-individual-axis/1436/3 "2019-08-01T13:39:27Z")

</div>

Thank you very much Luca, it does work.

For my need I just changed the RenderOpaqueGeometry() function:

```
int RenderOpaqueGeometry(vtkViewport *viewport) override {
	auto z_vis {ZAxisVisibility};
	ZAxisVisibility = false; // prevent calling ZAxis->RenderOpaqueGeometry();
                                 // from vtkCubeAxesActor2D
	const int ret = vtkCubeAxesActor2D::RenderOpaqueGeometry(viewport);
	if (z_vis)
	{
		ZAxis->SetLabelFormat(m_zLabelFormat);
		ZAxis->RenderOpaqueGeometry(viewport);
	}

	ZAxisVisibility = z_vis;
	
	return ret;
}

```

Francek

---

<div class="post-metadata">

### Author: ![lplavorante](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lplavorante/32/272_2.png) [@lplavorante](https://discourse.vtk.org/u/lplavorante)
#### Post date: [August 1, 2019, 3:23pm UTC](https://discourse.vtk.org/t/vtkcubeaxesactor2d-setting-label-format-for-individual-axis/1436/4 "2019-08-01T15:23:05Z")

</div>

Very good!🕶
