vtkCubeAxesActor2D, setting label format for individual axis

Hi,

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

When I run 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.

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

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

Very good!:sunglasses: