Back to Devexpress

How to: Create a Stock Chart

wpf-120514-controls-and-libraries-spreadsheet-examples-charts-how-to-create-a-stock-chart.md

latest3.7 KB
Original Source

How to: Create a Stock Chart

  • Mar 16, 2023
  • 10 minutes to read

This topic demonstrates how to create stock charts using the continuous and noncontiguous cell ranges as data sources.

Use a Continuous Source Range to Create a Stock Chart

Depending on the stock chart type, arrange columns or rows in the source range in the same order as the stock chart name:

  • High-Low-Close
  • Open-High-Low-Close
  • Volume-High-Low-Close
  • Volume-Open-High-Low-Close

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.

View Example

csharp
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");
vb
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")

Use a Noncontiguous Source Range to Create a Stock Chart

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:

See Also

How to: Create and Modify a Chart

How to: Change the Display of Chart Axes