Aligning Chart Areas Example for WPF

Aligning Chart Areas Example for WPF

Nevron NOV Chart for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF Chart examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Aligning Chart Areas Example for WPF Description

This example demonstrates how to align different chart areas.


Aligning Chart Areas Example

C# Aligning Chart Areas Example Code for Nevron Chart for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Chart;
using Nevron.Nov.Chart.Tools;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NAligningChartAreasExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			NChartViewWithCommandBars chartViewWithCommandBars = new NChartViewWithCommandBars();
			m_ChartView = chartViewWithCommandBars.View;

			NDockPanel dockPanel = new NDockPanel();
			m_ChartView.Surface.Content = dockPanel;

			NLabel label = new NLabel();
			label.Margins = new NMargins(10);
			label.Font = new NFont(NFontDescriptor.DefaultSansFamilyName, 12);
			label.TextFill = new NColorFill(NColor.Black);
			label.TextAlignment = ENContentAlignment.MiddleCenter;
			label.Text = "Aligning Chart Areas";
			NDockLayout.SetDockArea(label, ENDockArea.Top);
			dockPanel.AddChild(label);

			// configure title
			NStackPanel stackPanel = new NStackPanel();
			stackPanel.UniformHeights = ENUniformSize.Max;
			stackPanel.FillMode = ENStackFillMode.Equal;
			stackPanel.FitMode = ENStackFitMode.Equal;
			NDockLayout.SetDockArea(stackPanel, ENDockArea.Center);
			dockPanel.AddChild(stackPanel);

			NCartesianChart stockPriceChart = new NCartesianChart();
			stockPriceChart.Tag = "Chart 1";

            stockPriceChart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XValueTimelineYLinear);
			stockPriceChart.FitMode = ENCartesianChartFitMode.Stretch;
			stockPriceChart.Margins = new NMargins(10, 0, 10, 10);
			ConfigureInteractivity(stockPriceChart);
			stackPanel.AddChild(stockPriceChart);

            // setup the stock series
            m_StockPrice = new NStockSeries();
			stockPriceChart.Series.Add(m_StockPrice);
			m_StockPrice.Name = "Price";
			m_StockPrice.LegendView.Mode = ENSeriesLegendMode.None;
			m_StockPrice.DataLabelStyle = new NDataLabelStyle(false);
			m_StockPrice.CandleShape = ENCandleShape.Stick;

			m_StockPrice.UpStroke = new NStroke(1, NColor.RoyalBlue);
			m_StockPrice.CandleWidth = 10;
			m_StockPrice.UseXValues = true;
			m_StockPrice.InflateMargins = false;

			// setup the volume chart
			NCartesianChart stockVolumeChart = new NCartesianChart();
            stockVolumeChart.Tag = "Chart 2";

            stockVolumeChart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XValueTimelineYLinear);
			stockVolumeChart.FitMode = ENCartesianChartFitMode.Stretch;
			stockVolumeChart.Margins = new NMargins(10, 0, 10, 10);
			ConfigureInteractivity(stockVolumeChart);
			stackPanel.AddChild(stockVolumeChart);

			// setup the stock volume series
			// setup the volume series
			m_StockVolume = new NAreaSeries();
			stockVolumeChart.Series.Add(m_StockVolume);
			m_StockVolume.Name = "Volume";
			m_StockVolume.DataLabelStyle = new NDataLabelStyle(false);
			m_StockVolume.LegendView.Mode = ENSeriesLegendMode.None;
			m_StockVolume.Fill = new NColorFill(NColor.YellowGreen);
			m_StockVolume.UseXValues = true;
			
			// make sure all axes are synchronized
			stockPriceChart.Axes[ENCartesianAxis.PrimaryX].SynchronizedAxes = new NDomArray(new NNodeRef(stockVolumeChart.Axes[ENCartesianAxis.PrimaryX]));
			stockVolumeChart.Axes[ENCartesianAxis.PrimaryX].SynchronizedAxes = new NDomArray(new NNodeRef(stockPriceChart.Axes[ENCartesianAxis.PrimaryX]));

            // add interlaced stripe for Y axis
            NScaleStrip stripStyle1 = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
            stripStyle1.Interlaced = true;
            stockPriceChart.Axes[ENCartesianAxis.PrimaryY].Scale.Strips.Add(stripStyle1);

            NScaleStrip stripStyle2 = new NScaleStrip(new NColorFill(NColor.Beige), null, true, 0, 0, 1, 1);
            stripStyle2.Interlaced = true;
            stockVolumeChart.Axes[ENCartesianAxis.PrimaryY].Scale.Strips.Add(stripStyle2);

            GenerateData();

			// align the left parts of those charts
			NAlignmentGuidelineCollection guideLines = new NAlignmentGuidelineCollection();

			NAlignmentGuideline guideLine = new NAlignmentGuideline();
			guideLine.ContentSide = ENContentSide.Left;
			guideLine.Targets = new NDomArray(new NNodeRef[] { new NNodeRef(stockPriceChart), new NNodeRef(stockVolumeChart) });

			guideLines.Add(guideLine);

			m_ChartView.Surface.AlignmentGuidelines = guideLines;

			m_ChartView.Document.StyleSheets.ApplyTheme(new NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series));

			return chartViewWithCommandBars;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();
			NUniSizeBoxGroup boxGroup = new NUniSizeBoxGroup(stack);

			return boxGroup;
		}
		public string GetExampleDescription()
		{
			return @"

This example demonstrates how to align different chart areas.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Cartesian Chart", "https://www.nevron.com/docs/openvision/cartesian-chart.html"), ("Chart Types Overview", "https://www.nevron.com/docs/openvision/chart-types-overview.html"), }; } #endregion #region Implementation private void ConfigureInteractivity(NChart chart) { NInteractor interactor = new NInteractor(); NRectangleZoomTool rectangleZoomTool = new NRectangleZoomTool(); rectangleZoomTool.VerticalValueSnapper = new NAxisRulerMinMaxSnapper(); interactor.Add(rectangleZoomTool); NDataPanTool dataPanTool = new NDataPanTool(); dataPanTool.StartMouseButtonEvent = ENMouseButtonEvent.RightButtonDown; dataPanTool.EndMouseButtonEvent = ENMouseButtonEvent.RightButtonUp; interactor.Add(dataPanTool); chart.Interactor = interactor; } private void GenerateData() { double open, high, low, close; m_StockPrice.DataPoints.Clear(); m_StockVolume.DataPoints.Clear(); DateTime dt = DateTime.Now - new TimeSpan(120, 0, 0, 0); double dPrevClose = 100; double dVolume = 15; Random random = new Random(); for (int nIndex = 0; nIndex < 100; nIndex++) { open = dPrevClose; if (dPrevClose < 25 || random.NextDouble() > 0.5) { // upward price change close = open + (2 + (random.NextDouble() * 20)); high = close + (random.NextDouble() * 10); low = open - (random.NextDouble() * 10); } else { // downward price change close = open - (2 + (random.NextDouble() * 20)); high = open + (random.NextDouble() * 10); low = close - (random.NextDouble() * 10); } if (low < 1) { low = 1; } while (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday) { dt = dt.AddDays(1); } // add stock / volume data m_StockPrice.DataPoints.Add(new NStockDataPoint(NDateTimeHelpers.ToOADate(dt), open, close, high, low)); m_StockVolume.DataPoints.Add(new NAreaDataPoint(NDateTimeHelpers.ToOADate(dt), dVolume)); // move forward dVolume += 10 * (0.5 - random.NextDouble()); if (dVolume <= 0) dVolume += 15; dt = dt.AddDays(1); } } #endregion #region Event Handlers #endregion #region Fields private NChartView m_ChartView; private NStockSeries m_StockPrice; private NAreaSeries m_StockVolume; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Aligning Chart Areas Example for WPF Solution

VB.NET Aligning Chart Areas Example Code for Nevron Chart for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Chart
Imports Nevron.Nov.Chart.Tools
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf
Imports System

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NAligningChartAreasExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            Dim chartViewWithCommandBars As NChartViewWithCommandBars = New NChartViewWithCommandBars()
            m_ChartView = chartViewWithCommandBars.View

            Dim dockPanel As NDockPanel = New NDockPanel()
            m_ChartView.Surface.Content = dockPanel

            Dim label As NLabel = New NLabel()
            label.Margins = New NMargins(10)
            label.Font = New NFont(NFontDescriptor.DefaultSansFamilyName, 12)
            label.TextFill = New NColorFill(NColor.Black)
            label.TextAlignment = ENContentAlignment.MiddleCenter
            label.Text = "Aligning Chart Areas"
            NDockLayout.SetDockArea(label, ENDockArea.Top)
            dockPanel.AddChild(label)

            ' configure title
            Dim stackPanel As NStackPanel = New NStackPanel()
            stackPanel.UniformHeights = ENUniformSize.Max
            stackPanel.FillMode = ENStackFillMode.Equal
            stackPanel.FitMode = ENStackFitMode.Equal
            NDockLayout.SetDockArea(stackPanel, ENDockArea.Center)
            dockPanel.AddChild(stackPanel)

            Dim stockPriceChart As NCartesianChart = New NCartesianChart()
            stockPriceChart.Tag = "Chart 1"

            stockPriceChart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XValueTimelineYLinear)
            stockPriceChart.FitMode = ENCartesianChartFitMode.Stretch
            stockPriceChart.Margins = New NMargins(10, 0, 10, 10)
            ConfigureInteractivity(stockPriceChart)
            stackPanel.AddChild(stockPriceChart)

            ' setup the stock series
            m_StockPrice = New NStockSeries()
            stockPriceChart.Series.Add(m_StockPrice)
            m_StockPrice.Name = "Price"
            m_StockPrice.LegendView.Mode = ENSeriesLegendMode.None
            m_StockPrice.DataLabelStyle = New NDataLabelStyle(False)
            m_StockPrice.CandleShape = ENCandleShape.Stick

            m_StockPrice.UpStroke = New NStroke(1, NColor.RoyalBlue)
            m_StockPrice.CandleWidth = 10
            m_StockPrice.UseXValues = True
            m_StockPrice.InflateMargins = False

            ' setup the volume chart
            Dim stockVolumeChart As NCartesianChart = New NCartesianChart()
            stockVolumeChart.Tag = "Chart 2"

            stockVolumeChart.SetPredefinedCartesianAxes(ENPredefinedCartesianAxis.XValueTimelineYLinear)
            stockVolumeChart.FitMode = ENCartesianChartFitMode.Stretch
            stockVolumeChart.Margins = New NMargins(10, 0, 10, 10)
            ConfigureInteractivity(stockVolumeChart)
            stackPanel.AddChild(stockVolumeChart)

            ' setup the stock volume series
            ' setup the volume series
            m_StockVolume = New NAreaSeries()
            stockVolumeChart.Series.Add(m_StockVolume)
            m_StockVolume.Name = "Volume"
            m_StockVolume.DataLabelStyle = New NDataLabelStyle(False)
            m_StockVolume.LegendView.Mode = ENSeriesLegendMode.None
            m_StockVolume.Fill = New NColorFill(NColor.YellowGreen)
            m_StockVolume.UseXValues = True

            ' make sure all axes are synchronized
            stockPriceChart.Axes(ENCartesianAxis.PrimaryX).SynchronizedAxes = New NDomArray(Of NNodeRef)(New NNodeRef(stockVolumeChart.Axes(ENCartesianAxis.PrimaryX)))
            stockVolumeChart.Axes(ENCartesianAxis.PrimaryX).SynchronizedAxes = New NDomArray(Of NNodeRef)(New NNodeRef(stockPriceChart.Axes(ENCartesianAxis.PrimaryX)))

            ' add interlaced stripe for Y axis
            Dim stripStyle1 As NScaleStrip = New NScaleStrip(New NColorFill(NColor.Beige), Nothing, True, 0, 0, 1, 1)
            stripStyle1.Interlaced = True
            stockPriceChart.Axes(ENCartesianAxis.PrimaryY).Scale.Strips.Add(stripStyle1)

            Dim stripStyle2 As NScaleStrip = New NScaleStrip(New NColorFill(NColor.Beige), Nothing, True, 0, 0, 1, 1)
            stripStyle2.Interlaced = True
            stockVolumeChart.Axes(ENCartesianAxis.PrimaryY).Scale.Strips.Add(stripStyle2)

            GenerateData()

            ' align the left parts of those charts
            Dim guideLines As NAlignmentGuidelineCollection = New NAlignmentGuidelineCollection()

            Dim guideLine As NAlignmentGuideline = New NAlignmentGuideline()
            guideLine.ContentSide = ENContentSide.Left
            guideLine.Targets = New NDomArray(Of NNodeRef)(New NNodeRef() {New NNodeRef(stockPriceChart), New NNodeRef(stockVolumeChart)})

            guideLines.Add(guideLine)

            m_ChartView.Surface.AlignmentGuidelines = guideLines

            m_ChartView.Document.StyleSheets.ApplyTheme(New NChartTheme(ENChartPalette.Bright, ENChartPaletteTarget.Series))

            Return chartViewWithCommandBars
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()
            Dim boxGroup As NUniSizeBoxGroup = New NUniSizeBoxGroup(stack)

            Return boxGroup
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to align different chart areas.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Cartesian Chart", "https://www.nevron.com/docs/openvision/cartesian-chart.html"), ("Chart Types Overview", "https://www.nevron.com/docs/openvision/chart-types-overview.html")} End Function #End Region #Region "Implementation" Private Sub ConfigureInteractivity(chart As NChart) Dim interactor As NInteractor = New NInteractor() Dim rectangleZoomTool As NRectangleZoomTool = New NRectangleZoomTool() rectangleZoomTool.VerticalValueSnapper = New NAxisRulerMinMaxSnapper() interactor.Add(rectangleZoomTool) Dim dataPanTool As NDataPanTool = New NDataPanTool() dataPanTool.StartMouseButtonEvent = ENMouseButtonEvent.RightButtonDown dataPanTool.EndMouseButtonEvent = ENMouseButtonEvent.RightButtonUp interactor.Add(dataPanTool) chart.Interactor = interactor End Sub Private Sub GenerateData() Dim open, high, low, close As Double m_StockPrice.DataPoints.Clear() m_StockVolume.DataPoints.Clear() Dim dt As Date = Date.Now - New TimeSpan(120, 0, 0, 0) Dim dPrevClose As Double = 100 Dim dVolume As Double = 15 Dim random As Random = New Random() For nIndex = 0 To 99 open = dPrevClose If dPrevClose < 25 OrElse random.NextDouble() > 0.5 Then ' upward price change close = open + (2 + (random.NextDouble() * 20)) high = close + (random.NextDouble() * 10) low = open - (random.NextDouble() * 10) Else ' downward price change close = open - (2 + (random.NextDouble() * 20)) high = open + (random.NextDouble() * 10) low = close - (random.NextDouble() * 10) End If If low < 1 Then low = 1 End If While dt.DayOfWeek = DayOfWeek.Saturday OrElse dt.DayOfWeek = DayOfWeek.Sunday dt = dt.AddDays(1) End While ' add stock / volume data m_StockPrice.DataPoints.Add(New NStockDataPoint(NDateTimeHelpers.ToOADate(dt), open, close, high, low)) m_StockVolume.DataPoints.Add(New NAreaDataPoint(NDateTimeHelpers.ToOADate(dt), dVolume)) ' move forward dVolume += 10 * (0.5 - random.NextDouble()) If dVolume <= 0 Then dVolume += 15 dt = dt.AddDays(1) Next End Sub #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_ChartView As NChartView Private m_StockPrice As NStockSeries Private m_StockVolume As NAreaSeries #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Aligning Chart Areas Example for WPF Solution

Related Documentation Links