优化箱线图展示,可以自定义5个名字

This commit is contained in:
智能大石头 2025-07-28 10:49:07 +08:00
parent 55cd18109c
commit bb960cc5cb
3 changed files with 20 additions and 6 deletions

View File

@ -64,7 +64,9 @@ public class UserStatController : ReadOnlyEntityController<UserStat>
chart2.SetX(list2, "Date", e => e.Date.ToString("MM-dd"));
//chart2.SetY("用户数", "value");
chart2.AddBoxplot(list2.Select(e => new BoxplotItem(e.News, e.NewsT7, e.Actives, e.ActivesT7, e.ActivesT30)));
chart2.AddBoxplot(
list2.Select(e => new BoxplotItem(e.News, e.Actives, e.NewsT7, e.ActivesT7, e.ActivesT30)),
["新用户", "今日活跃", "7天注册", "7天活跃", "30天活跃"]);
ViewBag.Charts2 = new[] { chart2 };
}

View File

@ -731,15 +731,16 @@ public class ECharts : IExtend
}
/// <summary>添加箱线图</summary>
/// <param name="items"></param>
/// <param name="items">数据集</param>
/// <param name="names">五个名字。不一定是最小值最大值</param>
/// <returns></returns>
public Series AddBoxplot(IEnumerable<BoxplotItem> items)
public Series AddBoxplot(IEnumerable<BoxplotItem> items, String[] names = null)
{
var box = Create("boxplot", SeriesTypes.Boxplot) as SeriesBoxplot;
box.Data = items.Select(e => new[] { e.Min, e.Q1, e.Median, e.Q3, e.Max }).ToArray();
Add(box);
SetTooltip("item", """
var script = """
function boxplotFormatter(params) {
return `${params.name}<br/>
: ${params.value[1]}<br/>
@ -748,7 +749,18 @@ public class ECharts : IExtend
: ${params.value[4]}<br/>
: ${params.value[5]}`;
}
""");
""";
if (names != null && names.Length >= 5)
{
// 名称为null时表示不替换
if (names[0] != null) script = script.Replace("最小值", names[0]);
if (names[1] != null) script = script.Replace("下四分", names[1]);
if (names[2] != null) script = script.Replace("中位数", names[2]);
if (names[3] != null) script = script.Replace("上四分", names[3]);
if (names[4] != null) script = script.Replace("最大值", names[4]);
}
SetTooltip("item", script);
// 添加Y轴
if (YAxis == null || YAxis.Count == 0) SetY("值", "value");

View File

@ -6,4 +6,4 @@
/// <param name="Median">中位数</param>
/// <param name="Q3">上四分位数</param>
/// <param name="Max">最大值</param>
public record BoxplotItem(Single Min, Single Q1, Single Median, Single Q3, Single Max);
public record BoxplotItem(Double Min, Double Q1, Double Median, Double Q3, Double Max);