QXlsx icon indicating copy to clipboard operation
QXlsx copied to clipboard

suggestion : Support for X-axis Label Positioning in Charts

Open amg8192 opened this issue 6 months ago • 0 comments

Hey dantti,

While working with Chart objects—specifically LineChart and BarChart types—I came across a scenario where I needed to control the position of the category axis (X-axis) labels (e.g., move them to appear on top of the chart instead of at the bottom).

Currently, the library generates the <c:catAx> element without any <c:lblPos> child tag, which results in Excel defaulting the labels to "nextTo" or "bottom". However, in some cases, such as vertical label arrangements or when charts are embedded in constrained UI areas, it's useful to change the X-axis label position to "t" (top) or "low" (below the axis line).

To support configurable label positioning, I suggest adding a member to the ChartPrivate class to store the desired position (already done in my fork):

QString xAxisLabelPosition = "nextTo"; // default value

Then, in the saveXmlAxisCatAx function (located in xlsxchart.cpp), insert the following XML element right after saveXmlAxisEG_AxShared(writer, axis):

if (!xAxisLabelPosition.isEmpty()) {
    writer.writeStartElement(QStringLiteral("c:tickLblPos"));
    writer.writeAttribute(QStringLiteral("val"), xAxisLabelPosition);
    writer.writeEndElement(); // c:lblPos
}

And finally, expose this feature via a public API in Chart:

void setXAxisLabelPosition(const QString &position);

This function can set d->xAxisLabelPosition in ChartPrivate. Valid values would be:

"nextTo" (default)

"low"

"high"

"none"

in code :

chart->setXAxisLabelPosition("low");  
chart->setXAxisLabelPosition("high");   
chart->setXAxisLabelPosition("nextTo");  
chart->setXAxisLabelPosition("none");

Once added, the generated .xlsx will include:

<c:tickLblPos val="high"/>

inside the <c:catAx> node, which Excel correctly renders with the X-axis labels moved to the top of the chart.

Let me know if you're open to a pull request—I'd be happy to contribute this feature properly with backward compatibility in mind

amg8192 avatar May 18 '25 07:05 amg8192