Giter VIP home page Giter VIP logo

gagniuc / world-smallest-js-chart-v1.0 Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 254 KB

This js implementation may be the smallest source code for a useful chart to date (to my knowledge)! The World smallest chart plots only positive values, namely it takes values from zero up to an upper bound. The projects in this repository show two js charts and both use the HTML5 canvas object.

License: MIT License

HTML 87.71% JavaScript 12.29%
chartjs chart plot ploty js native small shortcode graph graphics

world-smallest-js-chart-v1.0's Introduction

World smallest chart 1.0 in native JS

This js implementation may be the smallest source code for a useful chart to date (to my knowledge)! The World smallest chart plots only positive values, namely it takes values from zero up to an upper bound. To take into account the negative values, you can use the chart project that is shown here. The projects in this repository show two js charts and both use the HTML5 canvas object. The first one from file shortest_chart.html contains the shortest chart source code. Basically the implementation is represented by a function named Chart that draws some consecutive numeric values on a canvas object. The second chart found in file simplest_chart_with_axes.html contains an addition to the first, namely it adds the x-axis and y-axis, and the corresponding baseline ticks. There are also two implementations in the js folder that store the Chart function in a separate ".js" file. For more detailed information, note that these native Charts in Javascript, were published in the supplementary materials of the book entitled Algorithms in Bioinformatics: Theory and Implementation. The lines below show how this Chart function can be called:

Chart('23,45,66,77,44,33,99', '#ff0000', 'y');

Where the numbers are passed directly as a parameter to the function, or, a situation in which the numbers are stored in a signal variable which in turn is passed as a parameter to the function:

var signal = '23,45,66,77,44,33,99';
Chart(signal, '#ff0000', 'y');

A function Chart that draws signals on a canvas object is added here. This function is called for each signal separately, while the previous plots can be maintained. The Chart function receives three parameters. A parameter (q) containing the signal (n values – each value separated by a delimiter, namely ","). A second parameter is the color of the line identifying each signal (c). A third parameter (e) that indicates to the function whether the canvas object should be cleared or not (’y’ for clear/erase). Inside the Chart function, the maximum value above the input (s) is detected and this value is stored in the mx variable. World shortest Chart is shown below:

function Chart(q,c,e) {
    var s = q.split(",");
    var mx = Math.max.apply(null, s);
    var canvas = document.getElementById('bio');
    var w = canvas.width;
    var h = canvas.height;
    
    if (canvas.getContext) {
        var ctx = canvas.getContext('2d');
        if(e=='y'){ctx.clearRect(0, 0, w, h);}
 
        ctx.moveTo(0, 0);
        ctx.beginPath();
        
        for (var i=0; i<=s.length-1; i++){
            var y = h-((h / mx) * s[i]);
            var x = (w / s.length) * i;
            ctx.lineTo(x, y);
        }
        ctx.lineWidth = 2;
        ctx.strokeStyle = c;
        ctx.stroke();
    }
}
An example of output:

How does it work? The Chart function contains a loop that makes a number of iterations (i) equal to the number of terms present in the sequence (s). Inside the main loop, the coordinates above the canvas object are calculated based on the maximum value, namely according to the value found in the mx variable. Thus, the y-axis is represented by the height (h) of the canvas object divided by the value in the mx variable (h/mx), and the result is multiplied by the current value in the sequence (s[i]). To position the zero values at the bottom of the chart, the y-axis is reversed by subtracting the result (the y value) from the height (h) of the canvas object:

In contrast, the x-axis is calculated by dividing the length of the canvas object by the total number of terms in the sequence (w/s.length), and the result is multiplied by the iteration number (i):

Once the two values are computed, the line is drawn from the previous coordinates to the current x and y coordinates (ctx.lineTo(x, y);). This concludes the discussions related to the Chart function. There is also an addition to the chart function shown in the simplest_chart_with_axes.html file, namely the axis. This longer version can also be used for different applications:

A chart with axes:

Live demo: https://gagniuc.github.io/World-smallest-js-chart-v1.0/

References

  • Paul A. Gagniuc. Algorithms in Bioinformatics: Theory and Implementation. John Wiley & Sons, Hoboken, NJ, USA, 2021, ISBN: 9781119697961.

world-smallest-js-chart-v1.0's People

Contributors

gagniuc avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.