我有以下代码,其目的是能够根据我们从QColorDialog
获得的数据动态更改线条颜色。如果我直接将颜色应用于vtkActor
并重新渲染窗口,那么它就可以正常工作了。
但最终我希望能够更改单个行的点颜色,这就是为什么我在self.linesPolyData.GetCellData().SetScalars(self.colors)
函数QPushButton
中使用def select_color(self):
更改颜色时设置from Analysis.Channel import *
import vtk
import numpy as np
import itertools
import sys
import math
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QScrollArea, QPushButton, QColorDialog
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
# Qt Class which will render the objects
class MainWindow(QWidget):
def __init__(self, parent = None):
super().__init__()
self.layout = QGridLayout(self)
self.color_button = QPushButton('Select Color')
self.layout.addWidget(self.color_button)
self.color_button.clicked.connect(self.select_color)
self.color = [255, 255, 0, 255]
self.colors = vtk.vtkUnsignedCharArray()
self.colors.SetNumberOfComponents(4)
# Create the polydata where we will store all the geometric data
self.linesPolyData = vtk.vtkPolyData()
# Create three points
origin = [0.0, 0.0, 0.0]
p0 = [1.0, 0.0, 0.0]
p1 = [0.0, 1.0, 0.0]
# Create a vtkPoints container and store the points in it
pts = vtk.vtkPoints()
pts.InsertNextPoint(origin)
pts.InsertNextPoint(p0)
pts.InsertNextPoint(p1)
self.linesPolyData.SetPoints(pts)
# Create the first line (between Origin and P0)
line0 = vtk.vtkLine()
line0.GetPointIds().SetId(0, 0) # the second 0 is the index of the Origin in linesPolyData's points
line0.GetPointIds().SetId(1, 1) # the second 1 is the index of P0 in linesPolyData's points
# Create the second line (between Origin and P1)
line1 = vtk.vtkLine()
line1.GetPointIds().SetId(0, 0) # the second 0 is the index of the Origin in linesPolyData's points
line1.GetPointIds().SetId(1, 2) # 2 is the index of P1 in linesPolyData's points
self.lines = vtk.vtkCellArray()
self.lines.InsertNextCell(line0)
self.lines.InsertNextCell(line1)
# Add the lines to the polydata container
self.linesPolyData.SetLines(self.lines)
for i in range(0, self.lines.GetNumberOfCells()):
self.colors.InsertNextTuple(self.color)
self.linesPolyData.GetCellData().SetScalars(self.colors)
self.mapper = vtk.vtkPolyDataMapper()
self.mapper.SetInputData(self.linesPolyData)
self.actor = vtk.vtkActor()
self.actor.SetMapper(self.mapper)
self.actor.GetProperty().SetLineWidth(4)
self.vtk_widget = QVTKRenderWindowInteractor()
# vtk_widget.setMinimumSize(round(1024/3), round(1024/3))
ren = vtk.vtkRenderer()
self.vtk_widget.GetRenderWindow().AddRenderer(ren)
ren.AddActor(self.actor)
ren.ResetCamera()
interactor = self.vtk_widget.GetRenderWindow().GetInteractor()
interactor.Initialize()
interactor.Start()
self.layout.addWidget(self.vtk_widget)
def select_color(self):
color = QColorDialog.getColor(QtCore.Qt.green, self)
if color.isValid():
self.color = color.getRgb()
for i in range(0, self.lines.GetNumberOfCells()):
self.colors.InsertNextTuple(self.color)
self.linesPolyData.GetCellData().SetScalars(self.colors)
self.mapper.SetInputData(self.linesPolyData)
self.linesPolyData.Modified()
self.mapper.Modified()
# self.actor.GetProperty().Modified()
self.vtk_widget.Render()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
它不会重新用新颜色渲染线条。我做错了什么?
var msftDataTable = anychart.data.table();
msftDataTable.addData(window.get_msft_daily_short_data());
var chart = anychart.stock();
var firstPlot = chart.plot(0);
firstPlot.area(msftDataTable.mapAs({'value': 4})).name('MSFT');
ReactDOM.render(
<AnyChart
width={800}
height={800}
instance={chart}
title="Stock demo"
/>, document.getElementById('root'));