Skip to content

rendering: Add ForceVerticalTitle option to vtkScalarBarActor

This new option is off by default, but when turned on, renders the scalar bar title vertically rather than horizontally.

In the animation below, I'm toggling on/off features of the vtkScalarBarActor to show how they interact with the new ForceVerticalTitle option.

output

Python application used to make animation
from vtkmodules.vtkFiltersCore import vtkCleanPolyData
from vtkmodules.vtkFiltersGeneral import vtkDistancePolyDataFilter
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkRenderingAnnotation import vtkScalarBarActor
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderer,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
)
from vtkmodules.vtkRenderingOpenGL2 import *


if __name__ == "__main__":
    sphereSource1 = vtkSphereSource()
    sphereSource1.SetCenter(1, 0, 0)
    sphereSource1.SetPhiResolution(21)
    sphereSource1.SetThetaResolution(21)
    sphereSource1.Update()
    input1 = sphereSource1.GetOutput()

    sphereSource2 = vtkSphereSource()
    sphereSource2.SetPhiResolution(21)
    sphereSource2.SetThetaResolution(21)
    sphereSource2.Update()
    input2 = sphereSource2.GetOutput()

    clean1 = vtkCleanPolyData()
    clean1.SetInputData(input1)

    clean2 = vtkCleanPolyData()
    clean2.SetInputData(input2)

    distanceFilter = vtkDistancePolyDataFilter()

    distanceFilter.SetInputConnection(0, clean1.GetOutputPort())
    distanceFilter.SetInputConnection(1, clean2.GetOutputPort())
    distanceFilter.Update()

    mapper = vtkPolyDataMapper()
    mapper.SetInputConnection(distanceFilter.GetOutputPort())
    mapper.SetScalarRange(
        distanceFilter.GetOutput().GetPointData().GetScalars().GetRange()[0],
        distanceFilter.GetOutput().GetPointData().GetScalars().GetRange()[1])

    actor = vtkActor()
    actor.SetMapper(mapper)

    scalarBar = vtkScalarBarActor()
    scalarBar.SetLookupTable(mapper.GetLookupTable())
    scalarBar.SetTitle("Distance")
    scalarBar.SetNumberOfLabels(4)
    scalarBar.UnconstrainedFontSizeOn()
    # scalarBar.SetOrientationToHorizontal()

    renderer = vtkRenderer()

    renWin = vtkRenderWindow()
    renWin.AddRenderer(renderer)
    renWin.SetSize(600, 500)
    renWin.SetWindowName("DistancePolyDataFilter")

    def keypress_callback(obj, ev):
        key = obj.GetKeySym()
        if key == 'v':
            forceVertical = scalarBar.GetForceVerticalTitle()
            scalarBar.SetForceVerticalTitle(not forceVertical)
        if key == 'o':
            isVert = scalarBar.GetOrientation() != 0
            if not isVert:
                scalarBar.SetOrientationToVertical()
            else:
                scalarBar.SetOrientationToHorizontal()
        if key == 'u':
            ufs = scalarBar.GetUnconstrainedFontSize()
            if ufs:
                scalarBar.UnconstrainedFontSizeOff()
            else:
                scalarBar.UnconstrainedFontSizeOn()
        if key == 'p':
            if scalarBar.GetTextPosition() == 0:
                scalarBar.SetTextPosition(1)
            else:
                scalarBar.SetTextPosition(0)

        renWin.Render()

    renWinInteractor = vtkRenderWindowInteractor()
    renWinInteractor.SetRenderWindow(renWin)
    renWinInteractor.AddObserver('KeyPressEvent', keypress_callback, 1.0)

    renderer.AddActor(actor)
    renderer.AddActor2D(scalarBar)

    renWin.Render()
    renWinInteractor.Start()
Edited by Scott Wittenburg

Merge request reports