how to fix the font size/length for vtkLegendScaleActor

I use vtkLegendScaleActor in my project. When the window is resized, I expect the font size/length of vtkLegendScaleActor is fixed.

In the resizeEvent:

    def resizeEvent(self, evt) -> None:
        super().resizeEvent(evt)

        y = self.height()
        dy = round((y-100)/2)
        self.legendScaleActor.SetTopBorderOffset(dy)
        self.legendScaleActor.SetBottomBorderOffset(dy)
        self.legendScaleActor.GetLeftAxis().SetSizeFontRelativeToAxis(False)
        self.legendScaleActor.GetLeftAxis().UseFontSizeFromPropertyOn()
        self.legendScaleActor.GetLeftAxis().GetLabelTextProperty().SetFontSize(10)
        self.legendScaleActor.Modified()
        self.renderWindow.Render()

I find the setting in resizeEvent do not work. When I change the window size by mouse, I find the length of axis is changed, and the font size is also changed.

The complete code is:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.vtkWidget = QVTKRenderWindowInteractor(self)
        self.renderer = vtk.vtkRenderer()
        self.renderWindow = self.vtkWidget.GetRenderWindow()
        self.renderWindow.AddRenderer(self.renderer)

        sphereSource = vtk.vtkSphereSource()
        sphereSource.SetCenter(0, 0, 0)
        sphereSource.SetRadius(1.0)

        sphereMapper = vtk.vtkPolyDataMapper()
        sphereMapper.SetInputConnection(sphereSource.GetOutputPort())
        sphereActor = vtk.vtkActor()
        sphereActor.SetMapper(sphereMapper)

        self.renderer.AddActor(sphereActor)

        self.legendScaleActor = vtk.vtkLegendScaleActor()
        self.renderer.AddActor(self.legendScaleActor)
        self.legendScaleActor.AllAxesOff()
        self.legendScaleActor.SetLeftAxisVisibility(True)

        layout = QVBoxLayout()
        layout.addWidget(self.vtkWidget)
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

        self.vtkWidget.Initialize()
        self.vtkWidget.Start()

    def resizeEvent(self, evt) -> None:
        super().resizeEvent(evt)

        y = self.height()
        dy = round((y-100)/2)
        self.legendScaleActor.SetTopBorderOffset(dy)
        self.legendScaleActor.SetBottomBorderOffset(dy)
        self.legendScaleActor.GetLeftAxis().SetSizeFontRelativeToAxis(False)
        self.legendScaleActor.GetLeftAxis().UseFontSizeFromPropertyOn()
        self.legendScaleActor.GetLeftAxis().GetLabelTextProperty().SetFontSize(10)
        self.legendScaleActor.Modified()
        self.renderWindow.Render()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Any suggestion is appreciated~~~

My vtk version is 9.2.6.

Hello,

Have you tried SetAdjustLabels([True|False]) on self.legendScaleActor before setting the font size?

best,

PC

I have tried SetAdjustLabels(False), but it doesn’t work.

I find the following resizeEvent works:

def resizeEvent(self, evt) -> None:
    super().resizeEvent(evt)
    y = self.height()
    dy = round((y-300)/2)
    # print(size, y, dy)
    self.legendScaleActor.SetTopBorderOffset(dy)
    self.legendScaleActor.SetBottomBorderOffset(dy)
    self.legendScaleActor.SetCornerOffsetFactor(1)

    relativeFontSize = 800/(self.width()+self.height())
    self.legendScaleActor.GetLeftAxis().SetLabelFactor(1.0)
    self.legendScaleActor.GetLeftAxis().SetSizeFontRelativeToAxis(False)
    self.legendScaleActor.GetLeftAxis().SetFontFactor(relativeFontSize)
    self.legendScaleActor.Modified()
    self.renderWindow.Render()
2 Likes