File size: 6,175 Bytes
da96562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*!
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

(function ($) {

var sparklineColorNames = ['backgroundColor', 'lineColor', 'minPointColor', 'maxPointColor', 'lastPointColor', 'fillColor'];

var sparklineDisplayHeight = 25;
var sparklineDisplayWidth = 100;

piwik.getSparklineColors = function () {
    var colors = piwik.ColorManager.getColors('sparkline-colors', sparklineColorNames);

    var comparisonService = window.CoreHome.ComparisonsStoreInstance;
    if (comparisonService.isComparing()) {
        var comparisons = comparisonService.getAllComparisonSeries();
        colors.lineColor = comparisons.map(function (comp) { return comp.color; });
    }

    return colors;
};

piwik.refreshSparklines = function () {
    $('.sparkline img').each(function () {
        var $self = $(this);
        var dataSrc = $self.attr('data-src');

        if (!dataSrc) {
            return;
        }

        var seriesIndices = $self.closest('.sparkline').data('series-indices');
        var sparklineColors = piwik.getSparklineColors();

        if (seriesIndices && sparklineColors.lineColor instanceof Array) {
            sparklineColors.lineColor = sparklineColors.lineColor.filter(function (c, index) {
                return seriesIndices.indexOf(index) !== -1;
            });
        }

        var colors = JSON.stringify(sparklineColors);
        var appendToSparklineUrl = '&colors=' + encodeURIComponent(colors);

        // The redesign lets sparklines be rendered at a custom size; without it we keep the
        // legacy behaviour of rendering at the server default and displaying at 100x25.
        var redesignEnabled = document.body.classList.contains('sparklines-redesign-enabled');
        var width = sparklineDisplayWidth;
        var height = sparklineDisplayHeight;

        if (redesignEnabled) {
            width = parseInt($self.attr('width'), 10) || sparklineDisplayWidth;
            height = parseInt($self.attr('height'), 10) || sparklineDisplayHeight;
            // Render the PNG at twice the displayed size so it stays crisp on hi-DPI screens
            // (matching the legacy 200x50-render / 100x25-display ratio).
            appendToSparklineUrl += '&width=' + encodeURIComponent(width * 2)
                + '&height=' + encodeURIComponent(height * 2);
        }

        // Append the token_auth to the URL if it was set (eg. embed dashboard)
        var token_auth = broadcast.getValueFromUrl('token_auth');
        if (token_auth.length && piwik.shouldPropagateTokenAuth) {
            appendToSparklineUrl += '&token_auth=' + token_auth;
        }

        $self.attr('width', width);
        $self.attr('height', height);
        $self.attr('src', dataSrc + appendToSparklineUrl);
    });
};

// initializes each sparkline so they use colors defined in CSS
piwik.initSparklines = function() {
    $(function () {
      piwik.refreshSparklines();
    });
};

window.addEventListener('themeModeChange', function () {
    piwik.refreshSparklines();
});

window.initializeSparklines = function () {
    $('.dataTableVizEvolution[data-report]').each(function () {
        var graph = $(this);

        // we search for .widget to make sure eg in the Dashboard to not update any graph of another report
        var selectorsToFindParent = ['.widget', '.widget-container', '.reporting-page', 'body'];
        var index = 0, selector, parent;
        for (index; index < selectorsToFindParent.length; index++) {
            selector = selectorsToFindParent[index];
            parent = graph.parents(selector).first();
            if (parent && parent.length) {
                break;
            }
        }

        if (!parent || !parent.length) {
            return;
        }

        var sparklines = parent.find('div.sparkline:not(.notLinkable)');

        // try to find sparklines and add them clickable behaviour
        sparklines.each(function () {
            // find the sparkline and get it's src attribute
            var sparklineUrl = $('img', this).attr('data-src');

            var $this = $(this);

            if (sparklineUrl != "") {

                $this.addClass('linked');

                var params = $this.data('graph-params') || {};
                if (!Object.keys(params).length) {
                    var urlParams = broadcast.getValuesFromUrl(sparklineUrl);

                    if (urlParams.columns) {
                        params.columns = decodeURIComponent(urlParams.columns);
                    }
                    if (urlParams.rows) {
                        params.rows = decodeURIComponent(urlParams.rows);
                    }
                    if (urlParams.idGoal) {
                        params.idGoal = decodeURIComponent(urlParams.idGoal);
                    }
                }

                // on click, reload the graph with the new url
                $this.off('click.sparkline');
                $this.on('click.sparkline', function () {
                    var reportId = graph.attr('data-report'),
                        dataTable = graph;

                    // when the metrics picker is used, the id of the data table might be updated (which is correct behavior).
                    // for example, in goal reports it might change from GoalsgetEvolutionGraph to GoalsgetEvolutionGraph1.
                    // if this happens, we can't find the graph using $('#'+idDataTable+"Chart");
                    // instead, we just use the first evolution graph we can find.
                    if (dataTable.length == 0) {
                        if ($(this).closest('.widget').length) {
                            dataTable = $(this).closest('.widget').find('div.dataTableVizEvolution');
                        } else {
                            dataTable = $('div.dataTableVizEvolution');
                        }
                    }

                    // reload the datatable w/ a new column & scroll to the graph
                    dataTable.trigger('reload', params);
                });
            }
        });
    });
};

}(jQuery));