1#
2# francy: Interactive Discrete Mathematics in GAP
3#
4
5#############################################################################
6##
7#M  Chart( <chart type> ) .
8##
9InstallMethod(Chart,
10  "a chart type, chart defaults",
11  true,
12  [IsChartType,
13   IsChartDefaults],
14  0,
15function(chartType, options)
16  return MergeObjects(Objectify(ChartObjectType, rec(
17    id   := GenerateID(),
18    data := rec(),
19    axis := DefaultAxis(chartType),
20    type := chartType!.value,
21  )), options);
22end);
23
24InstallOtherMethod(Chart,
25  "a Chart type",
26  true,
27  [IsChartType],
28  0,
29function(chartType)
30  return Chart(chartType, ChartDefaults);
31end);
32
33#############################################################################
34##
35#M  DefaultAxis( <chart type> ) .
36##
37InstallMethod(DefaultAxis,
38  "a chart type",
39  true,
40  [IsChartType],
41  0,
42function(chartType)
43  local axis;
44  axis := rec();
45  # default is linear
46  axis.x := XAxis(AxisScaleType.LINEAR, "", []);
47  axis.y := YAxis(AxisScaleType.LINEAR, "", []);
48  if chartType!.value = ChartType.BAR!.value then
49    axis.x := XAxis(AxisScaleType.BAND, "", []);
50  fi;
51  return axis;
52end);
53
54#############################################################################
55##
56#M  Add( <chart>, <francy object> ) . . . . . add objects to canvas
57##
58InstallOtherMethod(Add,
59  "a Chart, a dataset",
60  true,
61  [IsChart,
62   IsDataset],
63  0,
64function(chart, object)
65  chart!.data!.(object!.title) := object!.data;
66  return chart;
67end);
68
69InstallOtherMethod(Add,
70  "a Chart, a list of francy objects",
71  true,
72  [IsChart,
73   IsList],
74  0,
75function(chart, objects)
76  local object;
77  for object in objects do
78    Add(chart, object);
79  od;
80  return chart;
81end);
82
83#############################################################################
84##
85#M  Remove( <chart>, <francy object> ) . . . . . remove object from canvas
86##
87InstallOtherMethod(Remove,
88  "a chart, a dataset",
89  true,
90  [IsChart,
91   IsDataset],
92  0,
93function(chart, object)
94  Unbind(chart!.data!.(object!.title));
95  return chart;
96end);
97
98InstallOtherMethod(Remove,
99  "a Chart, a list of francy objects",
100  true,
101  [IsChart,
102   IsList],
103  0,
104function(chart, objects)
105  local object;
106  for object in objects do
107    Remove(chart, object);
108  od;
109  return chart;
110end);
111
112#############################################################################
113##
114#M  Dataset( <title>, <list of data> ) . . . . . create a dataset
115##
116InstallMethod(Dataset,
117  "a title, a list of data",
118  true,
119  [IsString,
120   IsList],
121  0,
122function(title, list)
123  return Objectify(DatasetObjectType, rec(
124    title := title,
125    data  := list
126  ));
127end);
128
129#############################################################################
130##
131#M  XAxis( <axis scale type>, <title>, <domain range> )
132##
133InstallMethod(XAxis,
134  "the axis scale type, a title, a list of data",
135  true,
136  [IsAxisScaleType,
137   IsString,
138   IsList],
139  0,
140function(axisScale, title, list)
141  return Objectify(XAxisObjectType, rec(
142    scale  := axisScale!.value,
143    title  := title,
144    domain := list
145  ));
146end);
147
148InstallOtherMethod(XAxis,
149  "the axis scale type, a title",
150  true,
151  [IsAxisScaleType,
152   IsString],
153  0,
154function(axisScale, title)
155  return XAxis(axisScale, title, []);
156end);
157
158#############################################################################
159##
160#M  YAxis( <axis scale type>, <title>, <domain range> )
161##
162InstallMethod(YAxis,
163  "the axis scale type, a title, a list of data",
164  true,
165  [IsAxisScaleType,
166   IsString,
167   IsList],
168  0,
169function(axisScale, title, list)
170  return Objectify(YAxisObjectType, rec(
171    scale  := axisScale!.value,
172    title  := title,
173    domain := list
174  ));
175end);
176
177InstallOtherMethod(YAxis,
178  "the axis scale type, a title",
179  true,
180  [IsAxisScaleType,
181   IsString],
182  0,
183function(axisScale, title)
184  return YAxis(axisScale, title, []);
185end);
186