Skip to content

Creating an Axis Title #502

Closed
Closed

Description

@JakeZw

I have created a scatter chart, and am working to put Axis Titles on it. I have not been able to figure out how to do this. I think a tutorial showing this would be useful in the documentation. I have created an example, where I need help on the setAxisTitleText command. I appreciate your help

`filename = "test.xlsx"
sheetname = "cougar"
column_labels = ["Diameter [mm]", "Vector1", "Vector2", "Vector3", "Vector4"]
dia = [10, 20, 30, 40, 50]
Vector1 = [1, 2, 3, 4, 5]
Vector2 = sqrt.(Vector1)
Vector3 = log.(Vector1)
Vector4 = Vector1 .+ Vector2

table = [dia, Vector1, Vector2, Vector3, Vector4]
nr = length(dia)
xf = XLSX.openxlsx(filename, mode="w")
XLSX.addsheet!(xf, sheetname)
sheet = xf[sheetname]

write the tabular data

XLSX.writetable!(sheet, table, column_labels)
XLSX.setFormat(sheet, "B2:E$(nr+1)"; format="Number")
XLSX.setColumnWidth(sheet, 2:3, width=16)

make a line chart (dia vs each vector)

c = XLSX.Charts.addChart(sheet, :line; anchor="G1:O25", title="Chart Title")
XLSX.Charts.addSeries(c, "B2:B$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[2])
XLSX.Charts.addSeries(c, "C2:C$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[3])
XLSX.Charts.addSeries(c, "D2:D$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[4])
XLSX.Charts.addSeries(c, "E2:E$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[5])
XLSX.Charts.getChartAxis(cn, 1)
XLSX.Charts.setAxisTitleText(c, :x, "X Label")
XLSX.Charts.setAxisTitleText(c, :y, "Y Value")

XLSX.savexlsx(xf)
`

Activity

  1. TimG1964 commented on Nov 14, 2026

    @TimG1964
    Collaborator

    Thanks for the report, and for including your script. It shows the problem clearly.

    With the current release (v0.13.4), setAxisTitleText needs the axis object rather than :x or :y. For a line chart:

    xax = only(XLSX.Charts.getChartAxes(c, :category))   # the horizontal axis
    yax = only(XLSX.Charts.getChartAxes(c, :value))      # the vertical axis
    
    XLSX.Charts.setAxisTitleText(c, xax, "X Label")
    XLSX.Charts.setAxisTitleText(c, yax, "Y Value")

    The getChartAxis(cn, 1) line didn't work for two reasons. cn should be c. And the number getChartAxis takes is Excel's internal axis ID (usually a large number like 500000001), not the axis's position.

    In the next release your original code will work as you wrote it. Axes can be named by where they're drawn:

    XLSX.Charts.setAxisTitleText(c, :x, "X Label")
    XLSX.Charts.setAxisTitleText(c, :y, "Y Value")

    You can also set the titles when you create the chart:

    c = XLSX.Charts.addChart(sheet, :line; anchor = "G1:O25", title = "Chart Title",
                             xtitle = "X Label", ytitle = "Y Value")

    :x and :y are the axes as Excel draws them, so on a horizontal :bar chart :y is the category axis. :x2 and :y2 are the secondary axes of a combo chart. Every function that takes an axis will accept these names, so setAxisTitleTextProp(c, :y, :size, 12) and setAxisMin(c, :y, 0) will work too.

    Calling getChartAxis(c, 1) gives an error that explains the axis ID. The Creating charts and Formatting charts pages of the docs show how to set axis titles.

    I will try to push this to Master soon and will update here when I do.

  2. added a commit that references this issue on Nov 15, 2026
  3. TimG1964 commented on Nov 15, 2026

    @TimG1964
    Collaborator

    This is now on Master if you want to try. There is a brief note on using xtitle and ytitle in the docs, too, which I hope is sufficient.

  4. JakeZw commented on Nov 18, 2026

    @JakeZw
    Author

    Thank you for your timely response. I really appreciate that.

    I changed the addChart line to be
    c = XLSX.Charts.addChart(sheet, :line; anchor="G1:O25", title="Chart Title", xtitle="X Label", ytitle="Y Value")
    and did not execute the getChartAxis or setAxisTitleText commands.

    I notice that the font style for the x and y axis is different from the Chart Title and different from the Excel default. I expect I can change that but I think it would be more convenient if they were the Excel default font and size and boldness.

  5. JakeZw commented on Nov 18, 2026

    @JakeZw
    Author

    Another quick test using

    c = XLSX.Charts.addChart(sheet, :line; anchor="G1:O25", title="Chart Title") XLSX.Charts.addSeries(c, "B2:B$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[2]) XLSX.Charts.addSeries(c, "C2:C$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[3]) XLSX.Charts.addSeries(c, "D2:D$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[4]) XLSX.Charts.addSeries(c, "E2:E$(nr+1)", categories="A2:A$(nr+1)", name=column_labels[5]) XLSX.Charts.setAxisTitleText(c, :x, "X Label") XLSX.Charts.setAxisTitleText(c, :y, "Y Value")

    shows the same behaviour.

    Sorry this shows poorly in Preview mode, it is fine in Write mode.

  6. TimG1964 commented on Nov 18, 2026

    @TimG1964
    Collaborator

    Thanks for testing this, and you're right. A new axis title had no formatting of its own, so Excel fell back to 18pt black text, or 10pt bold when the title was bound to a cell. The same thing happened when a chart title was retyped with setChartTitleText.

    On the next push to master, new axis titles look the same as ones you add in Excel: 10pt grey in the body font, and the vertical axis's title turned to read upwards. Changing a title's text now keeps its formatting, so a size or colour set earlier with setChartTitleTextProp / setAxisTitleTextProp stays. Your original script needs no changes.

    I'll update here when it's on master.

  7. TimG1964 commented on Nov 18, 2026

    @TimG1964
    Collaborator

    Should be on Master now.

  8. JakeZw commented on Nov 18, 2026

    @JakeZw
    Author

    It is working well, thank you for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions