• Jump To … +
    ./demo/modules/canvas-minimap.js ./demo/modules/canvas-scene-editor.js ./demo/modules/demo-m006-c1.js ./demo/modules/demo-m006-c2.js ./demo/modules/demo-m006-c3.js ./demo/modules/demo-m006-c4.js ./demo/modules/demo-m006-utils.js ./demo/modules/dom-entity-editor.js ./demo/modules/entity-copy-paste.js ./demo/modules/entity-manipulation-gui.js ./demo/modules/entity-navigation.js ./demo/modules/entity-ring-builder.js ./demo/modules/london-crime-lines.js ./demo/modules/london-crime-stacked-bars.js ./demo/modules/lottie-loader.js ./demo/modules/simple-chart-frame.js ./demo/modules/simple-graph-lines.js ./demo/modules/simple-graph-stacked-bars.js ./demo/modules/wikipedia-views-spiral-chart.js ./demo/snippets/animated-highlight-gradient-text-snippet.js ./demo/snippets/animated-hover-gradient-snippet.js ./demo/snippets/animated-word-gradient-snippet.js ./demo/snippets/before-after-slider-infographic.js ./demo/snippets/bubbles-text-snippet.js ./demo/snippets/green-box-snippet.js ./demo/snippets/jazzy-button-snippet.js ./demo/snippets/page-performance-snippet-test.js ./demo/snippets/page-performance-snippet.js ./demo/snippets/pan-image-snippet.js ./demo/snippets/placeholder-effect-snippet.js ./demo/snippets/ripple-effect-snippet.js ./demo/snippets/risograph-text-gradient-snippet.js ./demo/snippets/spotlight-text-snippet-test.js ./demo/snippets/spotlight-text-snippet.js ./demo/snippets/swirling-stripes-text-snippet.js ./demo/snippets/text-snippet-helper.js ./demo/snippets/word-highlighter-snippet.js ./demo/snippets/worley-text-gradient-snippet.js
  • §

    Demo Modules 001

    Scrawl-canvas modularized code - London crime charts

    Related files:

    • London crime charts - main module
    • London crime lines module
    • London crime stacked bar module
    • Simple chart frame module
    • Simple graph lines module
    • Simple graph stacked bars module
    /*
    Data format requirements - a Javascript object with structure:
    
    {
        area:           'area-label',
        yearLabels:     ['year-1-label', 'year-2-label', ...],
        categoryLabels: ['category-1-label', 'category-2-label', ...],
        yearData: {
            'year-1-label': ["category-1-data", "category-2-data", ...],
            'year-2-label': ["category-1-data", "category-2-data", ...],
            ...
        }
    }
    */
  • §

    Imports

    We need to adapt the graph frame with data specific to this graph

    import { 
        api as frame,
        dims as frameDims,
    } from './simple-chart-frame.js';
  • §

    Helper functionality

    Calculate height of the largest year

    • Rounded up to nearest thousand
    const extractHighestAnnualMaximum = (yearLabels, yearData) => {
    
        let max = 0;
    
        for (let i = 0; i < yearLabels.length; i++) {
    
            let total = yearData[yearLabels[i]].reduce((a, v) => a + v, 0);
            if (total > max) max = total;
        }
        return ((Math.floor(max / 1000)) * 1000) + 1000;
    };
    
    const colorArray = ['#257394', '#947a25', '#fc0004', '#00fcbd', '#de9bc8', '#9bdeaf', '#970b99', '#0b9910', '#5f11f0', '#66f011', '#a09de0', '#d1e09d'];
  • §

    API - exported functions

    export const api = {};
  • §

    The exported build function

    api.build = function (items) {
    
        const { namespace, canvas, data, scrawl } = items;
  • §

    Module state

        let selectedColumn = 0,
            selectedRow = 0,
            currentData = data;
    
        if (namespace && canvas && data && scrawl) {
  • §

    Namespace boilerplate

            const name = (item) => `${namespace}-${item}`;
  • §

    Local variables defined at the top of the build function

            const area = data.area,
                yearLabels = data.yearLabels, 
                categoryLabels = data.categoryLabels, 
                yearData = data.yearData,
    
                gap = 1,
  • §

    Magic numbers

                graphWidth = frameDims.graphWidth,
                graphHeight = frameDims.graphHeight,
                graphBottom = frameDims.graphBottom,
                graphLeft = frameDims.graphLeft + (gap / 2),
  • §

    Graph baseline calculations

                maximumBarTotal = extractHighestAnnualMaximum(yearLabels, yearData),
                numberOfYears = yearLabels.length,
                barDistance = graphWidth / numberOfYears,
                barWidth = `${barDistance - gap}%`,
                singleCrimeHeight = graphHeight / maximumBarTotal;
  • §

    Create group

            const group = scrawl.makeGroup({
    
                name: name('bargroup'),
                host: canvas.get('baseName'),
                order: 2,
            });
  • §

    Build bars

            yearLabels.forEach((year, yearIndex) => {
    
                let xPosition = (barDistance * yearIndex) + graphLeft,
                    localHeight = 0;
    
                const categoryLen = categoryLabels.length;
    
                categoryLabels.forEach((category, categoryIndex) => {
    
                    const categoryItem = yearData[year][categoryIndex],
                        crimeHeight = categoryItem * singleCrimeHeight;
    
                    localHeight += crimeHeight;
    
                    scrawl.makeBlock({
    
                        name: name(`${year}-${category}`),
                        group,
    
                        width: barWidth,
                        height: `${crimeHeight}%`,
    
                        startX: `${xPosition}%`,
                        startY: `${graphBottom - localHeight}%`,
    
                        fillStyle: colorArray[categoryIndex % categoryLen],
    
                        onEnter: function () {
    
                            selectedColumn = yearIndex;
                            selectedRow = categoryIndex;
    
                            updateSelected();
                        },
                    });
                });
            });
  • §

    Build highlight cursor

            const currentDatapoint = scrawl.makeBlock({
                name: name('dataframe'),
                group,
                mimic: name(`${selectedColumn}-${selectedRow}`),
                useMimicDimensions: true,
                useMimicStart: true,
                useMimicHandle: true,
                lockTo: 'mimic',
                lineWidth: 6,
                strokeStyle: 'yellow',
                method: 'draw',
            });
    
            const updateSelected = () => {
    
                const {yearLabels, categoryLabels, yearData} = currentData;
    
                const category = categoryLabels[selectedRow],
                    year = yearLabels[selectedColumn],
                    data = yearData[year][selectedRow];
    
                currentDatapoint.set({
                    mimic: name(`${year}-${category}`),
                });
    
                frame.updateSubtitle(`${category} in ${year}: §RED§${data.toLocaleString()}`);
            };
    
            updateSelected();
  • §

    Personalize the chart frame to meet this graph’s requirements

            frame.updateSubtitle('No data selected');
            frame.updateXLeft(yearLabels[0]);
            frame.updateXRight(yearLabels[numberOfYears - 1]);
            frame.updateYTop(maximumBarTotal.toLocaleString());
  • §

    Accessibility

            const doNavigation = (direction) => {
    
                const {yearLabels, categoryLabels} = currentData;
    
                const columnLen = yearLabels.length,
                    rowLen = categoryLabels.length;
    
                switch (direction) {
    
                    case 'up' : 
                        selectedRow++;
                        break;
    
                    case 'down' : 
                        selectedRow--;
                        break;
    
                    case 'left' : 
                        selectedColumn--;
                        break;
    
                    case 'right' : 
                        selectedColumn++;
                        break;
                }
    
                if (selectedColumn < 0) selectedColumn = columnLen - 1;
                else if (selectedColumn >= columnLen) selectedColumn = 0;
    
                if (selectedRow < 0) selectedRow = rowLen - 1;
                else if (selectedRow >= rowLen) selectedRow = 0;
    
                updateSelected();
            };
    
            frame.keyboard = scrawl.makeKeyboardZone({
    
                zone: canvas,
    
                none: {
                    ArrowLeft: () => doNavigation('left'),
                    ArrowUp: () => doNavigation('up'),
                    ArrowRight: () => doNavigation('right'),
                    ArrowDown: () => doNavigation('down'),
                },
            });
        }
    };