Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CombinedGraph.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
5using System.Xml;
6using SkiaSharp;
8using Waher.Events;
9
11{
15 public abstract class CombinedGraph : Graph, ISourceRecipient
16 {
17 private readonly LinkedList<ISource> sources = new LinkedList<ISource>();
18 private int count = 0;
19 private string title;
20 private bool legend;
21 private bool span;
22
29 : base(Parent, Model)
30 {
31 }
32
36 public string Title => this.title;
37
42 public override Task FromXml(XmlElement Definition)
43 {
44 this.title = XML.Attribute(Definition, "title");
45 this.legend = XML.Attribute(Definition, "legend", true);
46 this.span = XML.Attribute(Definition, "span", true);
47
48 return base.FromXml(Definition);
49 }
50
55 public virtual void Register(ISource Source)
56 {
57 this.sources.AddLast(Source);
58 this.count++;
59 }
60
65 public override void ExportGraph(StreamWriter Output)
66 {
67 Output.WriteLine("{");
68 Output.WriteLine("GraphWidth:=1000;");
69 Output.WriteLine("GraphHeight:=400;");
70 this.ExportGraphScript(Output, null, true);
71 Output.WriteLine("}");
72 Output.WriteLine();
73
74 if (this.legend)
75 {
76 List<string> Labels = new List<string>();
77
78 foreach (Source Source in this.sources)
79 Labels.Add(Source.Reference);
80
81 ExportLegend(Output, Labels.ToArray());
82 }
83 }
84
91 public static void ExportLegend(StreamWriter Output, string[] Labels, params SKColor[] Palette)
92 {
93 int i = 0;
94
95 if (Palette is null || Palette.Length < Labels.Length)
96 Palette = Model.CreatePalette(Labels.Length);
97
98 Output.WriteLine("```layout: Legend");
99 Output.WriteLine("<Layout2D xmlns=\"http://waher.se/Schema/Layout2D.xsd\"");
100 Output.WriteLine(" background=\"WhiteBackground\" pen=\"BlackPen\"");
101 Output.WriteLine(" font=\"Text\" textColor=\"Black\">");
102 Output.WriteLine(" <SolidPen id=\"BlackPen\" color=\"Black\" width=\"1px\"/>");
103 Output.WriteLine(" <SolidBackground id=\"WhiteBackground\" color=\"WhiteSmoke\"/>");
104
105 foreach (string Label in Labels)
106 {
107 SKColor Color = Palette[i++];
108
109 Output.Write(" <SolidBackground id=\"");
110 Output.Write(Label);
111 Output.Write("Bg\" color=\"");
112 Output.Write(Model.ToString(Color));
113 Output.WriteLine("\"/>");
114 }
115
116 Output.WriteLine(" <Font id=\"Text\" name=\"Arial\" size=\"8pt\" color=\"Black\"/>");
117 Output.WriteLine(" <Grid columns=\"2\">");
118
119 foreach (string Label in Labels)
120 {
121 Output.WriteLine(" <Cell>");
122 Output.WriteLine(" <Margins left=\"1mm\" top=\"1mm\" bottom=\"1mm\" right=\"1mm\">");
123 Output.Write(" <RoundedRectangle radiusX=\"1mm\" radiusY=\"1mm\" width=\"5mm\" height=\"5mm\" fill=\"");
124 Output.Write(Label);
125 Output.Write("Bg");
126 Output.WriteLine("\"/>");
127 Output.WriteLine(" </Margins>");
128 Output.WriteLine(" </Cell>");
129 Output.WriteLine(" <Cell>");
130 Output.WriteLine(" <Margins left=\"0.5em\" right=\"0.5em\">");
131 Output.Write(" <Label text=\"");
132 Output.Write(Label);
133 Output.WriteLine("\" x=\"0%\" y=\"50%\" halign=\"Left\" valign=\"Center\" font=\"Text\"/>");
134 Output.WriteLine(" </Margins>");
135 Output.WriteLine(" </Cell>");
136 }
137
138 Output.WriteLine(" </Grid>");
139 Output.WriteLine("</Layout2D>");
140 Output.WriteLine("```");
141 Output.WriteLine();
142 }
143
151 public override bool ExportGraphScript(StreamWriter Output, string CustomColor, bool Span)
152 {
153 SKColor[] Palette = Model.CreatePalette(this.count);
155 int i = 0;
156 bool First = true;
157
158 Output.WriteLine("G:=Sum([(");
159
160 foreach (Source Source in this.sources)
161 {
162 Graph = this.GetGraph(Source.Reference);
163 if (Graph is null)
164 Log.Error("Graph for " + Source.Reference + " could not be found.");
165 else
166 {
167 if (!First)
168 Output.WriteLine("), (");
169
170 if (Graph.ExportGraphScript(Output, Model.ToString(Palette[i]), Span && this.span))
171 First = false;
172 }
173
174 i++;
175 }
176
177 Output.WriteLine(")]);");
178 Output.Write("G.LabelX:=\"Time × ");
179 Output.Write(Model.TimeUnitStr);
180 Output.WriteLine("\";");
181 Output.Write("G.Title:=\"");
182 Output.Write(this.title.Replace("\"", "\\\""));
183 Output.WriteLine("\";");
184 Output.WriteLine("G");
185
186 return true;
187 }
188
194 public abstract IGraph GetGraph(string Reference);
195 }
196}
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
string TimeUnitStr
Time unit string
Definition: Model.cs:125
Abstract base class for combined graphs
virtual void Register(ISource Source)
Registers a source.
override bool ExportGraphScript(StreamWriter Output, string CustomColor, bool Span)
Exports the graph to a markdown output.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
CombinedGraph(ISimulationNode Parent, Model Model)
Abstract base class for combined graphs
override void ExportGraph(StreamWriter Output)
Exports the graph to a markdown output.
static void ExportLegend(StreamWriter Output, string[] Labels, params SKColor[] Palette)
Exports a legend.
abstract IGraph GetGraph(string Reference)
Gets a graph from its reference.
Abstract base class for graph nodes
Definition: Graph.cs:13
abstract bool ExportGraphScript(StreamWriter Output, string CustomColor, bool Span)
Exports the graph to a markdown output.
Graph source reference.
Definition: Source.cs:11
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:682
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Interface for graph nodes
Definition: IGraph.cs:10
Interface for nodes holding a source node