Non-interactive charts are still (partially) interactive
When a chart is made non-interactive, it still responds to certain mouse events. The issue can be reproduced using following python script, which creates a certain chart (specified through the chart_type
variable) that is made non-interactive on the left and an interactive pie chart on the right. While panning an XY chart is no longer possible, tooltips still show up for all chart types when hovering the mouse over the left (non-interactive) chart.
Script
import vtk
# TODOs on VTK side:
# ChartXY should call RecalculatePlotBounds when plot data or axis is modified?
# ChartPie (and others) show a tooltip even when not interactive
# Parallel chart supporting custom geometries (like pie and box chart changes)
def create_xy_chart():
data = [(1, 2), (2, 0), (3, 1)]
chart = vtk.vtkChartXY()
table = vtk.vtkTable()
arrX = vtk.vtkFloatArray()
arrX.SetName(f"X")
arrY = vtk.vtkFloatArray()
arrY.SetName(f"Y")
table.AddColumn(arrX)
table.AddColumn(arrY)
numPoints = len(data)
table.SetNumberOfRows(numPoints)
for p in range(numPoints):
table.SetValue(p, 0, data[p][0])
table.SetValue(p, 1, data[p][1])
plot = chart.AddPlot(vtk.vtkChart.POINTS)
plot.SetInputData(table, 0, 1)
return chart
def create_parallel_chart():
data = [(1, 2, 3), (2, 0, 1), (3, 1, 2)]
chart = vtk.vtkChartParallelCoordinates()
table = vtk.vtkTable()
numFields = len(data)
for f in range(numFields):
arr = vtk.vtkFloatArray()
arr.SetName(f"Field{f}")
table.AddColumn(arr)
numPoints = len(data[0])
table.SetNumberOfRows(numPoints)
for f in range(numFields):
for p in range(numPoints):
table.SetValue(p, f, data[f][p])
plot = chart.GetPlot(0)
plot.SetInputData(table)
return chart
def create_2dhist_chart():
data = [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
chart = vtk.vtkChartHistogram2D()
img = vtk.vtkImageData()
img.SetExtent(0, len(data)-1, 0, len(data[0])-1, 0, 0)
img.AllocateScalars(vtk.VTK_INT, 1)
for i in range(len(data)):
for j in range(len(data[0])):
img.SetScalarComponentFromFloat(i, j, 0, 0, data[i][j])
cs = vtk.vtkColorSeries()
cs.SetColorScheme(vtk.vtkColorSeries.BREWER_DIVERGING_SPECTRAL_11)
numColors = cs.GetNumberOfColors()
colors = [c / 255 for i in range(numColors) for c in cs.GetColor(i)]
tf = vtk.vtkColorTransferFunction()
tf.BuildFunctionFromTable(0, 4, numColors, colors)
chart.SetInputData(img)
chart.SetTransferFunction(tf)
return chart
def create_box_chart():
data = [0, 1, 1, 2, 3, 3, 4]
chart = vtk.vtkChartBox()
dataTable = vtk.vtkTable()
arrD = vtk.vtkIntArray()
arrD.SetName(f"Data")
dataTable.AddColumn(arrD)
labels = vtk.vtkStringArray()
labels.InsertNextValue(f"Data")
numData = len(data)
dataTable.SetNumberOfRows(numData)
for p in range(numData):
dataTable.SetValue(p, 0, data[p])
quartiles = vtk.vtkComputeQuartiles()
quartiles.SetInputData(dataTable)
quartiles.Update()
plot = chart.GetPlot(0)
plot.SetInputData(quartiles.GetOutput())
plot.SetLabels(labels)
chart.SetColumnVisibilityAll(True)
return chart
def create_pie_chart():
data = [1, 2, 3, 4]
chart = vtk.vtkChartPie()
table = vtk.vtkTable()
arrS = vtk.vtkFloatArray()
arrS.SetName(f"Segments")
table.AddColumn(arrS)
labels = vtk.vtkStringArray()
numPoints = len(data)
table.SetNumberOfRows(numPoints)
for p in range(numPoints):
table.SetValue(p, 0, data[p])
labels.InsertNextValue(f"Segment {p}")
plot = chart.AddPlot(0)
plot.SetInputData(table)
plot.SetInputArray(0, f"Segments")
plot.SetLabels(labels)
return chart
if __name__ == "__main__":
width, height = 400, 400
chart_type = "xy"
renwin = vtk.vtkRenderWindow()
renwin.SetSize(2*width, height)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renwin)
renderer = vtk.vtkRenderer()
renderer.SetBackground([1, 1, 1])
renwin.AddRenderer(renderer)
chart_scene = vtk.vtkContextScene()
chart_actor = vtk.vtkContextActor()
chart_actor.SetScene(chart_scene)
renderer.AddActor(chart_actor)
chart_scene.SetRenderer(renderer)
chart = {
"xy": create_xy_chart,
"parallel": create_parallel_chart,
"2dhist": create_2dhist_chart,
"box": create_box_chart,
"pie": create_pie_chart
}[chart_type]()
chart.SetInteractive(False)
chart.SetAutoSize(False)
chart.SetSize(vtk.vtkRectf(0, 0, width, height))
chart_scene.AddItem(chart)
chart_int = create_pie_chart()
chart_int.SetInteractive(True)
chart_int.SetAutoSize(False)
chart_int.SetSize(vtk.vtkRectf(width, 0, width, height))
chart_scene.AddItem(chart_int)
style = vtk.vtkContextInteractorStyle()
style.SetScene(chart_scene)
iren.SetInteractorStyle(style)
renwin.Render()
iren.Start()