How to ensure square plot area?

Oystein Bjorke 6 years ago 0
This discussion was imported from CodePlex

viklele wrote at 2012-11-20 07:47:


In my application, I need to ensure that the actual plot area where the chart is drawn in always square.

Please advice - sample code welcome.


Warm Regards,

- Vikram

objo wrote at 2012-11-26 06:12:

I think you can change the "PlotMargins" of the plot each time the size of the plot changes...

Or make sure the size of the control is square (handle the sizechanged event on the control or override the coerce value callbacks on the width/height dependency properties?). 

viklele wrote at 2012-11-26 06:42:

Thanks for your reply.


I understand that I need to adjust plot margins in resize event of the control. I am not clear about the calculation. I have adjusted margins using height / width of the control, and height / width of the title area. This still does not make the plot area square. Here is my calculation:

        private void AdjustPoincareChartMargins(Plot plot, double dWidth, double dHeight)
            Debug.WriteLine("Available W: " + dWidth + ", H: " + dHeight);

            plot.Model.IsLegendVisible = false;
            var availableWidth = dWidth;
            var availableHeight = dHeight - plot.Model.TitleArea.Height;

            var horzMargin = Math.Max(0, (availableWidth - availableHeight)) / 2;
            var vertMargin = Math.Max(0, (availableHeight - availableWidth)) / 2;

            plot.Model.PlotMargins = new OxyPlot.OxyThickness(horzMargin, vertMargin, horzMargin, vertMargin);

            Debug.WriteLine("Plot W: " + plot.Model.PlotArea.Width + ", H: " + plot.Model.PlotArea.Height);
            Debug.WriteLine("PlotAndAxisArea W: " + plot.Model.PlotAndAxisArea.Width + ", H: " + plot.Model.PlotAndAxisArea.Height);

- Vikram