我正在编写一个带有图形的程序,该图形应该在到达特定点时改变颜色。
这是我当前的代码:
self.chart = QtCharts.QChart()
self.chart.setAnimationOptions(QtCharts.QChart.AllAnimations)
self.chartView = QtCharts.QChartView(self.chart)
self.chartView.setRenderHint(QPainter.Antialiasing)
series = QtCharts.QLineSeries()
pen = QtGui.QPen();
pen.setStyle(Qt.SolidLine)
pen.setBrush(QColor(213, 0, 0))
pen2 = QtGui.QPen();
pen2.setStyle(Qt.SolidLine)
pen2.setBrush(QColor(25,255,25))
for x in range(51):
z = x*2
y = pow(x,2)
series.append(z,y)
series.setPen(pen)
if x >=10:
n = x
for st in range(n, 51):
series.setPen(pen2)
self.chart.addSeries(series)
self.main_layout = QVBoxLayout()
self.main_layout.addWidget(self.chartView)
self.setLayout(self.main_layout)
for n in range(11):
x_ax = n*5
self.axis_x = QtCharts.QValueAxis()
self.axis_x.setTickCount(11)
self.axis_x.setRange(0, x_ax)
self.axis_x.setLabelFormat("%.0f")
self.chart.addAxis(self.axis_x, Qt.AlignBottom)
series.attachAxis(self.axis_x)
for n in range(31):
y_ax = n*10
self.axis_y = QtCharts.QValueAxis()
self.axis_y.setTickCount(16)
self.axis_y.setRange(0, y_ax)
self.axis_y.setLabelFormat("%.0f")
self.chart.addAxis(self.axis_y, Qt.AlignLeft)
series.attachAxis(self.axis_y)
问题在于整个图形不仅会改变x上方的颜色,还会改变颜色。我尝试了很多不同的方法,但是没有任何效果。 图应该是这样的:
任何帮助都会很棒。