Controlling the size and location of charts, legends etc in WPF


https://www.nevron.com/Forum/Topic14260.aspx
Print Topic | Close Window

By Richard Eustace - 3 Years Ago
I have been going around in circles trying to size charts within panels using WPF!  There is a general lack of proper examples and documentation.
I have a chart control in a window
<Wpf:NChartControl x:Name="nChartControl1" Margin="5,0,0,5" Width="800" Height="500" HorizontalAlignment="Left" VerticalAlignment="Top"/>
I have a CartesianChart and a legend within that.  So, for example, how do I locate the legend on the right hand side half way up centered around the vertical mid point of the control.  There should be fixed gap between the right hand edge of the legend and the right hand edge of the chartcontrol.  The chart should be 50% of the height of the chart control and 80% of the width, again located around midpoint of the height but a fixed distance from the lefthand edge of the control? 
Thanks
Rich
By Nevron Support - 3 Years Ago
Hi Richard,

The following code configures the layout so that both the chart and the legend are centered vertically and at fixed offset from the sides of the control:

NChart chart = nChartControl1.Charts[0];
NLegend legend = nChartControl1.Legends[0];

// at fixed distance from the left side (20pt), but centered relative to height
chart.ContentAlignment = ContentAlignment.MiddleRight;
chart.Location = new NPointL(new NLength(20), new NLength(50, NRelativeUnit.ParentPercentage));

    // 50% of the height of the chart control and 80
    chart.Size = new NSizeL(new NLength(80, NRelativeUnit.ParentPercentage), new NLength(50, NRelativeUnit.ParentPercentage));

    // at fixed distance from the right side (20pt) centered relative to height
    legend.ContentAlignment = ContentAlignment.MiddleLeft;
    legend.Location = new NPointL(new NLength(100, NRelativeUnit.ParentPercentage), new NLength(50, NRelativeUnit.ParentPercentage));
    legend.Margins = new NMarginsL(new NLength(0), new NLength(0), new NLength(20), new NLength(0));

We hope this helps - let us know if you meet any problems or have any questions.
By Richard Eustace - 3 Years Ago
Thanks.  So a couple of questions.
If you want to align to the left edge do you use contentalignment = middleright?
Also, I have this code
  nChartControl1.Charts.Clear()
   nChartControl1.Legends.Clear()
   Dim chart As NCartesianChart = New NCartesianChart
   Dim legend As NLegend = New NLegend

   nChartControl1.Charts.Add(chart)
   nChartControl1.Panels.Add(legend)
   chart.DisplayOnLegend = legend

    legend.ContentAlignment = System.Drawing.ContentAlignment.MiddleLeft
   legend.Location = New NPointL(New NLength(100, NRelativeUnit.ParentPercentage), New NLength(50, NRelativeUnit.ParentPercentage))
   legend.Margins = New NMarginsL(New NLength(0), New NLength(0), New NLength(10), New NLength(0))

    chart.ContentAlignment = System.Drawing.ContentAlignment.MiddleRight
   chart.Size = New NSizeL(New NLength(100, NRelativeUnit.ParentPercentage), New NLength(50, NRelativeUnit.ParentPercentage))
   chart.Location = New NPointL(New NLength(0), New NLength(50, NRelativeUnit.ParentPercentage))

The legend is where I expect it but the chart should be the full width of the window starting from the left but it isn't.  Why is that?


Thanks Rich
By Richard Eustace - 3 Years Ago
If I reduce the size of the chart in the x direction then it gradually moves it into the correct position.  There is clearly some auto sizing and positioning going on.  Can that be disabled?  Or maybe it is an auto aspect ratio fix.  Not sure.  I hope you can help.
Rich
By Richard Eustace - 3 Years Ago
If I reduce the size of the chart it moves to the right place.  Is there some auto sizing or aspect ratio control that is still active?  Or is it WPF related?
thanks
Rich
By Nevron Support - 3 Years Ago
Hi Rich,

Yes, it's the aspect control that you need to turn off - just set:

chart.BoundsMode = BoundsMode.Stretch;

and this should fix the problem. The following WinForm example shows how to control the aspect/stretch:
Panels \ Chart \ Aspect \ Aspect 2D.

Regarding middle right - yes, that is the correct setting - it means right relative to the specified Location  check out the following topic:
http://helpdotnetvision.nevron.com/#UsersGuide_Layout_Content_Panels.html
It shows how ContentAlignment aligns the panel relative to the location.

We hope this helps – let us know if you meet any problems or have any questions.