4
4
5
5
namespace Signum.Engine.Chart;
6
6
7
-
public static class ChartColorLogic
7
+
public static class ColorPaletteLogic
8
8
{
9
-
public static ResetLazy<Dictionary<Type, Dictionary<PrimaryKey, string>>> Colors = null!;
9
+
public static ResetLazy<Dictionary<Type, ColorPaletteEntity>> ColorPaletteCache = null!;
10
10
11
11
public static readonly int Limit = 360;
12
12
13
13
internal static void Start(SchemaBuilder sb)
14
14
{
15
15
if (sb.NotDefined(MethodInfo.GetCurrentMethod()))
16
16
{
17
-
sb.Include<ChartColorEntity>()
17
+
sb.Include<ColorPaletteEntity>()
18
+
.WithSave(ColorPaletteOperation.Save)
19
+
.WithDelete(ColorPaletteOperation.Delete)
18
20
.WithQuery(() => cc => new
19
21
{
20
22
Entity = cc,
21
-
cc.Related,
22
-
cc.Color,
23
+
cc.Id,
24
+
cc.Type,
25
+
cc.CategoryName,
26
+
cc.Seed,
23
27
});
24
28
25
-
Colors = sb.GlobalLazy(() =>
26
-
Database.Query<ChartColorEntity>()
27
-
.Select(cc => new { cc.Related.EntityType, cc.Related.Id, cc.Color })
28
-
.AgGroupToDictionary(a => a.EntityType!, gr => gr.ToDictionary(a => a.Id, a => a.Color)),
29
-
new InvalidateWith(typeof(ChartColorEntity)));
29
+
ColorPaletteCache = sb.GlobalLazy(() =>
30
+
Database.Query<ColorPaletteEntity>()
31
+
.ToDictionaryEx(cc => cc.Type.ToType()),
32
+
new InvalidateWith(typeof(ColorPaletteEntity)));
30
33
}
31
34
}
32
35
33
-
public static Dictionary<string, string> Palettes = new Dictionary<string,string>(){
34
-
{"Category10", "#1f77b4 #ff7f0e #2ca02c #d62728 #9467bd #8c564b #e377c2 #7f7f7f #bcbd22 #17becf"},
35
-
{"Category20", "#1f77b4 #aec7e8 #ff7f0e #ffbb78 #2ca02c #98df8a #d62728 #ff9896 #9467bd #c5b0d5 #8c564b #c49c94 #e377c2 #f7b6d2 #7f7f7f #c7c7c7 #bcbd22 #dbdb8d #17becf #9edae5"},
36
-
{"Category20b", "#393b79 #5254a3 #6b6ecf #9c9ede #637939 #8ca252 #b5cf6b #cedb9c #8c6d31 #bd9e39 #e7ba52 #e7cb94 #843c39 #ad494a #d6616b #e7969c #7b4173 #a55194 #ce6dbd #de9ed6"},
37
-
{"Category20c", "#3182bd #6baed6 #9ecae1 #c6dbef #e6550d #fd8d3c #fdae6b #fdd0a2 #31a354 #74c476 #a1d99b #c7e9c0 #756bb1 #9e9ac8 #bcbddc #dadaeb #636363 #969696 #bdbdbd #d9d9d9"},
38
-
};
39
-
40
-
public static void CreateNewPalette(Type type, string palette)
41
-
{
42
-
AssertFewEntities(type);
43
-
44
-
var dic = Database.RetrieveAllLite(type).Select(l => new ChartColorEntity { Related = (Lite<Entity>)l }).ToDictionary(a => a.Related);
45
-
46
-
dic.SetRange(Database.Query<ChartColorEntity>().Where(c => c.Related.EntityType == type).ToDictionary(a => a.Related));
47
-
48
-
var list = dic.Values.ToList();
49
-
50
-
var cats = Palettes.GetOrThrow(palette).Split(' ');
51
-
52
-
for (int i = 0; i < list.Count; i++)
53
-
{
54
-
list[i].Color = cats[i % cats.Length];
55
-
}
56
-
57
-
list.SaveList();
58
-
}
59
-
60
-
public static void AssertFewEntities(Type type)
61
-
{
62
-
int count = giCount.GetInvoker(type)();
63
-
64
-
if (count > Limit)
65
-
throw new ApplicationException("Too many {0} ({1}), maximum is {2}".FormatWith(type.NicePluralName(), count, Limit));
66
-
}
67
-
68
-
public static bool HasTooManyEntities(Type type, out int count)
69
-
{
70
-
count = giCount.GetInvoker(type)();
71
-
72
-
return count > Limit;
73
-
}
74
-
75
-
public static void SavePalette(ChartPaletteModel model)
76
-
{
77
-
using (var tr = new Transaction())
78
-
{
79
-
Type type = TypeLogic.GetType(model.TypeName);
80
-
81
-
giDeleteColors.GetInvoker(type)();
82
-
83
-
model.Colors.Where(a => a.Color != null).SaveList();
84
-
tr.Commit();
85
-
}
86
-
}
87
-
88
-
89
-
static readonly GenericInvoker<Func<int>> giCount = new(() => Count<Entity>());
90
-
static int Count<T>() where T : Entity
91
-
{
92
-
return Database.Query<T>().Count();
93
-
}
94
-
95
-
static readonly GenericInvoker<Func<int>> giDeleteColors = new(() => DeleteColors<Entity>());
96
-
static int DeleteColors<T>() where T : Entity
97
-
{
98
-
return (from t in Database.Query<T>() // To filter by type conditions
99
-
join cc in Database.Query<ChartColorEntity>() on t.ToLite() equals cc.Related
100
-
select cc).UnsafeDelete();
101
-
}
102
-
103
-
public static ChartPaletteModel? GetPalette(Type type, bool allEntities)
104
-
{
105
-
var dic = ChartColorLogic.Colors.Value.TryGetC(type);
106
-
107
-
if (allEntities)
108
-
{
109
-
AssertFewEntities(type);
110
-
111
-
return new ChartPaletteModel
112
-
{
113
-
TypeName = TypeLogic.GetCleanName(type),
114
-
Colors = Database.RetrieveAllLite(type).Select(l => new ChartColorEntity
115
-
{
116
-
Related = (Lite<Entity>)l,
117
-
Color = dic?.TryGetC(l.Id)!
118
-
}).ToMList()
119
-
};
120
-
}
121
-
else
122
-
{
123
-
if (dic == null)
124
-
return null;
125
-
126
-
if (EnumEntity.IsEnumEntity(type))
127
-
{
128
-
var lites = EnumEntity.GetEntities(EnumEntity.Extract(type)!).ToDictionary(a => a.Id, a => a.ToLite());
129
-
130
-
return new ChartPaletteModel
131
-
{
132
-
TypeName = type.ToTypeEntity().CleanName,
133
-
Colors = dic.Select(kvp => new ChartColorEntity
134
-
{
135
-
Related = lites.GetOrThrow(kvp.Key),
136
-
Color = kvp.Value
137
-
}).ToMList()
138
-
};
139
-
}
140
-
else
141
-
{
142
-
return new ChartPaletteModel
143
-
{
144
-
TypeName = type.ToTypeEntity().CleanName,
145
-
Colors = dic.Select(kvp => new ChartColorEntity
146
-
{
147
-
Related = Lite.Create(type, kvp.Key),
148
-
Color = kvp.Value
149
-
}).ToMList()
150
-
};
151
-
}
152
-
}
153
-
}
154
-
155
-
public static string? ColorFor(Type type, PrimaryKey id)
156
-
{
157
-
return Colors.Value.TryGetC(type)?.TryGetC(id);
158
-
}
159
-
160
-
public static string? ColorFor(Lite<Entity> lite)
161
-
{
162
-
return ColorFor(lite.EntityType, lite.Id);
163
-
}
164
-
165
-
public static string? ColorFor(Entity ident)
166
-
{
167
-
return ColorFor(ident.GetType(), ident.Id);
168
-
}
169
-
170
-
public static void DeletePalette(Type type)
36
+
public static string? ColorFor(Entity entity)
171
37
{
172
-
Database.Query<ChartColorEntity>().Where(c => c.Related.EntityType == type).UnsafeDelete();
38
+
return ColorPaletteCache.Value.TryGetC(entity.GetType())?.SpecificColors.SingleEx(a => a.Entity.Is(entity))?.Color;
173
39
}
174
40
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4