I want to add an annotation on vtkChartXY
. The annotation is vtkTextActor
.
My code is:
import vtkmodules.all as vtk
import numpy as np
colors = vtk.vtkNamedColors()
table = vtk.vtkTable()
arrX = vtk.vtkFloatArray()
arrX.SetName('X Axis')
table.AddColumn(arrX)
arrC = vtk.vtkFloatArray()
arrC.SetName('Cosine')
table.AddColumn(arrC)
numPoints = 69
inc = 7.5/(numPoints-1)
table.SetNumberOfRows(numPoints)
for i in range(numPoints):
table.SetValue(i, 0, float(i*inc))
table.SetValue(i, 1, float(np.cos(i*inc)))
view = vtk.vtkContextView()
view.GetRenderWindow().SetWindowName('linePlot')
chart = vtk.vtkChartXY()
view.GetScene().AddItem(chart)
line = chart.AddPlot(vtk.vtkChart.LINE)
line.SetInputData(table, 0, 1)
line.SetColor(0, 255, 0, 255)
line.SetWidth(1.0)
txtActor = vtk.vtkTextActor()
txtActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
txtActor.GetTextProperty().SetJustificationToLeft()
txtActor.GetTextProperty().SetVerticalJustificationToTop()
txtActor.SetInput('Annotation')
txtActor.SetPosition([0.008, 0.992])
render = list(view.GetRenderWindow().GetRenderers())[0]
render.AddActor(txtActor)
view.GetRenderWindow().Render()
view.GetInteractor().Initialize()
view.GetInteractor().Start()
Finally, there is no text:
How can I add some text on vtkChartXY
?
Any suggestion is appreciated~~~
Hello,
My two cents: maybe the vtkTextActor
is behind de chart so it does not appear. Perhaps setting a proper Z coordinate will cause it to show on top of the chart.
regards,
Paulo
@Paulo_Carvalho Thank you for your kindly reply.
It is confuzed for me. The vtkTextActor
is added using normalized viewport
:
txtActor.GetPositionCoordinate().SetCoordinateSystemToNormalizedViewport()
txtActor.SetPosition([0.008, 0.992])
How can I set the Z
coordinate?
Hello,
I’ve never used VTK for 2D rendering. VTK needs to know somehow that the text actor should appear over the chart. If there’s no Z, then there ought to be some other setting (drawing order?).
By normalized I understand that the coordinates vary betwen 0.0 and 1.0 (instead of pixel values, for instance). Normalization, as I understand it, doesn’t mean 2D space. Have you tried adding a third number to SetPosition()
?
take care,
Paulo
The SetPosition
do not support three input. The code txtActor.SetPosition([0.008, 0.992, 1.0])
report bug:
TypeError: SetPosition argument 1: expected a sequence of 2 values, got 3 values
In addition, I want to check the position of vtkChartXY
, and my code is:
render = view.GetRenderer()
actors = list(render.GetActors())
But, the actors
is empty, which means vtkChartXY
is not an actor, and I cannot check the position. It is stranger.
Then I believe you need to use some class of VTK Charts API to achieve the effect you want. I mean, using a vtkActor
won’t do.
https://kitware.github.io/vtk-examples/site/Cxx/#plotting
https://kitware.github.io/vtk-examples/site/Python/#plotting
Thank you for your suggestion. I will try these examples to find a solution. If they don’t help, I will read the source code to find how vtkChartXY
add title/legend or some other annotation.
mwestphal
(Mathieu Westphal (Kitware))
June 1, 2022, 8:23am
8
Hi @zhang-qiang-github ,
zhang-qiang-github:
vtkTextActor
You should use vtkBlockItem
Best,
Thank you for your kindly suggestion. I will try it.