* UIDoughnutChart: 修复数据全为0时报错

This commit is contained in:
Sunny 2024-05-07 14:16:55 +08:00
parent 06309a546f
commit 1277bad3f5

View File

@ -20,6 +20,7 @@
* 2021-07-22: V3.0.5 * 2021-07-22: V3.0.5
* 2022-07-29: V3.2.2 Option.DecimalPlaces * 2022-07-29: V3.2.2 Option.DecimalPlaces
* 2023-05-14: V3.3.6 DrawString函数 * 2023-05-14: V3.3.6 DrawString函数
* 2024-05-07: V3.6.6 0
******************************************************************************/ ******************************************************************************/
using System; using System;
@ -126,12 +127,11 @@ namespace Sunny.UI
all += data.Value; all += data.Value;
} }
if (all.IsZero()) return;
float start = 0; float start = 0;
for (int i = 0; i < pie.Data.Count; i++) for (int i = 0; i < pie.Data.Count; i++)
{ {
float angle = (float)(pie.Data[i].Value * 360.0f / all); float angle = all.IsZero() ? 0 : (float)(pie.Data[i].Value * 360.0f / all);
float percent = (float)(pie.Data[i].Value * 100.0f / all); float percent = all.IsZero() ? 0 : (float)(pie.Data[i].Value * 100.0f / all);
string text = ""; string text = "";
if (Option.ToolTip != null) if (Option.ToolTip != null)
{ {
@ -166,7 +166,22 @@ namespace Sunny.UI
for (int pieIndex = 0; pieIndex < series.Count; pieIndex++) for (int pieIndex = 0; pieIndex < series.Count; pieIndex++)
{ {
var pie = series[pieIndex]; var pie = series[pieIndex];
if (pie.Data.Count == 0) continue;
double all = 0;
foreach (var data in pie.Data)
{
all += data.Value;
}
if (all.IsZero())
{
Angle angle = Angles[pieIndex][0];
g.DrawEllipse(rectColor, new RectangleF(angle.Center.X - angle.Inner, angle.Center.Y - angle.Inner, angle.Inner * 2, angle.Inner * 2));
g.DrawEllipse(rectColor, new RectangleF(angle.Center.X - angle.Outer, angle.Center.Y - angle.Outer, angle.Outer * 2, angle.Outer * 2));
}
else
{
for (int azIndex = 0; azIndex < pie.Data.Count; azIndex++) for (int azIndex = 0; azIndex < pie.Data.Count; azIndex++)
{ {
Angle angle = Angles[pieIndex][azIndex]; Angle angle = Angles[pieIndex][azIndex];
@ -191,6 +206,7 @@ namespace Sunny.UI
} }
} }
} }
}
private readonly ConcurrentDictionary<int, ConcurrentDictionary<int, Angle>> Angles = new ConcurrentDictionary<int, ConcurrentDictionary<int, Angle>>(); private readonly ConcurrentDictionary<int, ConcurrentDictionary<int, Angle>> Angles = new ConcurrentDictionary<int, ConcurrentDictionary<int, Angle>>();