Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

6.0 KB
Raw Blame
@using Constants = Grand.SharedUIResources.Constants
<div class="grand-dashboard__chart grand-dashboard__chart--wide">
    <canvas id="canvasReportOrderTimeChart" aria-label="@Loc["Admin.Dashboard.ReportOrderTime.TitleChart"]" role="img"></canvas>
</div>

<script src="@(Constants.WwwRoot)/administration/chart.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function () {
        initReportOrderTimeChart(true);
        $("#DarkLight").change(function () {
            initReportOrderTimeChart(true);
        });
    });

    //the colours of the panel: the accent of the form fields, the muted text and a faint rule
    function dashboardChartPalette() {
        var toggle = document.getElementById('DarkLight');
        var dark = toggle ? toggle.checked : document.documentElement.getAttribute('data-bs-theme') === 'dark';
        return dark
            ? { accent: '#66d9e8', accentFill: 'rgba(102, 217, 232, 0.55)', second: '#ffa94d', text: '#c9ced4', grid: 'rgba(255, 255, 255, 0.1)', zero: 'rgba(255, 255, 255, 0.3)' }
            : { accent: '#0b7285', accentFill: 'rgba(11, 114, 133, 0.7)', second: '#d9480f', text: '#5c6773', grid: 'rgba(15, 23, 42, 0.07)', zero: 'rgba(15, 23, 42, 0.25)' };
    }

    function initReportOrderTimeChart(first) {
        function additionalData() {
            var data = {
                startDate: $('#dashboard-report-range').data('daterangepicker').startDate.format('YYYY-MM-DD'),
                endDate: $('#dashboard-report-range').data('daterangepicker').endDate.format('YYYY-MM-DD'),
            };

            addAntiForgeryToken(data);
            return data;
        }

        var add = additionalData();

        $.ajax({
            type: "POST",
            url: "@Html.Raw(Url.Action("ReportOrderTimeChart", "Reports", new { area = Grand.Web.Admin.Extensions.Constants.AreaAdmin }))",
            dataType: "json",
            data: add,
            success: function (data) {
                var palette = dashboardChartPalette();
                //the report groups by day without ordering the groups
                var rows = (data.Data || []).slice().sort(function (a, b) { return String(a.Time).localeCompare(String(b.Time)); });

                var chartData = {
                    labels: rows.map(function (row) { return row.Time; }),
                    datasets: [{
                        type: 'line',
                        label: @Json.Serialize(Loc["Admin.Dashboard.ReportOrderTime.Count"].Value),
                        yAxisID: 'R',
                        borderColor: palette.second,
                        backgroundColor: palette.second,
                        pointBackgroundColor: palette.second,
                        pointRadius: 4,
                        pointHoverRadius: 6,
                        borderWidth: 2,
                        lineTension: 0.3,
                        fill: false,
                        data: rows.map(function (row) { return row.TotalOrders; })
                    }, {
                        type: 'bar',
                        label: @Json.Serialize(Loc["Admin.Dashboard.ReportOrderTime.TotalSales"].Value),
                        yAxisID: 'L',
                        backgroundColor: palette.accentFill,
                        hoverBackgroundColor: palette.accent,
                        borderWidth: 0,
                        maxBarThickness: 64,
                        data: rows.map(function (row) { return row.SumOrders; })
                    }]
                };

                var ctx = document.getElementById('canvasReportOrderTimeChart').getContext('2d');
                if (first) {
                    //the switch redraws the chart in the other scheme; the old one has to go, or
                    //both stay on the canvas and flicker under the pointer
                    if (window.myReportOrderTimeChart) window.myReportOrderTimeChart.destroy();
                    Chart.defaults.global.defaultFontFamily = getComputedStyle(document.body).fontFamily;
                    window.myReportOrderTimeChart = new Chart(ctx, {
                        type: 'bar',
                        data: chartData,
                        options: {
                            responsive: true,
                            maintainAspectRatio: false,
                            legend: {
                                position: 'bottom',
                                labels: { fontColor: palette.text, usePointStyle: true, padding: 16 }
                            },
                            tooltips: {
                                mode: 'index',
                                intersect: false
                            },
                            scales: {
                                xAxes: [{
                                    gridLines: { display: false },
                                    ticks: { fontColor: palette.text }
                                }],
                                yAxes: [{
                                    id: 'L',
                                    type: 'linear',
                                    position: 'left',
                                    gridLines: { color: palette.grid, zeroLineColor: palette.zero, drawBorder: false },
                                    ticks: { min: 0, fontColor: palette.text }
                                }, {
                                    id: 'R',
                                    type: 'linear',
                                    position: 'right',
                                    gridLines: { display: false, drawBorder: false },
                                    ticks: { min: 0, precision: 0, fontColor: palette.text }
                                }]
                            }
                        }
                    });
                } else {
                    window.myReportOrderTimeChart.data = chartData;
                    window.myReportOrderTimeChart.update();
                }

            },
            error: function () {
                alert('error');
            }
        });
    }
</script>