| xlsgen > overview > Charts |
Here are a few code samples to create native Excel charts. The first example shows how little code you need in order to create a chart. 3 lines of source code. The second example dives deeper into details. I am using C++ code, but you can use any of the languages supported by xlsgen, including VB, all .NET languages and Java.

A barebone chart, leaving all automatic settings untouched
// A barebone chart, leaving all automatic settings untouched
xlsgen::IXlsChartPtr chart = wksht->NewChart(xlsgen::charttype_bar2D,
8, //row1
1, //col1
16, //row2
5 //col2
);
chart->DataSource->SeriesInRows = TRUE;
chart->DataSource->Range = L"Sheet1!R1C2:R3C6";

A 3D chart, quite formatted
// A 3D chart, quite formatted
xlsgen::IXlsChartPtr chart = wksht->NewChart(xlsgen::charttype_area3D,
18, //row1
1, //col1
32, //row2
8 //col2
);
chart->DataSource->SeriesInRows = TRUE;
chart->DataSource->Range = L"Sheet1!R1C2:R3C6";
chart->Legend->Show = xlsgen::chartlegend_hidden;
// add, position and format a title
chart->MainTitle->Label = L"a sample 3D chart";
chart->MainTitle->Options->X = 80;
chart->MainTitle->Options->Y = 5;
chart->MainTitle->Options->Patterns->Borders->Shadow = TRUE;
chart->MainTitle->Options->Patterns->Area->Type = xlsgen::chartareatype_automatic;
// format the background chart area
chart->SurfaceArea->Options->Patterns->Borders->RoundCorners = TRUE;
chart->SurfaceArea->Options->Patterns->Borders->Type = xlsgen::chartbordertype_custom;
chart->SurfaceArea->Options->Patterns->Borders->Style = xlsgen::chartborderstyle_dashdot;
chart->SurfaceArea->Options->Patterns->Borders->Weight = xlsgen::chartborderweight_double;
chart->SurfaceArea->Options->Patterns->Borders->Color = xlsgen::colorRed;
chart->SurfaceArea->Options->Patterns->Area->Type = xlsgen::chartareatype_custom;
chart->SurfaceArea->Options->Patterns->Area->Gradient->SingleColor->GradientStyle =
xlsgen::gradientstyle_verticall0;
chart->SurfaceArea->Options->Patterns->Area->Gradient->SingleColor->Color =
0xDD8800;
chart->SurfaceArea->Options->Patterns->Area->Gradient->SingleColor->LightThreshold =
100;
// format the first series with a gradient
chart->SeriesByIndex[1]->Options->Patterns->Borders->Type = xlsgen::chartbordertype_none;
chart->SeriesByIndex[1]->Options->Patterns->Area->Type =
xlsgen::chartareatype_custom;
chart->SeriesByIndex[1]->Options->Patterns->Area->Gradient->SingleColor->GradientStyle =
xlsgen::gradientstyle_diagonalUp0;
chart->SeriesByIndex[1]->Options->Patterns->Area->Gradient->SingleColor->Color =
0x0000DD;
chart->SeriesByIndex[1]->Options->Patterns->Area->Gradient->SingleColor->LightThreshold =
100;
// format the second series with a texture
chart->SeriesByIndex[2]->Options->Patterns->Area->Type =
xlsgen::chartareatype_custom;
chart->SeriesByIndex[2]->Options->Patterns->Area->PredefinedTexture->TextureStyle =
xlsgen::charttexture_WhiteMarble;
// format the Z axis, and change the ticks
xlsgen::IXlsChartAxisPtr y_axis = chart->YAxis[xlsgen::chartaxis_primary];
y_axis->Options->Font->Italic = TRUE;
y_axis->Options->Font->Bold = TRUE;
y_axis->Options->Font->Color = 0x00DD0000;
y_axis->MinorTicks = xlsgen::chartaxisticks_cross;
And this just scratches the surface of things. There are many objects to program against like data labels, data points, gridlines, and the learning curve is greatly reduced thanks to a composition metaphore used across the chart object model.
To see more in-depth how to manipulate charts with code, you may want to take a look at one of the following pages :
In addition to being generated, charts are rendered if you do a print, preview, or generate a PDF document. See here for more information.
xlsgen documentation. © ARsT Design all rights reserved.