Other API Samples

Charting Scatter Chart

Demonstrates generating a Scatter Chart where x and y data sets are plotted on a single set of axes.

``````// Create a new workbook and get a reference to the active worksheet, window info,
// and cells.

// Get the chart's source data range and add some sample data.
cells["G2:J30"].Formula = "=INT(RAND() * 500) + 150"; ;

// Add a chart to the worksheet's shape collection.
// NOTE: Calculate the coordinates of the chart by converting row and column
//       coordinates to points.  Use fractional row and column values to get
//       coordinates anywhere in between row and column boundaries.
double left = windowInfo.ColumnToPoints(0.15);
double top = windowInfo.RowToPoints(0.5);
double right = windowInfo.ColumnToPoints(5.85);
double bottom = windowInfo.RowToPoints(13.5);
worksheet.Shapes.AddChart(left, top, right - left, bottom - top).Chart;

// Add a series and link x and y values to the source data range.
series1.XValues = "=G2:G30";
series1.Values = "=H2:H30";

// Add a second series and link x an y values to the source data range.
series2.XValues = "=I2:I30";
series2.Values = "=J2:J30";

// Set the chart type to XYScatter.

// Get a reference to the x value axis, add a title, and link the title to a cell.
xValueAxis.HasTitle = true;
xValueAxis.AxisTitle.Text = "X Value Axis";

// Get a reference to the y value axis, add a title, and link the title to a cell.
yValueAxis.HasTitle = true;
yValueAxis.AxisTitle.Text = "Y Value Axis";

// Delete the major gridlines from the y value axis.
yValueAxis.HasMajorGridlines = false;

// Delete the legend.
chart.HasLegend = false;

// Add a chart title and change the font size.
chart.HasTitle = true;
chart.ChartTitle.Text = "XY Scatter Plot";
chart.ChartTitle.Font.Size = 12;``````
Render Image

Generate an image representation of the results of this sample, which uses the SpreadsheetGear.Drawing.Image class to convert ranges, charts and shapes to images.