EasyXLS™ library allows you to create Excel charts that can be placed in a separate sheet. A chart can have two positions, as a distinct sheet or as an object in a worksheet. This chapter explains the first situation.
EasyXLS also allows you to read Excel files that contain charts and extract chart information.
// Create an instance of the class that creates Excel files
ExcelDocument workbook = new ExcelDocument();
// Add one sheet
workbook.easy_addWorksheet("SourceData");
// Populate the sheet with data
...
// Add the chart sheet to workbook
workbook.easy_addChart("Chart", "=SourceData!$A$1:$D$5", Chart.SERIES_IN_COLUMNS);
// Create Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel chart.xlsx");
' Create an instance of the class that creates Excel filesDim workbook As New ExcelDocument
' Add one sheet
workbook.easy_addWorksheet("SourceData")
' Populate the sheet with data
...
' Add the chart sheet to workbook
workbook.easy_addChart("Chart", "=SourceData!$A$1:$D$5", Chart.SERIES_IN_COLUMNS)
' Create Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx")
C++// Create an instance of the class that creates Excel files
EasyXLS::IExcelDocumentPtr workbook;
hr = CoCreateInstance(__uuidof(EasyXLS::ExcelDocument),
NULL,
CLSCTX_ALL,
__uuidof(EasyXLS::IExcelDocument),
(void**) &workbook) ;
// Add one sheet
workbook->easy_addWorksheet_2("SourceData");
// Populate the sheet with data
...
// Add the chart sheet to workbook
workbook->easy_addChart_5("Chart", "=SourceData!$A$1:$D$5", CHART_SERIES_IN_COLUMNS);
// Create Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel chart.xlsx");
Click here to see Continuous Code ListingC++.NET// Create an instance of the class that creates Excel files
ExcelDocument ^workbook = gcnew ExcelDocument();
// Add one sheet
workbook->easy_addWorksheet("SourceData");
// Populate the sheet with data
...
// Add the chart sheet to workbook
workbook->easy_addChart("Chart", "=SourceData!$A$1:$D$5", Chart::SERIES_IN_COLUMNS);
// Create Excel file
workbook->easy_WriteXLSXFile("C:\\Samples\\Excel chart.xlsx");
Click here to see Continuous Code Listing
// Create an instance of the class that creates Excel files
ExcelDocument workbook = new ExcelDocument();
// Add one sheet
workbook.easy_addWorksheet("SourceData");
// Populate the sheet with data
...
// Add the chart sheet to workbook
workbook.easy_addChart("Chart", "=SourceData!$A$1:$D$5", Chart.SERIES_IN_COLUMNS);
// Create Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel chart.xlsx");
.NET:// Create an instance of the class that creates Excel files
$workbook = new COM("EasyXLS.ExcelDocument");
// Add one sheet
$workbook->easy_addWorksheet_2("SourceData");
// Populate the sheet with data
...
// Add the chart sheet to workbook
$workbook->easy_addChart_5("Chart",
"=SourceData!\$A$1:\$D$5", $CHART_SERIES_IN_COLUMNS);
// Create Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx");
Click here to see Continuous Code ListingJava:// Create an instance of the class that exports Excel files
$workbook = new java("EasyXLS.ExcelDocument");
// Create an worksheet
$workbook->easy_addWorksheet("SourceData");
// Populate the sheet with data
...
// Add a chart sheet
$workbook->easy_addChart("Chart", "=SourceData!\$A$1:\$D$5", $CHART_SERIES_IN_COLUMNS);
// Export Excel file
$workbook->easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx");
Click here to see Continuous Code Listing
' Create an instance of the class that creates Excel filesset workbook = Server.CreateObject("EasyXLS.ExcelDocument")
' Add one sheet
workbook.easy_addWorksheet_2("SourceData")
' Populate the sheet with data
...
' Add the chart sheet to workbook
workbook.easy_addChart_5 "Chart", "=SourceData!$A$1:$D$5", CHART_SERIES_IN_COLUMNS
' Create Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx")
' Create an instance of the class that creates Excel filesSet workbook = CreateObject("EasyXLS.ExcelDocument")
' Add one sheet
workbook.easy_addWorksheet_2("SourceData")
' Populate the sheet with data
...
' Add the chart sheet to workbook
workbook.easy_addChart_5 "Chart", _
"=SourceData!$A$1:$D$5", Chart.CHART_SERIES_IN_COLUMNS
' Create Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx")
' Create an instance of the class that creates Excel filesset workbook = CreateObject("EasyXLS.ExcelDocument")
' Add one sheet
workbook.easy_addWorksheet_2("SourceData")
' Populate the sheet with data
...
' Add the chart sheet to workbook
workbook.easy_addChart_5 "Chart", "=SourceData!$A$1:$D$5", CHART_SERIES_IN_COLUMNS
' Create Excel file
workbook.easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx")
<!-- Create an instance of the class that creates Excel files --><cfobject type="java"class="EasyXLS.ExcelDocument"name="workbook"action="CREATE"><!-- Add one sheet --><cfset ret = workbook.easy_addWorksheet("SourceData")><!-- Populate the sheet with data -->
...
<!-- Add the chart sheet to workbook --><cfset workbook.easy_addChart("Chart",
"=SourceData!$A$1:$D$5", Chart.CHART_SERIES_IN_COLUMNS)><!-- Create Excel file --><cfset ret = workbook.easy_WriteXLSXFile("C:\Samples\Excel chart.xlsx")>
.NET:# Create an instance of the class that exports Excel files
workbook = ExcelDocument()
# Create an worksheet
workbook.easy_addWorksheet("SourceData")
# Populate the sheet with data
...
# Add the chart sheet to workbook
workbook.easy_addChart("Chart", "=SourceData!$A$1:$D$5", Chart.SERIES_IN_COLUMNS)
# Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel chart.xlsx")
Click here to see Continuous Code ListingJava:# Create an instance of the class that exports Excel files
workbook = gateway.jvm.ExcelDocument()
# Create an worksheet
workbook.easy_addWorksheet("SourceData")
# Populate the sheet with data
...
# Add the chart sheet to workbook
workbook.easy_addChart("Chart", "=SourceData!$A$1:$D$5", gateway.jvm.Chart.SERIES_IN_COLUMNS)
# Export Excel file
workbook.easy_WriteXLSXFile("C:\\Samples\\Excel chart.xlsx")
Click here to see Continuous Code Listing
The screen shot provides an example of an exported column chart that has a legend and the chart is placed in a separate sheet.
Chart type
EasyXLS™ library provides a large number of chart types, such as column chart, bar chart, line chart, pie chart, scatter chart, area chart, doughnut chart, radar chart, surface chart, bubble chart, stock chart, cylinder chart, cone chart and pyramid chart.
A chart data source is composed of two sections: - Category axis labels (X axis) - Series (Y axis)
As presented in the above code sample, the data source can be set when the chart sheet is added to the workbook. Also, ExcelChart.easy_setCategoryXAxisLabels method can be used for setting the labels on the category X axis. The series for Y axis values are added using ExcelChart.easy_addSeries method.
EasyXLS lets you format the chart elements such as chart area, plot area, series, category X axis, value Y axis, gridlines, data table, legend, titles and 3D rotation.