新增加 利润统计Window。
隐藏利润统计按钮 增加 Common 文件夹 内部是WPF公用类库
This commit is contained in:
1 parent
4a7af051b2
commit
7c6f91f207
25 files changed
+6462
-3554
No files matched your search
@@ -0,0 +1,17 @@
|
||||
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
<Style TargetType="Ellipse" x:Key="LineEllipse">
|
||||
<Setter Property="Width" Value="7"></Setter>
|
||||
<Setter Property="Height" Value="7"></Setter>
|
||||
<Setter Property="Fill" Value="White"></Setter>
|
||||
<Setter Property="Stroke" Value="#1677ff"></Setter>
|
||||
<Setter Property="StrokeThickness" Value="2"></Setter>
|
||||
<Setter Property="ToolTipService.InitialShowDelay" Value="50"></Setter>
|
||||
<Setter Property="ToolTipService.BetweenShowDelay" Value="0" />
|
||||
<Style.Triggers>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter Property="Fill" Value="White" />
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
</ResourceDictionary>
|
||||
@@ -0,0 +1,9 @@
|
||||
<UserControl x:Class="_29WxOrderWpf.Ctrls.Statistics.Charts.RevenueLineChart"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="320" d:DesignWidth="640">
|
||||
<Canvas x:Name="LayoutRoot" ClipToBounds="True"/>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,242 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
using _29WxOrderWpf.Ctrls.Statistics.Models;
|
||||
|
||||
namespace _29WxOrderWpf.Ctrls.Statistics.Charts
|
||||
{
|
||||
/// <summary>
|
||||
/// 销售趋势折线图(轻量自绘,无第三方依赖)。
|
||||
/// 绑定 <see cref="Points"/>(IList<RevenueTrendPoint>)即可,数据变化时自动重绘。
|
||||
/// 鼠标悬停在数据点圆圈上时显示该点数据(日期 / 销售额 / 订单数)。
|
||||
/// </summary>
|
||||
public partial class RevenueLineChart : UserControl
|
||||
{
|
||||
public static readonly DependencyProperty PointsProperty =
|
||||
DependencyProperty.Register("Points", typeof(IList<RevenueTrendPoint>), typeof(RevenueLineChart),
|
||||
new PropertyMetadata(null, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>趋势数据点</summary>
|
||||
public IList<RevenueTrendPoint> Points
|
||||
{
|
||||
get { return (IList<RevenueTrendPoint>)GetValue(PointsProperty); }
|
||||
set { SetValue(PointsProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty LineBrushProperty =
|
||||
DependencyProperty.Register("LineBrush", typeof(Brush), typeof(RevenueLineChart),
|
||||
new PropertyMetadata(new SolidColorBrush(Color.FromRgb(22, 119, 255)), OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>折线颜色</summary>
|
||||
public Brush LineBrush
|
||||
{
|
||||
get { return (Brush)GetValue(LineBrushProperty); }
|
||||
set { SetValue(LineBrushProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty LineThicknessProperty =
|
||||
DependencyProperty.Register("LineThickness", typeof(double), typeof(RevenueLineChart),
|
||||
new PropertyMetadata(2d, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>折线粗细</summary>
|
||||
public double LineThickness
|
||||
{
|
||||
get { return (double)GetValue(LineThicknessProperty); }
|
||||
set { SetValue(LineThicknessProperty, value); }
|
||||
}
|
||||
|
||||
public RevenueLineChart()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += delegate { Render(); };
|
||||
SizeChanged += delegate { Render(); };
|
||||
}
|
||||
|
||||
private static void OnRenderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
((RevenueLineChart)d).Render();
|
||||
}
|
||||
|
||||
private void Render()
|
||||
{
|
||||
LayoutRoot.Children.Clear();
|
||||
var points = Points;
|
||||
if (points == null || points.Count == 0 || ActualWidth <= 20 || ActualHeight <= 20)
|
||||
return;
|
||||
|
||||
const double marginLeft = 52;
|
||||
const double marginRight = 16;
|
||||
const double marginTop = 18;
|
||||
const double marginBottom = 30;
|
||||
|
||||
double plotWidth = Math.Max(10, ActualWidth - marginLeft - marginRight);
|
||||
double plotHeight = Math.Max(10, ActualHeight - marginTop - marginBottom);
|
||||
|
||||
var values = points.Select(p => (double)p.SalesAmount).ToList();
|
||||
double maxValue = values.Max();
|
||||
if (maxValue <= 0) maxValue = 1;
|
||||
double yMax = maxValue * 1.15;
|
||||
|
||||
var lineBrush = LineBrush ?? new SolidColorBrush(Color.FromRgb(22, 119, 255));
|
||||
Color lineColor = lineBrush is SolidColorBrush ? ((SolidColorBrush)lineBrush).Color : Color.FromRgb(22, 119, 255);
|
||||
|
||||
int count = points.Count;
|
||||
double stepX = count <= 1 ? 0 : plotWidth / (count - 1);
|
||||
|
||||
Func<int, double> getX = i => marginLeft + stepX * i;
|
||||
Func<double, double> getY = v => marginTop + plotHeight * (1 - v / yMax);
|
||||
|
||||
// 1. 横向网格线 + Y 轴刻度
|
||||
for (int k = 0; k <= 4; k++)
|
||||
{
|
||||
double ratio = k / 4.0;
|
||||
double y = marginTop + plotHeight * ratio;
|
||||
double v = yMax * (1 - ratio);
|
||||
|
||||
LayoutRoot.Children.Add(new Line
|
||||
{
|
||||
X1 = marginLeft, Y1 = y, X2 = marginLeft + plotWidth, Y2 = y,
|
||||
Stroke = new SolidColorBrush(Color.FromRgb(238, 238, 238)),
|
||||
StrokeThickness = 1
|
||||
});
|
||||
|
||||
var yLabel = new TextBlock
|
||||
{
|
||||
Text = FormatValue(v),
|
||||
FontSize = 11,
|
||||
Foreground = new SolidColorBrush(Color.FromRgb(153, 153, 153)),
|
||||
Width = marginLeft - 6,
|
||||
TextAlignment = TextAlignment.Right
|
||||
};
|
||||
LayoutRoot.Children.Add(yLabel);
|
||||
Canvas.SetLeft(yLabel, 0);
|
||||
Canvas.SetTop(yLabel, y - 8);
|
||||
}
|
||||
|
||||
// 2. 面积填充
|
||||
if (count > 1)
|
||||
{
|
||||
var figure = new PathFigure { StartPoint = new Point(getX(0), getY(values[0])) };
|
||||
for (int i = 1; i < count; i++)
|
||||
figure.Segments.Add(new LineSegment { Point = new Point(getX(i), getY(values[i])) });
|
||||
figure.Segments.Add(new LineSegment { Point = new Point(getX(count - 1), marginTop + plotHeight) });
|
||||
figure.Segments.Add(new LineSegment { Point = new Point(getX(0), marginTop + plotHeight) });
|
||||
figure.IsClosed = true;
|
||||
|
||||
var gradient = new LinearGradientBrush
|
||||
{
|
||||
StartPoint = new Point(0, 0),
|
||||
EndPoint = new Point(0, 1)
|
||||
};
|
||||
gradient.GradientStops.Add(new GradientStop(Color.FromArgb(64, lineColor.R, lineColor.G, lineColor.B), 0));
|
||||
gradient.GradientStops.Add(new GradientStop(Color.FromArgb(8, lineColor.R, lineColor.G, lineColor.B), 1));
|
||||
|
||||
LayoutRoot.Children.Add(new Path
|
||||
{
|
||||
Data = new PathGeometry { Figures = { figure } },
|
||||
Fill = gradient
|
||||
});
|
||||
}
|
||||
|
||||
// 3. 折线
|
||||
var polylinePoints = new PointCollection();
|
||||
for (int i = 0; i < count; i++)
|
||||
polylinePoints.Add(new Point(getX(i), getY(values[i])));
|
||||
|
||||
LayoutRoot.Children.Add(new Polyline
|
||||
{
|
||||
Points = polylinePoints,
|
||||
Stroke = lineBrush,
|
||||
StrokeThickness = LineThickness,
|
||||
StrokeLineJoin = PenLineJoin.Round
|
||||
});
|
||||
|
||||
// 4. 数据点(点较多时省略;悬停显示该点数据)
|
||||
if (count <= 31)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var dot = new Ellipse
|
||||
{
|
||||
ToolTip = CreateTooltip(points[i])
|
||||
};
|
||||
dot. Style = (Style)FindResource("LineEllipse");
|
||||
//// 悬停时高亮圆点
|
||||
//dot.MouseEnter += (s, e) => dot.Fill = lineBrush;
|
||||
//dot.MouseLeave += (s, e) => dot.Fill = Brushes.White;
|
||||
|
||||
LayoutRoot.Children.Add(dot);
|
||||
Canvas.SetLeft(dot, getX(i) - 3.5);
|
||||
Canvas.SetTop(dot, getY(values[i]) - 3.5);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. X 轴日期刻度(最多显示 8 个)
|
||||
int labelStep = Math.Max(1, (int)Math.Ceiling(count / 8.0));
|
||||
for (int i = 0; i < count; i += labelStep)
|
||||
{
|
||||
var tb = new TextBlock
|
||||
{
|
||||
Text = points[i].DateText,
|
||||
FontSize = 11,
|
||||
Foreground = new SolidColorBrush(Color.FromRgb(153, 153, 153)),
|
||||
TextAlignment = TextAlignment.Center,
|
||||
Width = 60
|
||||
};
|
||||
LayoutRoot.Children.Add(tb);
|
||||
double x = getX(i) - 30;
|
||||
x = Math.Max(0, Math.Min(ActualWidth - 60, x));
|
||||
Canvas.SetLeft(tb, x);
|
||||
Canvas.SetTop(tb, marginTop + plotHeight + 6);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>生成数据点悬浮提示(日期 / 销售额 / 订单数)</summary>
|
||||
private static object CreateTooltip(RevenueTrendPoint p)
|
||||
{
|
||||
var panel = new StackPanel();
|
||||
var txtBrush = new SolidColorBrush(Color.FromRgb(51, 51, 51));
|
||||
panel.Children.Add(new TextBlock
|
||||
{
|
||||
Text = p.Date.ToString("yyyy-MM-dd"),
|
||||
FontSize = 11,
|
||||
FontWeight = FontWeights.SemiBold,
|
||||
Foreground = txtBrush
|
||||
});
|
||||
panel.Children.Add(new TextBlock
|
||||
{
|
||||
Text = "销售额:" + FormatValue((double)p.SalesAmount) + "元",
|
||||
FontSize = 11,
|
||||
Foreground = txtBrush
|
||||
});
|
||||
panel.Children.Add(new TextBlock
|
||||
{
|
||||
Text = "订单数:" + p.OrderCount + " 单",
|
||||
FontSize = 11,
|
||||
Foreground = txtBrush
|
||||
});
|
||||
|
||||
return new Border
|
||||
{
|
||||
Background = Brushes.White,
|
||||
CornerRadius = new CornerRadius(4),
|
||||
Padding = new Thickness(8, 5, 8, 5),
|
||||
Child = panel
|
||||
};
|
||||
}
|
||||
|
||||
private static string FormatValue(double value)
|
||||
{
|
||||
double abs = Math.Abs(value);
|
||||
if (abs >= 100000000)
|
||||
return (value / 100000000d).ToString("0.#") + "亿";
|
||||
if (abs >= 10000)
|
||||
return (value / 10000d).ToString("0.#") + "万";
|
||||
return value.ToString("#,##0");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<UserControl x:Class="_29WxOrderWpf.Ctrls.Statistics.Charts.RevenuePieChart"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="320" d:DesignWidth="520">
|
||||
<Canvas x:Name="LayoutRoot" ClipToBounds="True"/>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,425 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
using _29WxOrderWpf.Ctrls.Statistics.Models;
|
||||
|
||||
namespace _29WxOrderWpf.Ctrls.Statistics.Charts
|
||||
{
|
||||
/// <summary>
|
||||
/// 店铺销售占比饼图(轻量自绘,无第三方依赖)。
|
||||
/// 绑定 <see cref="ItemsSource"/>(IList<ShopSalesProportionItem>)即可,数据变化时自动重绘。
|
||||
/// 设置 <see cref="MaxItems"/>(>0)可只显示前 N 项,其余项合并为一个“<see cref="OtherLabel"/>”扇区(百分比为剩余总和,整体仍为 100%)。
|
||||
/// 设置 <see cref="Title"/> 可在环形图中央留白处显示标题(<see cref="TitlePlacement"/> 可切换为顶部),不会遮挡扇形;
|
||||
/// 图例通过 <see cref="LegendPosition"/> 可放置到 4 个角,饼图区域由独立 panel 包裹,方便整体挪动和调整大小。
|
||||
/// </summary>
|
||||
public partial class RevenuePieChart : UserControl
|
||||
{
|
||||
/// <summary>标题显示位置</summary>
|
||||
public enum PieTitlePlacement
|
||||
{
|
||||
/// <summary>居中显示在环形图中央留白处(默认,不遮挡扇形)</summary>
|
||||
Center,
|
||||
|
||||
/// <summary>显示在图表顶部</summary>
|
||||
Top
|
||||
}
|
||||
|
||||
/// <summary>图例显示位置(4 个角)</summary>
|
||||
public enum PieLegendPosition
|
||||
{
|
||||
/// <summary>左上角</summary>
|
||||
TopLeft,
|
||||
|
||||
/// <summary>右上角(默认)</summary>
|
||||
TopRight,
|
||||
|
||||
/// <summary>左下角</summary>
|
||||
BottomLeft,
|
||||
|
||||
/// <summary>右下角</summary>
|
||||
BottomRight
|
||||
}
|
||||
|
||||
/// <summary>待绘制扇区(含颜色)</summary>
|
||||
private sealed class DisplaySlice
|
||||
{
|
||||
public ShopSalesProportionItem Item;
|
||||
public Brush Brush;
|
||||
}
|
||||
|
||||
/// <summary>ECharts 默认配色</summary>
|
||||
private static readonly Color[] Palette =
|
||||
{
|
||||
Color.FromRgb(84, 112, 198),
|
||||
Color.FromRgb(145, 204, 117),
|
||||
Color.FromRgb(250, 200, 88),
|
||||
Color.FromRgb(238, 102, 102),
|
||||
Color.FromRgb(115, 192, 222),
|
||||
Color.FromRgb(59, 162, 114),
|
||||
Color.FromRgb(252, 132, 82),
|
||||
Color.FromRgb(154, 96, 180),
|
||||
Color.FromRgb(234, 124, 204)
|
||||
};
|
||||
|
||||
/// <summary>“其他”合并项默认颜色(灰色)</summary>
|
||||
private static readonly Color OtherColor = Color.FromRgb(192, 196, 204);
|
||||
|
||||
public static readonly DependencyProperty ItemsSourceProperty =
|
||||
DependencyProperty.Register("ItemsSource", typeof(IList<ShopSalesProportionItem>), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(null, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>占比数据</summary>
|
||||
public IList<ShopSalesProportionItem> ItemsSource
|
||||
{
|
||||
get { return (IList<ShopSalesProportionItem>)GetValue(ItemsSourceProperty); }
|
||||
set { SetValue(ItemsSourceProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty MaxItemsProperty =
|
||||
DependencyProperty.Register("MaxItems", typeof(int), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(0, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>最多显示的独立扇区数(>0 时其余项合并为“其他”;<=0 显示全部)</summary>
|
||||
public int MaxItems
|
||||
{
|
||||
get { return (int)GetValue(MaxItemsProperty); }
|
||||
set { SetValue(MaxItemsProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty OtherLabelProperty =
|
||||
DependencyProperty.Register("OtherLabel", typeof(string), typeof(RevenuePieChart),
|
||||
new PropertyMetadata("其他", OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>合并项的显示名称(默认“其他”)</summary>
|
||||
public string OtherLabel
|
||||
{
|
||||
get { return (string)GetValue(OtherLabelProperty); }
|
||||
set { SetValue(OtherLabelProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty OtherBrushProperty =
|
||||
DependencyProperty.Register("OtherBrush", typeof(Brush), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(new SolidColorBrush(OtherColor), OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>合并项的颜色(默认灰色)</summary>
|
||||
public Brush OtherBrush
|
||||
{
|
||||
get { return (Brush)GetValue(OtherBrushProperty); }
|
||||
set { SetValue(OtherBrushProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TitleProperty =
|
||||
DependencyProperty.Register("Title", typeof(string), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(null, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>标题文本(空/Null 不显示)</summary>
|
||||
public string Title
|
||||
{
|
||||
get { return (string)GetValue(TitleProperty); }
|
||||
set { SetValue(TitleProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TitlePlacementProperty =
|
||||
DependencyProperty.Register("TitlePlacement", typeof(PieTitlePlacement), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(PieTitlePlacement.Center, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>标题显示位置(默认居中显示在环形图留白处,不遮挡扇形)</summary>
|
||||
public PieTitlePlacement TitlePlacement
|
||||
{
|
||||
get { return (PieTitlePlacement)GetValue(TitlePlacementProperty); }
|
||||
set { SetValue(TitlePlacementProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TitleFontSizeProperty =
|
||||
DependencyProperty.Register("TitleFontSize", typeof(double), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(14d, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>标题字号</summary>
|
||||
public double TitleFontSize
|
||||
{
|
||||
get { return (double)GetValue(TitleFontSizeProperty); }
|
||||
set { SetValue(TitleFontSizeProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty TitleBrushProperty =
|
||||
DependencyProperty.Register("TitleBrush", typeof(Brush), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(new SolidColorBrush(Color.FromRgb(51, 51, 51)), OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>标题颜色</summary>
|
||||
public Brush TitleBrush
|
||||
{
|
||||
get { return (Brush)GetValue(TitleBrushProperty); }
|
||||
set { SetValue(TitleBrushProperty, value); }
|
||||
}
|
||||
|
||||
public static readonly DependencyProperty LegendPositionProperty =
|
||||
DependencyProperty.Register("LegendPosition", typeof(PieLegendPosition), typeof(RevenuePieChart),
|
||||
new PropertyMetadata(PieLegendPosition.TopRight, OnRenderPropertyChanged));
|
||||
|
||||
/// <summary>图例显示位置(默认右上角,与饼图分列放置,不遮挡)</summary>
|
||||
public PieLegendPosition LegendPosition
|
||||
{
|
||||
get { return (PieLegendPosition)GetValue(LegendPositionProperty); }
|
||||
set { SetValue(LegendPositionProperty, value); }
|
||||
}
|
||||
|
||||
public RevenuePieChart()
|
||||
{
|
||||
InitializeComponent();
|
||||
Loaded += delegate { Render(); };
|
||||
SizeChanged += delegate { Render(); };
|
||||
}
|
||||
|
||||
private static void OnRenderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
((RevenuePieChart)d).Render();
|
||||
}
|
||||
|
||||
private void Render()
|
||||
{
|
||||
LayoutRoot.Children.Clear();
|
||||
var items = ItemsSource;
|
||||
if (items == null || items.Count == 0 || ActualWidth <= 20 || ActualHeight <= 20)
|
||||
return;
|
||||
|
||||
double width = ActualWidth;
|
||||
double height = ActualHeight;
|
||||
|
||||
// 图例列宽:图例占左/右一列,饼图占另一列,天然不会互相遮挡
|
||||
double legendWidth = Math.Min(210, width * 0.38);
|
||||
bool legendOnLeft = LegendPosition == PieLegendPosition.TopLeft
|
||||
|| LegendPosition == PieLegendPosition.BottomLeft;
|
||||
|
||||
double pieLeft = legendOnLeft ? legendWidth : 0;
|
||||
double pieWidth = Math.Max(10, width - legendWidth);
|
||||
double pieHeight = height;
|
||||
|
||||
// 顶部标题:先预留高度,饼图相应下移,避免遮挡
|
||||
double topTitleHeight = 0;
|
||||
if (TitlePlacement == PieTitlePlacement.Top && !string.IsNullOrEmpty(Title))
|
||||
{
|
||||
var probe = CreateTitleBlock(pieWidth);
|
||||
probe.Measure(new Size(pieWidth, double.PositiveInfinity));
|
||||
topTitleHeight = Math.Ceiling(probe.DesiredSize.Height) + 8;
|
||||
}
|
||||
|
||||
// 饼图区域用独立 Canvas 包裹,方便整体挪动(SetLeft/SetTop)和修改大小(Width/Height)
|
||||
var piePanel = new Canvas { Width = pieWidth, Height = pieHeight };
|
||||
LayoutRoot.Children.Add(piePanel);
|
||||
Canvas.SetLeft(piePanel, pieLeft);
|
||||
Canvas.SetTop(piePanel, 0);
|
||||
|
||||
double cx = pieWidth / 2;
|
||||
double cy = (topTitleHeight + pieHeight) / 2;
|
||||
double radius = Math.Max(10, Math.Min(pieWidth, pieHeight - topTitleHeight) / 2 - 18);
|
||||
|
||||
// 0. 生成实际绘制的扇区列表(MaxItems > 0 时其余项合并为“其他”)
|
||||
var slices = BuildSlices(items);
|
||||
|
||||
// 1. 绘制扇形
|
||||
double startAngle = -90; // 从正上方开始,顺时针
|
||||
foreach (var slice in slices)
|
||||
{
|
||||
double sweep = slice.Item.Percentage / 100d * 360d;
|
||||
if (sweep < 0) sweep = 0;
|
||||
var brush = slice.Brush;
|
||||
|
||||
if (sweep >= 359.9)
|
||||
{
|
||||
var circle = new Ellipse
|
||||
{
|
||||
Width = radius * 2,
|
||||
Height = radius * 2,
|
||||
Fill = brush
|
||||
};
|
||||
piePanel.Children.Add(circle);
|
||||
Canvas.SetLeft(circle, cx - radius);
|
||||
Canvas.SetTop(circle, cy - radius);
|
||||
}
|
||||
else if (sweep > 0.01)
|
||||
{
|
||||
piePanel.Children.Add(CreateSlice(cx, cy, radius, startAngle, sweep, brush));
|
||||
}
|
||||
startAngle += sweep;
|
||||
}
|
||||
|
||||
// 2. 中间留白圆心
|
||||
double holeRadius = radius * 0.55;
|
||||
var hole = new Ellipse
|
||||
{
|
||||
Width = holeRadius * 2,
|
||||
Height = holeRadius * 2,
|
||||
Fill = Brushes.White
|
||||
};
|
||||
piePanel.Children.Add(hole);
|
||||
Canvas.SetLeft(hole, cx - holeRadius);
|
||||
Canvas.SetTop(hole, cy - holeRadius);
|
||||
|
||||
// 3. 标题(居中显示在环形图留白处,宽度限制在留白直径内,不遮挡扇形)
|
||||
if (!string.IsNullOrEmpty(Title))
|
||||
{
|
||||
double maxW = TitlePlacement == PieTitlePlacement.Center
|
||||
? Math.Max(20, holeRadius * 2 - 8)
|
||||
: pieWidth;
|
||||
var titleBlock = CreateTitleBlock(maxW);
|
||||
titleBlock.Measure(new Size(maxW, double.PositiveInfinity));
|
||||
double w = Math.Min(titleBlock.DesiredSize.Width, maxW);
|
||||
double h = titleBlock.DesiredSize.Height;
|
||||
|
||||
piePanel.Children.Add(titleBlock);
|
||||
Canvas.SetLeft(titleBlock, cx - w / 2);
|
||||
Canvas.SetTop(titleBlock, TitlePlacement == PieTitlePlacement.Center ? cy - h / 2 : 0);
|
||||
}
|
||||
|
||||
// 4. 图例(4 个角可设置,与饼图分列,不遮挡)
|
||||
var legendPanel = CreateLegendPanel(slices, legendWidth - 16);
|
||||
legendPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
|
||||
double legendW = legendPanel.DesiredSize.Width;
|
||||
double legendH = legendPanel.DesiredSize.Height;
|
||||
|
||||
const double padding = 8;
|
||||
double lx, ly;
|
||||
if (LegendPosition == PieLegendPosition.TopLeft || LegendPosition == PieLegendPosition.TopRight)
|
||||
ly = padding;
|
||||
else
|
||||
ly = Math.Max(padding, height - legendH - padding);
|
||||
lx = legendOnLeft ? padding : width - legendW - padding;
|
||||
|
||||
LayoutRoot.Children.Add(legendPanel);
|
||||
Canvas.SetLeft(legendPanel, lx);
|
||||
Canvas.SetTop(legendPanel, ly);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 <see cref="MaxItems"/> 生成实际绘制的扇区列表:
|
||||
/// 按占比从大到小取前 N 项单独显示,其余项合并为一个“其他”扇区(百分比为剩余总和,整体仍为 100%)。
|
||||
/// </summary>
|
||||
private List<DisplaySlice> BuildSlices(IList<ShopSalesProportionItem> items)
|
||||
{
|
||||
var result = new List<DisplaySlice>();
|
||||
if (items == null || items.Count == 0)
|
||||
return result;
|
||||
|
||||
var ordered = items
|
||||
.OrderByDescending(i => i.Percentage)
|
||||
.ThenByDescending(i => i.SalesAmount)
|
||||
.ToList();
|
||||
|
||||
int max = MaxItems > 0 ? MaxItems : ordered.Count;
|
||||
if (max >= ordered.Count)
|
||||
{
|
||||
for (int i = 0; i < ordered.Count; i++)
|
||||
result.Add(new DisplaySlice
|
||||
{
|
||||
Item = ordered[i],
|
||||
Brush = new SolidColorBrush(Palette[i % Palette.Length])
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
for (int i = 0; i < max; i++)
|
||||
result.Add(new DisplaySlice
|
||||
{
|
||||
Item = ordered[i],
|
||||
Brush = new SolidColorBrush(Palette[i % Palette.Length])
|
||||
});
|
||||
|
||||
var rest = ordered.Skip(max).ToList();
|
||||
result.Add(new DisplaySlice
|
||||
{
|
||||
Item = new ShopSalesProportionItem
|
||||
{
|
||||
ShopName = string.IsNullOrEmpty(OtherLabel) ? "其他" : OtherLabel,
|
||||
Percentage = rest.Sum(r => r.Percentage),
|
||||
SalesAmount = rest.Sum(r => r.SalesAmount)
|
||||
},
|
||||
Brush = OtherBrush ?? new SolidColorBrush(OtherColor)
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
private TextBlock CreateTitleBlock(double maxWidth)
|
||||
{
|
||||
var brush = TitleBrush ?? new SolidColorBrush(Color.FromRgb(51, 51, 51));
|
||||
return new TextBlock
|
||||
{
|
||||
Text = Title,
|
||||
FontSize = TitleFontSize,
|
||||
FontWeight = FontWeights.Bold,
|
||||
Foreground = brush,
|
||||
TextAlignment = TextAlignment.Center,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
TextTrimming = TextTrimming.CharacterEllipsis,
|
||||
MaxWidth = maxWidth
|
||||
};
|
||||
}
|
||||
|
||||
private static StackPanel CreateLegendPanel(IList<DisplaySlice> slices, double maxWidth)
|
||||
{
|
||||
double maxLegendWidth = Math.Max(60, maxWidth);
|
||||
var legendPanel = new StackPanel { MaxWidth = maxLegendWidth };
|
||||
foreach (var slice in slices)
|
||||
{
|
||||
var legendRow = new StackPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
Margin = new Thickness(0, 4, 0, 4)
|
||||
};
|
||||
legendRow.Children.Add(new Rectangle
|
||||
{
|
||||
Width = 12,
|
||||
Height = 12,
|
||||
Fill = slice.Brush
|
||||
});
|
||||
|
||||
var text = new TextBlock
|
||||
{
|
||||
Text = slice.Item.ShopName + " " + slice.Item.Percentage.ToString("0.0") + "%",
|
||||
FontSize = 12,
|
||||
Foreground = new SolidColorBrush(Color.FromRgb(102, 102, 102)),
|
||||
Margin = new Thickness(6, 0, 0, 0),
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
MaxWidth = Math.Max(40, maxLegendWidth - 18),
|
||||
TextTrimming = TextTrimming.CharacterEllipsis
|
||||
};
|
||||
legendRow.Children.Add(text);
|
||||
legendPanel.Children.Add(legendRow);
|
||||
}
|
||||
return legendPanel;
|
||||
}
|
||||
|
||||
private static Path CreateSlice(double cx, double cy, double radius, double startAngle, double sweep, Brush fill)
|
||||
{
|
||||
var start = PointOnCircle(cx, cy, radius, startAngle);
|
||||
var end = PointOnCircle(cx, cy, radius, startAngle + sweep);
|
||||
|
||||
var figure = new PathFigure { StartPoint = new Point(cx, cy), IsClosed = true, IsFilled = true };
|
||||
figure.Segments.Add(new LineSegment { Point = start });
|
||||
figure.Segments.Add(new ArcSegment
|
||||
{
|
||||
Point = end,
|
||||
Size = new Size(radius, radius),
|
||||
IsLargeArc = sweep > 180,
|
||||
SweepDirection = SweepDirection.Clockwise
|
||||
});
|
||||
|
||||
return new Path
|
||||
{
|
||||
Data = new PathGeometry { Figures = { figure } },
|
||||
Fill = fill,
|
||||
Stroke = Brushes.White,
|
||||
StrokeThickness = 1
|
||||
};
|
||||
}
|
||||
|
||||
private static Point PointOnCircle(double cx, double cy, double radius, double angle)
|
||||
{
|
||||
double rad = angle * Math.PI / 180d;
|
||||
return new Point(cx + radius * Math.Cos(rad), cy + radius * Math.Sin(rad));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user