Pixiv ranking scatter plot

Show a scatter plot of date versus rank

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Pixiv ranking scatter plot
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Show a scatter plot of date versus rank
// @author       cro
// @match        https://www.pixiv.net/dashboard/report/ranking*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @grant        none
// @license      MIT
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-adapter-date-fns.bundle.min.js
// ==/UserScript==
/* jshint esversion: 6 */

(function() {
    'use strict';
    let zipwith = (a, b, f) => a.map((k, i) => f(k, b[i]));
    let jpcn_regex = /(\d+)\D(\d+)\D(\d+)\D/;
    let rank_regex = /\d+/;
    let date_parse = function(str)
    {
        let match = str.match(jpcn_regex);
        if (match)
        {
            str = `${match[1]} ${match[2]} ${match[3]}`;
        }
        return new Date(str);
    };

    let menubar = document.querySelector('section.analytics-menu-unit');
    let div = document.createElement('div');
    let canvas = document.createElement('canvas');
    canvas.style.backgroundColor = 'white';
    div.appendChild(canvas);
    menubar.insertAdjacentElement('beforeBegin', div);
    let dates = Array.from(document.querySelectorAll('h1.date')).map(x => date_parse(x.textContent));
    let top_rank = function(n)
    {
        let crown = document.querySelector(`.crown${n}`);
        return crown ? Array.from(crown.nextSibling.querySelectorAll('h1.date')).map(x => n) : [];
    }
    let top_ranks = [1,2,3].map(top_rank).flat();
    let ranks = top_ranks.concat(Array.from(document.querySelectorAll('h1.rank')).map(x => parseInt(x.textContent.match(rank_regex))));
    let data = {
        datasets: [{
            label: 'Date vs Rank',
            data: zipwith(dates, ranks, (date, rank) => ({ x: date, y: rank })),
            backgroundColor: 'black'
        }]
    };
    let config = {
        type: 'scatter',
        data: data,
        options: {
            animation: false,
            scales: {
                x: {
                    type: 'time',
                    display: true,
                    title: {
                        display: true,
                        text: 'Date'
                    }
                },
                y: {
                    display: true,
                    title: {
                        display: true,
                        text: 'Rank'
                    },
                    reverse: true,
                    min: 0,
                    suggestedMax: 100,
                }
            }
        }
    };
    let chart = new Chart(canvas, config);
})();