Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CountTable.cs
1using SkiaSharp;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using Waher.Content;
6
8{
12 public class CountTable
13 {
14 private readonly LinkedList<KeyValuePair<string, long>> counts = new LinkedList<KeyValuePair<string, long>>();
15 private readonly Dictionary<string, string> bgColors = new Dictionary<string, string>();
16 private readonly Dictionary<string, string> fgColors = new Dictionary<string, string>();
17 private long maxCount = 1; // To avoid division by zero
18 private int count = 0;
19
23 public CountTable()
24 {
25 }
26
32 public void Add(string Id, long Count)
33 {
34 this.counts.AddLast(new KeyValuePair<string, long>(Id, Count));
35 this.count++;
36
37 if (Count > this.maxCount)
38 this.maxCount = Count;
39 }
40
46 public void SetFgColor(string Id, string Color)
47 {
48 this.fgColors[Id] = Color;
49 }
50
56 public void SetBgColor(string Id, string Color)
57 {
58 this.bgColors[Id] = Color;
59 }
60
68 public void ExportTableMarkdown(StreamWriter Output, string Header, string Title, string Id)
69 {
70 Output.Write("| ");
71 Output.Write(Header);
72 Output.WriteLine(" | Count |");
73 Output.WriteLine("|:---------|------:|");
74
75 foreach (KeyValuePair<string, long> P in this.counts)
76 {
77 Output.Write("| `");
78 Output.Write(P.Key);
79 Output.Write("` | ");
80 Output.Write(P.Value.ToString());
81 Output.WriteLine(" |");
82 }
83
84 Output.Write("[");
85 Output.Write(Title);
86 Output.Write("][");
87 Output.Write(Id);
88 Output.WriteLine("]");
89 Output.WriteLine();
90 }
91
97 public void ExportTableGraph(StreamWriter Output, string Title)
98 {
99 SKColor[] Palette = Model.CreatePalette(this.count);
100 int i = 0;
101
102 Output.Write("```layout: ");
103 Output.WriteLine(Title);
104 Output.WriteLine("<Layout2D xmlns=\"http://waher.se/Schema/Layout2D.xsd\"");
105 Output.WriteLine(" background=\"WhiteBackground\" pen=\"BlackPen\"");
106 Output.WriteLine(" font=\"Text\" textColor=\"Black\">");
107 Output.WriteLine(" <SolidPen id=\"BlackPen\" color=\"Black\" width=\"1px\"/>");
108 Output.WriteLine(" <SolidBackground id=\"WhiteBackground\" color=\"WhiteSmoke\"/>");
109
110 foreach (KeyValuePair<string, long> P in this.counts)
111 {
112 SKColor Color = Palette[i++];
113
114 Output.Write(" <SolidBackground id=\"");
115 Output.Write(P.Key);
116 Output.Write("Bg\" color=\"");
117
118 if (this.bgColors.TryGetValue(P.Key, out string s))
119 Output.Write(s);
120 else
121 Output.Write(Model.ToString(Color));
122
123 Output.WriteLine("\"/>");
124 }
125
126 Output.WriteLine(" <Font id=\"Text\" name=\"Arial\" size=\"12pt\" color=\"Black\"/>");
127
128 foreach (KeyValuePair<string, string> P in this.fgColors)
129 {
130 Output.Write(" <Font id=\"");
131 Output.Write(P.Key);
132 Output.Write("Fg\" name=\"Arial\" size=\"12pt\" color=\"");
133 Output.Write(P.Value);
134 Output.WriteLine("\"/>");
135 }
136
137 Output.WriteLine(" <Grid columns=\"2\">");
138
139 long HalfMaxCount = this.maxCount / 2;
140
141 foreach (KeyValuePair<string, long> P in this.counts)
142 {
143 Output.WriteLine(" <Cell>");
144 Output.WriteLine(" <Margins left=\"0.5em\" right=\"0.5em\">");
145 Output.Write(" <Label text=\"");
146 Output.Write(P.Key);
147 Output.WriteLine("\" x=\"100%\" y=\"50%\" halign=\"Right\" valign=\"Center\" font=\"Text\"/>");
148 Output.WriteLine(" </Margins>");
149 Output.WriteLine(" </Cell>");
150 Output.WriteLine(" <Cell>");
151 Output.WriteLine(" <Margins left=\"1mm\" top=\"1mm\" bottom=\"1mm\" right=\"1mm\">");
152 Output.Write(" <RoundedRectangle radiusX=\"3mm\" radiusY=\"3mm\" width=\"");
153 Output.Write(CommonTypes.Encode(((P.Value * 10000 + HalfMaxCount) / this.maxCount)*0.01));
154 Output.Write("%\" height=\"1cm\" fill=\"");
155 Output.Write(P.Key);
156 Output.Write("Bg");
157 Output.WriteLine("\">");
158 Output.WriteLine(" <Margins left=\"0.5em\" right=\"0.5em\">");
159 Output.Write(" <Label text=\"");
160 Output.Write(P.Value.ToString());
161 Output.Write("\" x=\"50%\" y=\"50%\" halign=\"Center\" valign=\"Center\"");
162
163 if (this.fgColors.ContainsKey(P.Key))
164 {
165 Output.Write(" font=\"");
166 Output.Write(P.Key);
167 Output.Write("Fg\"");
168 }
169
170 Output.WriteLine("/>");
171 Output.WriteLine(" </Margins>");
172 Output.WriteLine(" </RoundedRectangle>");
173 Output.WriteLine(" </Margins>");
174 Output.WriteLine(" </Cell>");
175 }
176
177 Output.WriteLine(" </Grid>");
178 Output.WriteLine("</Layout2D>");
179 Output.WriteLine("```");
180 Output.WriteLine();
181 }
182
183 }
184}
Root node of a simulation model
Definition: Model.cs:49
static SKColor[] CreatePalette(int N)
Creates a palette for graphs.
Definition: Model.cs:1185
static string ToString(SKColor Color)
Gets the string representation of a color.
Definition: Model.cs:1212
Represents a simple count table
Definition: CountTable.cs:13
void ExportTableGraph(StreamWriter Output, string Title)
Exports data as a graph embedded in markdown
Definition: CountTable.cs:97
void ExportTableMarkdown(StreamWriter Output, string Header, string Title, string Id)
Exports data as a Markdown Table
Definition: CountTable.cs:68
void Add(string Id, long Count)
Adds a record to the table.
Definition: CountTable.cs:32
void SetFgColor(string Id, string Color)
Sets the foreground color for a record.
Definition: CountTable.cs:46
CountTable()
Represents a simple count table
Definition: CountTable.cs:23
void SetBgColor(string Id, string Color)
Sets the background color for a record.
Definition: CountTable.cs:56
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594