|
| 1 | +--- |
| 2 | +title: Work with data labels in charts using the Excel JavaScript API |
| 3 | +description: Code samples demonstrating chart data label tasks using the Excel JavaScript API. |
| 4 | +ms.date: 04/14/2025 |
| 5 | +ms.localizationpriority: medium |
| 6 | +--- |
| 7 | + |
| 8 | +# Work with data labels in charts using the Excel JavaScript API |
| 9 | + |
| 10 | +Add data labels to Excel charts to provide a better visualization experience about important aspects of the chart. To learn more about data labels, see [Add or remove data labels in a chart](https://support.microsoft.com/office/add-or-remove-data-labels-in-a-chart-884bf2f1-2e29-454e-8b42-f467c9f4eb2d). |
| 11 | + |
| 12 | +The following code sample sets up the sample data and **Bicycle Part Production** chart used in this article. |
| 13 | + |
| 14 | +```javascript |
| 15 | +async function setup() { |
| 16 | + await Excel.run(async (context) => { |
| 17 | + context.workbook.worksheets.getItemOrNullObject("Sample").delete(); |
| 18 | + const sheet = context.workbook.worksheets.add("Sample"); |
| 19 | + |
| 20 | + let salesTable = sheet.tables.add("A1:E1", true); |
| 21 | + salesTable.name = "SalesTable"; |
| 22 | + |
| 23 | + salesTable.getHeaderRowRange().values = [["Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"]]; |
| 24 | + |
| 25 | + salesTable.rows.add(null, [ |
| 26 | + ["Frames", 5000, 7000, 6544, 5377], |
| 27 | + ["Saddles", 400, 323, 276, 1451], |
| 28 | + ["Brake levers", 9000, 8766, 8456, 9812], |
| 29 | + ["Chains", 1550, 1088, 692, 2553], |
| 30 | + ["Mirrors", 225, 600, 923, 344], |
| 31 | + ["Spokes", 6005, 7634, 4589, 8765] |
| 32 | + ]); |
| 33 | + |
| 34 | + sheet.activate(); |
| 35 | + createChart(context); |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +async function createChart(context: Excel.RequestContext) { |
| 40 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 41 | + const chart = worksheet.charts.add( |
| 42 | + Excel.ChartType.lineMarkers, |
| 43 | + worksheet.getRange("A1:E7"), |
| 44 | + Excel.ChartSeriesBy.rows |
| 45 | + ); |
| 46 | + chart.axes.categoryAxis.setCategoryNames(worksheet.getRange("B1:E1")); |
| 47 | + chart.name = "PartChart"; |
| 48 | + |
| 49 | + // Place the chart below sample data. |
| 50 | + chart.top = 125; |
| 51 | + chart.left = 5; |
| 52 | + chart.height = 300; |
| 53 | + chart.width = 450; |
| 54 | + |
| 55 | + chart.title.text = "Bicycle Part Production"; |
| 56 | + chart.legend.position = "Bottom"; |
| 57 | + |
| 58 | + await context.sync(); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +This image shows how the chart should display after running the sample code. |
| 63 | + |
| 64 | +:::image type="content" source="../images/excel-data-labels-starter-chart.png" alt-text="Screenshot of basic line chart with no data labels, showing six different bicycle parts being produced over four quarters."::: |
| 65 | + |
| 66 | +## Add data labels |
| 67 | + |
| 68 | +To add data labels to a chart, get the series of data points you want to change, and set the `hasDataLabels` property to `true`. |
| 69 | + |
| 70 | +```javascript |
| 71 | +async function addDataLabels() { |
| 72 | + await Excel.run(async (context) => { |
| 73 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 74 | + const chart = worksheet.charts.getItem("PartChart"); |
| 75 | + await context.sync(); |
| 76 | + |
| 77 | + // Get spokes data series. |
| 78 | + const series = chart.series.getItemAt(5); |
| 79 | + |
| 80 | + // Turn on data labels and set location. |
| 81 | + series.hasDataLabels = true; |
| 82 | + series.dataLabels.position = Excel.ChartDataLabelPosition.top; |
| 83 | + await context.sync(); |
| 84 | + }); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +:::image type="content" source="../images/excel-data-labels-chart.png" alt-text="Screenshot of chart showing data labels that display the amount for each data point."::: |
| 89 | + |
| 90 | +## Format data label size, shape, and text |
| 91 | + |
| 92 | +You can change attributes on data labels using the following APIs. |
| 93 | + |
| 94 | +- Change data label shapes by setting the [geometricShapeType](/javascript/api/excel/excel.chartdatalabel) property. |
| 95 | +- Change height and width using the [setWidth and setHeight](/javascript/api/excel/excel.chartdatalabel) methods. |
| 96 | +- Change the text using the [text](/javascript/api/excel/excel.chartdatalabel) property. |
| 97 | +- Change the text formatting using the [format](/javascript/api/excel/excel.chartdatalabel) property. You can change the [border, fill, and font](/javascript/api/excel/excel.chartdatalabelformat) properties. |
| 98 | + |
| 99 | +The following code sample shows how to set the shape type, height and width, and font formatting for the data labels. |
| 100 | + |
| 101 | +```javascript |
| 102 | + await Excel.run(async (context) => { |
| 103 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 104 | + const chart = worksheet.charts.getItem("PartChart"); |
| 105 | + const series = chart.series.getItemAt(5); |
| 106 | + |
| 107 | + // Set geometric shape of data labels to cubes. |
| 108 | + series.dataLabels.geometricShapeType = Excel.GeometricShapeType.cube; |
| 109 | + series.points.load("count"); |
| 110 | + await context.sync(); |
| 111 | + let pointsLoaded = series.points.count; |
| 112 | + |
| 113 | + // Change height, width, and font size of all data labels. |
| 114 | + for (let j = 0; j < pointsLoaded; j++) { |
| 115 | + series.points.getItemAt(j).dataLabel.setWidth(60); |
| 116 | + series.points.getItemAt(j).dataLabel.setHeight(30); |
| 117 | + series.points.getItemAt(j).dataLabel.format.font.size = 12; |
| 118 | + } |
| 119 | + |
| 120 | + // Set text of a data label. |
| 121 | + series.points.getItemAt(2).dataLabel.setWidth(80); |
| 122 | + series.points.getItemAt(2).dataLabel.setHeight(50); |
| 123 | + series.points.getItemAt(2).dataLabel.text = "Spokes Qtr3: 4589 ↓"; |
| 124 | + |
| 125 | + await context.sync(); |
| 126 | +}); |
| 127 | +``` |
| 128 | + |
| 129 | +In the following screenshot, the chart now includes *count* data labels for the **Spokes** data, with custom text at the third data point. |
| 130 | + |
| 131 | +:::image type="content" source="../images/excel-data-labels-chart-formats.png" alt-text="Screenshot of chart with data labels set to cubes, new size, and custom text in one of the data labels."::: |
| 132 | + |
| 133 | +You can also change the formatting of text in a data label. The following code sample shows how to use the [getSubstring](/javascript/api/excel/excel.chartdatalabel) method to get part of data label text and apply font formatting. |
| 134 | + |
| 135 | +```javascript |
| 136 | +await Excel.run(async (context) => { |
| 137 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 138 | + const chart = worksheet.charts.getItem("PartChart"); |
| 139 | + const series = chart.series.getItemAt(5); |
| 140 | + |
| 141 | + // Get the "Spokes" data label. |
| 142 | + let label = series.points.getItemAt(2).dataLabel; |
| 143 | + label.load(); |
| 144 | + await context.sync(); |
| 145 | + |
| 146 | + // Change border weight of this label. |
| 147 | + label.format.border.weight = 2; |
| 148 | + // Format "Qtr3" as bold and italicized. |
| 149 | + label.getSubstring(7, 4).font.bold = true; |
| 150 | + label.getSubstring(7, 4).font.italic = true; |
| 151 | + // Format "Spokes" as green. |
| 152 | + label.getSubstring(0, 6).font.color = "green"; |
| 153 | + // Format "4589" as red. |
| 154 | + label.getSubstring(12).font.color = "red"; |
| 155 | + // Move label up by 15 points. |
| 156 | + label.top = label.top - 15; |
| 157 | + |
| 158 | + await context.sync(); |
| 159 | + }); |
| 160 | +``` |
| 161 | + |
| 162 | +:::image type="content" source="../images/excel-data-labels-chart-substring.png" alt-text="Screenshot of chart showing data label with Spokes set to green, 4589 set to red, and Qtr3 bold and italicized."::: |
| 163 | + |
| 164 | +## Format leader lines |
| 165 | + |
| 166 | +Leader lines connect data labels to their respective data points and make it easier to see what they refer to in the chart. Turn leader lines on using the [showLeaderLines](/javascript/api/excel/excel.chartseries) property. You can set the format of leader lines with the [leaderLines.format](/javascript/api/excel/excel.chartleaderlines) property. |
| 167 | + |
| 168 | +```javascript |
| 169 | +await Excel.run(async (context) => { |
| 170 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 171 | + const chart = worksheet.charts.getItem("PartChart"); |
| 172 | + const series = chart.series.getItemAt(5); |
| 173 | + |
| 174 | + // Show leader lines. |
| 175 | + series.showLeaderLines = true; |
| 176 | + await context.sync(); |
| 177 | + |
| 178 | + // Format leader lines as dotted orange lines with weight 2. |
| 179 | + series.dataLabels.leaderLines.format.line.lineStyle = Excel.ChartLineStyle.dot; |
| 180 | + series.dataLabels.leaderLines.format.line.color = "orange"; |
| 181 | + series.dataLabels.leaderLines.format.line.weight = 2; |
| 182 | +}); |
| 183 | +``` |
| 184 | + |
| 185 | +:::image type="content" source="../images/excel-data-labels-chart-leaderlines.png" alt-text="Screenshot of chart with orange dotted leader lines connecting data labels to their data points."::: |
| 186 | + |
| 187 | +## Create callouts |
| 188 | + |
| 189 | +A callout is a data label that connects to the data point using a bubble-shaped pointer. A callout has an anchor which can be moved from the data point to other locations on the chart. |
| 190 | + |
| 191 | +The following code sample shows how to change data labels in a series to use [Excel.GeometricShapeType.wedgeRectCallout](/javascript/api/excel/excel.geometricshapetype). Note that leader lines are turned off to avoid showing two indicators to the same data label. |
| 192 | + |
| 193 | +```javascript |
| 194 | + await Excel.run(async (context) => { |
| 195 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 196 | + const chart = worksheet.charts.getItem("PartChart"); |
| 197 | + const series = chart.series.getItemAt(5); |
| 198 | + |
| 199 | + // Change to a wedge rectangle style callout. |
| 200 | + series.dataLabels.geometricShapeType = Excel.GeometricShapeType.wedgeRectCallout; |
| 201 | + series.showLeaderLines = false; |
| 202 | + await context.sync(); |
| 203 | +}); |
| 204 | +``` |
| 205 | + |
| 206 | +:::image type="content" source="../images/excel-data-labels-chart-callout.png" alt-text="Screenshot of chart with data labels formatted as callouts."::: |
| 207 | + |
| 208 | +The following code sample shows how to change the anchor location of a data label. |
| 209 | + |
| 210 | +```javascript |
| 211 | +await Excel.run(async (context) => { |
| 212 | + const worksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 213 | + const chart = worksheet.charts.getItem("PartChart"); |
| 214 | + const series = chart.series.getItemAt(5); |
| 215 | + |
| 216 | + let label = series.points.getItemAt(2).dataLabel; |
| 217 | + let point = series.points.getItemAt(2); |
| 218 | + label.load(); |
| 219 | + await context.sync(); |
| 220 | + |
| 221 | + let anchor = label.getTailAnchor(); |
| 222 | + anchor.load(); |
| 223 | + await context.sync(); |
| 224 | + |
| 225 | + anchor.top = anchor.top - 10; |
| 226 | + anchor.left = 40; |
| 227 | +}); |
| 228 | +``` |
| 229 | + |
| 230 | +This screenshot demonstrates how the anchor of the third data label is adjusted by the preceding code sample. |
| 231 | + |
| 232 | +:::image type="content" source="../images/excel-data-labels-chart-anchor-change.png" alt-text="Screenshot of chart with anchor of Spokes data label moved up and left of the original data point location."::: |
| 233 | + |
| 234 | +## See also |
| 235 | + |
| 236 | +- [Excel JavaScript object model in Office Add-ins](excel-add-ins-core-concepts.md) |
| 237 | +- [Work with charts using the Excel JavaScript API](excel-add-ins-charts.md) |
0 commit comments