Get Started
Documentation

AddChart

AddChart(sDataRange, bInRows, sType, nStyleIndex, nExtX, nExtY, nFromCol, nColOffset, nFromRow, nRowOffset) → {ApiChart}

Create a chart of the set type from the selected data range of the current sheet.

Parameters:

Name Type Description
sDataRange string The selected cell range which will be used to get the data for the chart, formed specifically and including the sheet name.
bInRows bool Specifies whether to take the data from the rows or from the columns. If true the data from the rows will be used.
sType ChartType The chart type used for the chart display.
nStyleIndex number The chart color style index (can be 1 - 48, as described in OOXML specification).
nExtX EMU The chart width in English measure units
nExtY EMU The chart height in English measure units.
nFromCol number The number of the column where the beginning of the chart will be placed.
nColOffset EMU The offset from the nFromCol column to the left part of the chart measured in English measure units.
nFromRow number The number of the row where the beginning of the chart will be placed.
nRowOffset EMU The offset from the nFromRow row to the upper part of the chart measured in English measure units.
Please note, that the horizontal nColOffset and vertical nRowOffset offsets are calculated within the limits of the specified nFromCol column and nFromRow row cell only. If this value exceeds the cell width or height, another vertical/horizontal position will be set.

Returns:

Type
ApiChart

Example

Copy code
builder.CreateFile("xlsx");
var oWorksheet = Api.GetActiveSheet();
oWorksheet.SetName("sheet 1");
oWorksheet.GetRange("B1").SetValue("Row 1");
oWorksheet.GetRange("C1").SetValue("Row 2");
oWorksheet.GetRange("D1").SetValue("Row 3");
oWorksheet.GetRange("A2").SetValue("Category 1");
oWorksheet.GetRange("A3").SetValue("Category 2");
oWorksheet.GetRange("A4").SetValue("Category 3");
oWorksheet.GetRange("A5").SetValue("Category 4");
oWorksheet.GetRange("B2").SetValue("4.3");
oWorksheet.GetRange("B3").SetValue("2.5");
oWorksheet.GetRange("B4").SetValue("3.5");
oWorksheet.GetRange("B5").SetValue("4.5");
oWorksheet.GetRange("C2").SetValue("2.4");
oWorksheet.GetRange("C3").SetValue("4.4");
oWorksheet.GetRange("C4").SetValue("1.8");
oWorksheet.GetRange("C5").SetValue("2.8");
oWorksheet.GetRange("D2").SetValue("2");
oWorksheet.GetRange("D3").SetValue("2");
oWorksheet.GetRange("D4").SetValue("3");
oWorksheet.GetRange("D5").SetValue("5");
var oChart = oWorksheet.AddChart("'sheet 1'!$A$1:$D$5", true, "bar", 2, 100 * 36000, 70 * 36000, 5, 2 * 36000, 1, 3 * 36000);
oChart.SetVerAxisTitle("Vertical Title", 10);
oChart.SetHorAxisTitle("Horizontal Title", 11);
oChart.SetLegendPos("right");
oChart.SetShowDataLabels(false, false, true, false);
oChart.SetTitle("Main Chart Title", 13);
builder.SaveFile("xlsx", "AddChart.xlsx");
builder.CloseFile();

Resulting document