Skip to content

Commit

Permalink
improved pie label positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-rodriguez committed Feb 5, 2017
1 parent 28ee5a2 commit 952586e
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 12 deletions.
1 change: 1 addition & 0 deletions Core/Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="LogarithmicAxisCore.cs" />
<Compile Include="Maps\MapData.cs" />
<Compile Include="PanningOptions.cs" />
<Compile Include="PieLabelPosition.cs" />
<Compile Include="PointTracker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScrollMode.cs" />
Expand Down
39 changes: 39 additions & 0 deletions Core/PieLabelPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//The MIT License(MIT)

//copyright(c) 2016 Alberto Rodriguez

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

namespace LiveCharts
{
/// <summary>
/// Describes where a label should be placed
/// </summary>
public enum PieLabelPosition
{
/// <summary>
/// Places the label inside the pie slice
/// </summary>
InsideSlice,
/// <summary>
/// Places the label outside the pie slice
/// </summary>
OutsideSlice
}
}
1 change: 1 addition & 0 deletions Core40/Core40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="LogarithmicAxisCore.cs" />
<Compile Include="Maps\MapData.cs" />
<Compile Include="PanningOptions.cs" />
<Compile Include="PieLabelPosition.cs" />
<Compile Include="PointTracker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScrollMode.cs" />
Expand Down
39 changes: 39 additions & 0 deletions Core40/PieLabelPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//The MIT License(MIT)

//copyright(c) 2016 Alberto Rodriguez

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

namespace LiveCharts
{
/// <summary>
/// Describes where a label should be placed
/// </summary>
public enum PieLabelPosition
{
/// <summary>
/// Places the label inside the pie slice
/// </summary>
InsideSlice,
/// <summary>
/// Places the label outside the pie slice
/// </summary>
OutsideSlice
}
}
19 changes: 19 additions & 0 deletions WpfView/PieSeries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ public double PushOut
get { return (double) GetValue(PushOutProperty); }
set { SetValue(PushOutProperty, value); }
}

/// <summary>
/// The label position property
/// </summary>
public static readonly DependencyProperty LabelPositionProperty = DependencyProperty.Register(
"LabelPosition", typeof(PieLabelPosition), typeof(PieSeries),
new PropertyMetadata(PieLabelPosition.InsideSlice, CallChartUpdater()));
/// <summary>
/// Gets or sets the label position.
/// </summary>
/// <value>
/// The label position.
/// </value>
public PieLabelPosition LabelPosition
{
get { return (PieLabelPosition) GetValue(LabelPositionProperty); }
set { SetValue(LabelPositionProperty, value); }
}

#endregion

#region Overridden Methods
Expand Down
36 changes: 24 additions & 12 deletions WpfView/Points/PiePointView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public override void DrawOrMove(ChartPoint previousDrawn, ChartPoint current, in

if (DataLabel != null && double.IsNaN(Canvas.GetLeft(DataLabel)))
{
Canvas.SetTop(DataLabel, 0d);
Canvas.SetLeft(DataLabel, 0d);
Canvas.SetTop(DataLabel, chart.DrawMargin.Height/2);
Canvas.SetLeft(DataLabel, chart.DrawMargin.Width/2);
}

if (HoverShape != null)
Expand All @@ -68,25 +68,31 @@ public override void DrawOrMove(ChartPoint previousDrawn, ChartPoint current, in
hs.Radius = Radius;
}

Canvas.SetTop(Slice, chart.DrawMargin.Height / 2);
Canvas.SetLeft(Slice, chart.DrawMargin.Width / 2);
Slice.InnerRadius = InnerRadius;
Slice.Radius = Radius;
var lh = 0d;
if (DataLabel != null)
{
DataLabel.UpdateLayout();
lh = DataLabel.ActualHeight;
}

var hypo = (Slice.Radius + Slice.InnerRadius)/2;
var hypo = ((PieSeries) current.SeriesView).LabelPosition == PieLabelPosition.InsideSlice
? (Radius + InnerRadius)*(Math.Abs(InnerRadius) < 0.01 ? .65 : .5)
: Radius + lh;
var gamma = current.Participation*360/2 + Rotation;
var cp = new Point(hypo * Math.Sin(gamma * (Math.PI / 180)), hypo * Math.Cos(gamma * (Math.PI / 180)));

if (chart.View.DisableAnimations)
{
Slice.InnerRadius = InnerRadius;
Slice.Radius = Radius;
Slice.WedgeAngle = Wedge;
Slice.RotationAngle = Rotation;
Canvas.SetTop(Slice, chart.DrawMargin.Height / 2);
Canvas.SetLeft(Slice, chart.DrawMargin.Width / 2);

if (DataLabel != null)
{
DataLabel.UpdateLayout();

var lx = cp.X + chart.DrawMargin.Width / 2 - DataLabel.ActualWidth * .5;
var lx = cp.X + chart.DrawMargin.Width/2 - DataLabel.ActualWidth * .5;
var ly = chart.DrawMargin.Height/2 - cp.Y - DataLabel.ActualHeight*.5;

Canvas.SetLeft(DataLabel, lx);
Expand All @@ -102,13 +108,19 @@ public override void DrawOrMove(ChartPoint previousDrawn, ChartPoint current, in
{
DataLabel.UpdateLayout();

var lx = cp.X + chart.DrawMargin.Width / 2 - DataLabel.ActualWidth * .5;
var ly = chart.DrawMargin.Height / 2 - cp.Y - DataLabel.ActualHeight * .5;
var lx = cp.X + chart.DrawMargin.Width/2 - DataLabel.ActualWidth * .5;
var ly = chart.DrawMargin.Height/2 - cp.Y - DataLabel.ActualHeight * .5;

DataLabel.BeginAnimation(Canvas.LeftProperty, new DoubleAnimation(lx, animSpeed));
DataLabel.BeginAnimation(Canvas.TopProperty, new DoubleAnimation(ly, animSpeed));
}

Slice.BeginAnimation(Canvas.LeftProperty,
new DoubleAnimation(chart.DrawMargin.Width / 2, animSpeed));
Slice.BeginAnimation(Canvas.TopProperty,
new DoubleAnimation(chart.DrawMargin.Height/2, animSpeed));
Slice.BeginAnimation(PieSlice.InnerRadiusProperty, new DoubleAnimation(InnerRadius, animSpeed));
Slice.BeginAnimation(PieSlice.RadiusProperty, new DoubleAnimation(Radius, animSpeed));
Slice.BeginAnimation(PieSlice.WedgeAngleProperty, new DoubleAnimation(Wedge, animSpeed));
Slice.BeginAnimation(PieSlice.RotationAngleProperty, new DoubleAnimation(Rotation, animSpeed));
}
Expand Down

0 comments on commit 952586e

Please sign in to comment.