I’ve been working to intergrate a print function for my desktop app program where the users can print a document with the generated graphs added on it. Before I do that, I need to find a way to save the graphs as images. After many attempts, I only managed to print out images of the graphs before the data was to be added, even though I saved these images right after adding the values from a CSV file into the graph. However, I’m having some kind of issue of converting the graphs into images. Here’s what the graph should look like:
The graph before image convertion
When I save the grap as an image, however, the values (the colored bars) are not present. How can I fix this?
I’m using PyQt for my program. Here’swhat I used to create the graph as well as saving and displaying the graph as an image:
`def create_temp_chart(self):
if not self.fileName:
return
low = QtChart.QBarSet(“min”)
high = QtChart.QBarSet(“max”)
ltemp = []
htemp = []
with open(self.fileName) as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
header_found = False
for row in csvreader:
if row[0] == "MinTemp":
header_found = True
continue
if header_found:
ltemp.append(float(row[0]))
htemp.append(float(row[1]))
if len(ltemp) >= 12:
break # Assuming only the first 12 months of data are needed
low.append(ltemp)
high.append(htemp)
series = QtChart.QStackedBarSeries()
series.append(low)
series.append(high)
chart = QtChart.QChart()
chart.addSeries(series)
chart.setTitle("Muestra de Temperatura en Celsius")
chart.setAnimationOptions(QtChart.QChart.AnimationOption.SeriesAnimations)
category = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
axisX = QtChart.QBarCategoryAxis()
axisX.append(category)
axisX.setTitleText("Mes")
chart.addAxis(axisX, Qt.AlignmentFlag.AlignBottom)
axisY = QtChart.QValueAxis()
axisY.setRange(-52, 52)
axisY.setTitleText("Temperatura en Celsius")
chart.addAxis(axisY, Qt.AlignmentFlag.AlignLeft)
series.attachAxis(axisX)
series.attachAxis(axisY)
self.ui.chart_view_temp = QtChart.QChartView(chart)
self.ui.chart_view_temp.setRenderHint(QPainter.RenderHint.Antialiasing)
self.ui.chart_view_temp.chart().setTheme(QtChart.QChart.ChartTheme.ChartThemeDark)
sizePolicy = QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
sizePolicy.setHeightForWidth(self.ui.chart_view_temp.sizePolicy().hasHeightForWidth())
self.ui.chart_view_temp.setSizePolicy(sizePolicy)
self.ui.gridLayout_2.addWidget(self.ui.chart_view_temp)
self.ui.frame_18.setStyleSheet(u"background-color: transparent")
# Save chart as image
self.save_chart_as_image(self.ui.chart_view_temp, "temp_chart.png")
def save_chart_as_image(self, chart_view, file_name):
QApplication.processEvents()
image = QImage(chart_view.size(), QImage.Format.Format_ARGB32)
painter = QPainter(image)
chart_view.render(painter)
painter.end()
image.save(file_name)
# Display the image after saving
self.display_image(file_name)
def display_image(self, file_path):
dialog = QDialog(self)
dialog.setWindowTitle("Chart Image")
layout = QVBoxLayout()
label = QLabel()
pixmap = QPixmap(file_path)
label.setPixmap(pixmap)
layout.addWidget(label)
dialog.setLayout(layout)
dialog.exec_()
`
I’m not sure if I’m missing something here or if it’s the way I’m designing the graphs.