wpf-120514-controls-and-libraries-spreadsheet-examples-charts-how-to-create-a-stock-chart.md
This topic demonstrates how to create stock charts using the continuous and noncontiguous cell ranges as data sources.
Depending on the stock chart type, arrange columns or rows in the source range in the same order as the stock chart name:
For example, to create an Open-High-Low-Close stock chart, organize columns containing the stock data in the following order: open, high, low and close (see the image below).
After you arrange data in the source range, call the ChartCollection.Add method and pass the specified range as a parameter to create a chart. Refer to the How to: Create and Modify a Chart example for details on how to create a chart in code and adjust its settings.
Worksheet worksheet = workbook.Worksheets["chartStock"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.StockOpenHighLowClose, worksheet["B2:F7"]);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N15"];
// Display the chart title.
chart.Title.Visible = true;
chart.Title.SetValue("NASDAQ:MSFT");
// Hide the legend.
chart.Legend.Visible = false;
// Access the value axis by its index in the primary axis collection.
Axis axis = chart.PrimaryAxes[1];
// Add a title to the value axis.
axis.Title.Visible = true;
axis.Title.SetValue("Price in USD");
Dim worksheet As Worksheet = workbook.Worksheets("chartStock")
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a chart and specify its location.
Dim chart As Chart = worksheet.Charts.Add(ChartType.StockOpenHighLowClose, worksheet("B2:F7"))
chart.TopLeftCell = worksheet.Cells("H2")
chart.BottomRightCell = worksheet.Cells("N15")
' Display the chart title.
chart.Title.Visible = True
chart.Title.SetValue("NASDAQ:MSFT")
' Hide the legend.
chart.Legend.Visible = False
' Access the value axis by its index in the primary axis collection.
Dim axis As Axis = chart.PrimaryAxes(1)
' Add a title to the value axis.
axis.Title.Visible = True
axis.Title.SetValue("Price in USD")
You can use the SeriesCollection.Add method to add a separate data series to a chart if a stock chart’s data is in non-adjacent columns. The number and order of series depend on the stock chart type:
High-Low-Close Stock Chart
Open-High-Low-Close Stock Chart
Volume-High-Low-Close Stock Chart
Volume-Open-High-Low-Close Stock Chart
See Also