xref: /webtrees/resources/views/modules/statistics-chart/custom.phtml (revision 302fdb8d52cb4f4341c6cc9dc09d1eb0794caab6)
1<?php
2
3use Fisharebest\Webtrees\Http\RequestHandlers\AutoCompleteSurname;
4use Fisharebest\Webtrees\I18N;
5use Fisharebest\Webtrees\Module\StatisticsChartModule;
6use Fisharebest\Webtrees\Tree;
7use Fisharebest\Webtrees\View;
8
9/**
10 * @var StatisticsChartModule $module
11 * @var Tree                  $tree
12 */
13
14?>
15
16<div class="container pt-3">
17    <h4 class="border-bottom p-2 mb-4">
18        <?= I18N::translate('Create your own chart') ?>
19    </h4>
20
21    <form method="post" action="<?= e(route('module', ['module' => $module->name(), 'action' => 'CustomChart', 'tree' => $tree->name()])) ?>" id="own-stats-form" class="wt-page-options wt-page-options-statistics">
22        <?= csrf_field() ?>
23
24        <div class="row mb-3">
25            <div class="col-sm-2 wt-page-options-label">
26                <?= I18N::translate('Chart type') ?>
27            </div>
28
29            <div class="col-sm-4 wt-page-options-value">
30                <fieldset>
31                    <legend><?= I18N::translate('Individual') ?></legend>
32
33                    <?= view('components/radios', [
34                        'name' => 'x-as', 'selected' => $module::X_AXIS_BIRTH_MONTH, 'options' => [
35                            $module::X_AXIS_BIRTH_MONTH           => I18N::translate('Month of birth'),
36                            $module::X_AXIS_DEATH_MONTH           => I18N::translate('Month of death'),
37                            $module::X_AXIS_FIRST_CHILD_MONTH     => I18N::translate('Month of birth of first child in a relation'),
38                            $module::X_AXIS_AGE_AT_DEATH          => I18N::translate('Average age at death'),
39                            $module::X_AXIS_AGE_AT_MARRIAGE       => I18N::translate('Age in year of marriage'),
40                            $module::X_AXIS_AGE_AT_FIRST_MARRIAGE => I18N::translate('Age in year of first marriage'),
41
42                        ],
43                    ]) ?>
44                </fieldset>
45
46                <fieldset>
47                    <legend><?= I18N::translate('Family') ?></legend>
48
49                    <?= view('components/radios', [
50                        'name' => 'x-as', 'selected' => $module::X_AXIS_BIRTH_MONTH, 'options' => [
51                            $module::X_AXIS_MARRIAGE_MONTH       => I18N::translate('Month of marriage'),
52                            $module::X_AXIS_FIRST_MARRIAGE_MONTH => I18N::translate('Month of first marriage'),
53                            $module::X_AXIS_NUMBER_OF_CHILDREN   => I18N::translate('Number of children'),
54
55                        ],
56                    ]) ?>
57                </fieldset>
58
59                <fieldset>
60                    <legend><?= I18N::translate('Map') ?></legend>
61
62                    <?= view('components/radios', [
63                        'name' => 'x-as', 'selected' => $module::X_AXIS_BIRTH_MONTH, 'options' => [
64                            $module::X_AXIS_INDIVIDUAL_MAP => I18N::translate('Individuals'),
65                            $module::X_AXIS_BIRTH_MAP      => I18N::translate('Births'),
66                            $module::X_AXIS_MARRIAGE_MAP   => I18N::translate('Marriages'),
67                            $module::X_AXIS_DEATH_MAP      => I18N::translate('Deaths'),
68                        ],
69                    ]) ?>
70                </fieldset>
71            </div>
72
73            <div class="col-sm-2 wt-page-options-label">
74                <?= I18N::translate('Details') ?>
75            </div>
76
77            <div class="col-sm-4 wt-page-options-value">
78                <fieldset id="axes">
79                    <legend><?= I18N::translate('Categories') ?></legend>
80
81                    <label>
82                        <input type="radio" id="z_none" name="z-as" value="<?= $module::Z_AXIS_ALL ?>" onclick="statusDisable('z-axis-boundaries-periods');">
83                        <?= I18N::translate('none') ?>
84                    </label>
85                    <br>
86                    <label>
87                        <input type="radio" id="z_sex" name="z-as" value="<?= $module::Z_AXIS_SEX ?>" onclick="statusDisable('z-axis-boundaries-periods');">
88                        <?= I18N::translate('gender') ?>
89                    </label>
90                    <br>
91                    <label>
92                        <input type="radio" id="z_time" name="z-as" value="<?= $module::Z_AXIS_TIME ?>" checked onclick="statusEnable('z-axis-boundaries-periods');">
93                        <?= I18N::translate('date periods') ?>
94                    </label>
95                    <label for="z-axis-boundaries-periods" class="visually-hidden">
96                        <?= I18N::translate('Date range') ?>
97                    </label>
98                    <select id="z-axis-boundaries-periods" class="form-control" name="z-axis-boundaries-periods">
99                        <option value="1700,1750,1800,1850,1900,1950,2000" selected>
100                            <?= /* I18N: from 1700 interval 50 years */
101                            I18N::plural('from %1$s interval %2$s year', 'from %1$s interval %2$s years', 50, I18N::digits(1700), I18N::number(50)) ?>
102                        </option>
103                        <option value="1800,1840,1880,1920,1950,1970,2000">
104                            <?= I18N::plural('from %1$s interval %2$s year', 'from %1$s interval %2$s years', 40, I18N::digits(1800), I18N::number(40)) ?>
105                        </option>
106                        <option value="1800,1850,1900,1950,2000">
107                            <?= I18N::plural('from %1$s interval %2$s year', 'from %1$s interval %2$s years', 50, I18N::digits(1800), I18N::number(50)) ?>
108                        </option>
109                        <option value="1900,1920,1940,1960,1980,1990,2000">
110                            <?= I18N::plural('from %1$s interval %2$s year', 'from %1$s interval %2$s years', 20, I18N::digits(1900), I18N::number(20)) ?>
111                        </option>
112                        <option value="1900,1925,1950,1975,2000">
113                            <?= I18N::plural('from %1$s interval %2$s year', 'from %1$s interval %2$s years', 25, I18N::digits(1900), I18N::number(25)) ?>
114                        </option>
115                        <option value="1940,1950,1960,1970,1980,1990,2000">
116                            <?= I18N::plural('from %1$s interval %2$s year', 'from %1$s interval %2$s years', 10, I18N::digits(1940), I18N::number(10)) ?>
117                        </option>
118                    </select>
119                </fieldset>
120
121                <fieldset id="zyaxes">
122                    <legend><?= I18N::translate('Results') ?></legend>
123
124                    <label>
125                        <input type="radio" name="y-as" value="<?= $module::Y_AXIS_NUMBERS ?>" checked>
126                        <?= I18N::translate('numbers') ?>
127                    </label>
128                    <br>
129                    <label>
130                        <input type="radio" name="y-as" value="<?= $module::Y_AXIS_PERCENT ?>">
131                        <?= I18N::translate('percentage') ?>
132                    </label>
133                </fieldset>
134
135                <fieldset id="x_years" style="display:none;">
136                    <legend><?= I18N::translate('Age interval') ?></legend>
137
138                    <label for="x-axis-boundaries-ages" class="visually-hidden">
139                        <?= I18N::translate('Age interval') ?>
140                    </label>
141                    <?= view('components/select', ['name' => 'x-axis-boundaries-ages', 'selected' => '1,5,10,20,30,40,50,60,70,80,90,100', 'options' => ['1,5,10,20,30,40,50,60,70,80,90,100' => I18N::plural('%s year', '%s years', 10, I18N::number(10)), '5,20,40,60,75,80,85,90' => I18N::plural('%s year', '%s years', 20, I18N::number(20)), '10,25,50,75,100' => I18N::plural('%s year', '%s years', 25, I18N::number(25))]]) ?>
142                </fieldset>
143
144                <fieldset id="x_years_m" style="display:none;">
145                    <legend><?= I18N::translate('Age interval') ?></legend>
146
147                    <label for="x-axis-boundaries-ages_m" class="visually-hidden">
148                        <?= I18N::translate('Select the desired age interval') ?>
149                    </label>
150                    <?= view('components/select', ['name' => 'x-axis-boundaries-ages_m', 'selected' => '16,18,20,22,24,26,28,30,32,35,40,50', 'options' => ['16,18,20,22,24,26,28,30,32,35,40,50' => I18N::plural('%s year', '%s years', 2, I18N::number(2)), '20,25,30,35,40,45,50' => I18N::plural('%s year', '%s years', 5, I18N::number(5))]]) ?>
151                </fieldset>
152
153                <div id="map_opt" style="display:none;">
154                    <fieldset id="chart_type">
155                        <legend><?= I18N::translate('Chart type') ?></legend>
156
157                        <label for="chart_type" class="visually-hidden"><?= I18N::translate('Chart type') ?></label>
158                        <select name="chart_type" class="form-control" onchange="statusShowSurname(this);">
159                            <option value="indi_distribution_chart" selected>
160                                <?= I18N::translate('Individuals') ?>
161                            </option>
162                            <option value="surname_distribution_chart">
163                                <?= I18N::translate('Surnames') ?>
164                            </option>
165                        </select>
166
167                        <div id="surname_opt" class="mb-3 d-none;">
168                            <label for="SURN"><?= I18N::translate('Surname') ?></label>
169                            <input data-wt-autocomplete-url="<?= e(route(AutoCompleteSurname::class, ['tree' => $tree->name()])) ?>" autocomplete="off" class="form-control" type="text" id="SURN" name="SURN">
170                        </div>
171                    </fieldset>
172
173                    <fieldset>
174                        <legend id="label-area"><?= I18N::translate('Geographic area') ?></legend>
175
176                        <label for="chart_shows" class="visually-hidden"><?= I18N::translate('Geographic area') ?></label>
177                        <select id="chart_shows" class="form-control" name="chart_shows">
178                            <option value="world" selected>
179                                <?= I18N::translate('World') ?>
180                            </option>
181                            <option value="150">
182                                <?= I18N::translate('Europe') ?>
183                            </option>
184                            <option value="021">
185                                <?= I18N::translate('Northern America') ?>
186                            </option>
187                            <option value="005">
188                                <?= I18N::translate('South America') ?>
189                            </option>
190                            <option value="142">
191                                <?= I18N::translate('Asia') ?>
192                            </option>
193                            <option value="145">
194                                <?= I18N::translate('Middle East') ?>
195                            </option>
196                            <option value="002">
197                                <?= I18N::translate('Africa') ?>
198                            </option>
199                        </select>
200                    </fieldset>
201                </div>
202            </div>
203        </div>
204
205        <p>
206            <button type="submit" class="btn btn-primary">
207                <?= view('icons/save') ?>
208                <?= I18N::translate('show the chart') ?>
209            </button>
210        </p>
211    </form>
212
213    <div id="custom-chart" class="wt-ajax-load">
214        <!-- Not initially empty, to disable spinner -->
215    </div>
216</div>
217
218<?php View::push('javascript') ?>
219<script>
220    function statusDisable(sel) {
221        var cbox      = document.getElementById(sel);
222        cbox.checked  = false;
223        cbox.disabled = true;
224    }
225
226    function statusEnable(sel) {
227        var cbox      = document.getElementById(sel);
228        cbox.disabled = false;
229    }
230
231    function statusHide(sel) {
232        var box           = document.getElementById(sel);
233        box.style.display = "none";
234        var box_m         = document.getElementById(sel + "_m");
235        if (box_m) {
236            box_m.style.display = "none";
237        }
238        if (sel === "map_opt") {
239            var box_axes = document.getElementById("axes");
240            if (box_axes) {
241                box_axes.style.display = "";
242            }
243            var box_zyaxes = document.getElementById("zyaxes");
244            if (box_zyaxes) {
245                box_zyaxes.style.display = "";
246            }
247        }
248    }
249
250    function statusShow(sel) {
251        var box           = document.getElementById(sel);
252        box.style.display = "";
253        var box_m         = document.getElementById(sel + "_m");
254        if (box_m) {
255            box_m.style.display = "none";
256        }
257        if (sel === "map_opt") {
258            var box_axes = document.getElementById("axes");
259            if (box_axes) {
260                box_axes.style.display = "none";
261            }
262            var box_zyaxes = document.getElementById("zyaxes");
263            if (box_zyaxes) {
264                box_zyaxes.style.display = "none";
265            }
266        }
267    }
268
269    function statusChecked(sel) {
270        var cbox     = document.getElementById(sel);
271        cbox.checked = true;
272    }
273
274    function statusShowSurname(x) {
275        if (x.value === "surname_distribution_chart") {
276            document.getElementById("surname_opt").style.display = "";
277        } else if (x.value !== "surname_distribution_chart") {
278            document.getElementById("surname_opt").style.display = "none";
279        }
280    }
281
282    function loadCustomChart() {
283        $("#custom-chart").html("");
284        var form = $("#own-stats-form");
285        jQuery.post(form.attr("action"), form.serialize())
286            .done(function (data) {
287                $("#custom-chart").html(data);
288            })
289            .fail(function (jqXHR, textStatus) {
290                // Server error?  Show something to get rid of the spinner.
291                $("#custom-chart").html(textStatus);
292            });
293        return false;
294    }
295
296    $("[name='x-as']").change(function () {
297        var x_axis = document.querySelector("[name='x-as']:checked").value;
298
299        switch (x_axis) {
300            case "<?= e($module::X_AXIS_BIRTH_MONTH) ?>":
301            case "<?= e($module::X_AXIS_DEATH_MONTH) ?>":
302            case "<?= e($module::X_AXIS_FIRST_CHILD_MONTH) ?>":
303                statusEnable("z_sex");
304                statusHide("x_years");
305                statusHide("map_opt");
306                break;
307
308            case "<?= e($module::X_AXIS_AGE_AT_DEATH) ?>":
309                statusEnable("z_sex");
310                statusShow("x_years");
311                statusHide("map_opt");
312                break;
313
314            case "<?= e($module::X_AXIS_AGE_AT_MARRIAGE) ?>":
315            case "<?= e($module::X_AXIS_AGE_AT_FIRST_MARRIAGE) ?>":
316                statusEnable("z_sex");
317                statusHide("x_years");
318                statusShow("x_years_m");
319                statusHide("map_opt");
320                break;
321
322            case "<?= e($module::X_AXIS_MARRIAGE_MONTH) ?>":
323            case "<?= e($module::X_AXIS_FIRST_MARRIAGE_MONTH) ?>":
324            case "<?= e($module::X_AXIS_NUMBER_OF_CHILDREN) ?>":
325                statusChecked("z_none");
326                statusDisable("z_sex");
327                statusHide("x_years");
328                statusHide("map_opt");
329                break;
330
331            case "<?= e($module::X_AXIS_INDIVIDUAL_MAP) ?>":
332                statusHide("x_years");
333                statusShow("map_opt");
334                statusShow("chart_type");
335                break;
336
337            case "<?= e($module::X_AXIS_BIRTH_MAP) ?>":
338            case "<?= e($module::X_AXIS_MARRIAGE_MAP) ?>":
339            case "<?= e($module::X_AXIS_DEATH_MAP) ?>":
340                statusHide("x_years");
341                statusShow("map_opt");
342                statusHide("chart_type");
343                statusHide("surname_opt");
344                break;
345        }
346    });
347
348    $("#own-stats-form").on("submit", loadCustomChart);
349    webtrees.autocomplete('#SURN');
350</script>
351<?php View::endpush() ?>
352