I am re writing a graphing / diagram code library I wrote in WPF to be consistent with the MVVM design pattern. I currently have a user control which has a canvas inside it. I calculate the positions / size of the elements which need to be drawn to it in the code behind so there is quite alot of code in it. I believe most of the logic that calculates the properties of the graph should be moved out of the code behind and into the ViewModel but i am a bit unsure if I should move the code that actually creates the shapes since it is actually regarding the View.
Here is an example method in the code behind used to draw the labels of each segment in a pie chart:
private void drawSegmentLabel(CatagoricalDataPoint dataPoint, double totalAngle, double segmentAngle)
{
//Calculate the X and Y coordinates of the label
double x = centerPoint.X + (radius + 20) * Math.Cos(totalAngle + segmentAngle / 2);
double y = centerPoint.Y + (radius + 20) * Math.Sin(totalAngle + segmentAngle / 2);
//Create the Textblock that represents the percentage of the segment
TextBlock segmentLabel = new TextBlock();
segmentLabel.Text = Math.Round(((double)dataPoint.value / data.TotalValue) * 100).ToString() + "%";
segmentLabel.FontFamily = new FontFamily("Calibri Light");
segmentLabel.FontWeight = FontWeights.Bold;
segmentLabel.FontSize = 16;
segmentLabel.Width = 60;
segmentLabel.Tag = dataPoint;
segmentLabel.TextAlignment = TextAlignment.Center;
segmentLabel.Foreground = new SolidColorBrush(Colors.Black);
//If the label is on the right side of the center then align it to the left and vica versa
if (x >= centerPoint.X)
{
Canvas.SetLeft(segmentLabel, x);
segmentLabel.TextAlignment = TextAlignment.Left;
}
else
{
Canvas.SetLeft(segmentLabel, x - segmentLabel.Width);
segmentLabel.TextAlignment = TextAlignment.Right;
}
Canvas.SetTop(segmentLabel, y - 10);
GraphCanvas.Children.Add(segmentLabel);
}
As you can see there are some lines calculating positions and some lines creating the visual elements. Should these lines be in separate files?
Most of the shapes I am drawing are in the system.windows.media namespace. rather then controls such as the textblock in the example above.