• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Simple Bar Graph using Chart.js Library Tutorial

erty

Manga Panel Translator
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
35
Likes
104
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we will tackle about how to create a Bar Graph using Chart.js. Chart.js is an open-source javascript library that focuses in generating a graphical canvases such as Line , Bar, and Pie Graphs. Here, I will be providin a source code that demonstrates how to generate a simple Bar Graph using the said javascript library.

The Bar Graph that I will show is a simple Annual Report within 3 years that has 3 types of data set each year which are the total Sales, Expenses, and Profit. I will be using Bootstrap page user-interface/design.

Getting Started

In this tutorial source code, I usedBootstrap v5 for the design of application, jQuery Library, and the most important is Chart.js Library.

Creating the Interface

First, I created an HTML scripts which contains the codes of the application interface. I save this file as index.html.

The code contains a simple top navigation bar, canvas tag where the graph will be shown, and a button for generate a new random data of the graph.

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >

  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    Chart.js</

    title

    >
  8. <link

    rel

    =

    "stylesheet"

    href

    =

    "./css/bootstrap.min.css"

    >
  9. <script

    src

    =

    "./js/jquery-3.6.0.min.js"

    ></

    script

    >
  10. <script

    src

    =

    "./js/bootstrap.min.js"

    ></

    script

    >
  11. <script

    src

    =

    "./js/chart.min.js"

    ></

    script

    >
  12. <script

    src

    =

    "./js/script.js"

    ></

    script

    >
  13. <style

    >
  14. :root {
  15. --bs-success-rgb: 71, 222, 152 !important;
  16. }

  17. html,
  18. body {
  19. height: 100%;
  20. width: 100%;
  21. font-family: Apple Chancery, cursive;
  22. }
  23. </

    style

    >
  24. </

    head

    >

  25. <body

    class

    =

    "bg-light"

    >
  26. <nav class

    =

    "navbar navbar-expand-lg navbar-dark bg-primary bg-gradient"

    id

    =

    "topNavBar"

    >
  27. <div

    class

    =

    "container"

    >
  28. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >
  29. Sourcecodester
  30. </

    a

    >
  31. </

    div

    >
  32. </

    nav>
  33. <div

    class

    =

    "container py-3"

    id

    =

    "page-container"

    >
  34. <small

    >

    How to Create a Bar Graph using Chart.js</

    small

    >
  35. <hr

    >

  36. <canvas id

    =

    "chartCanvas"

    class

    =

    "w-100"

    style

    =

    "height: 60vh"

    ></

    canvas>
  37. <center

    ><button

    class

    =

    "btn btn-sm btn-primary rounded-pill col-md-4"

    type

    =

    "button"

    onclick

    =

    "regenerate()"

    >

    Randomize Data</

    button

    ></

    center

    >
  38. </

    div

    >

  39. </

    body

    >

  40. </

    html

    >

Creating the Main Function

The following script is main script for the application which contains the Bar Charts Initilization, generating Randow Data values, and the function of Generating new Random Value of the Graph. I save this file as script.js.

  1. var

    ctx,
  2. myChart;
  3. // Simple Function for generating a Ranfom value
  4. function

    randomNumber(

    )

    {
  5. return

    Math

    .random

    (

    )

    *

    (

    String

    (

    999999999

    )

    .substring

    (

    Math

    .floor

    (

    Math

    .random

    *

    9

    )

    +

    3

    )

    )
  6. }

  7. // Function for Generate a New Ranom Data of the Graph
  8. function

    regenerate(

    )

    {
  9. // looping the number of chart's data sets
  10. Object

    .keys

    (

    myChart.data

    .datasets

    )

    .map

    (

    k =>

    {
  11. var

    set

    =

    myChart.data

    .datasets

    [

    k]

    ;
  12. // Updating the data values
  13. set

    .data

    =

    [

    randomNumber(

    )

    ,

    randomNumber(

    )

    ,

    randomNumber(

    )

    ]
  14. }

    )

  15. // Triggers Chart to display the updated Data
  16. myChart.update

    (

    )
  17. }
  18. $(

    function

    (

    )

    {
  19. // Canvas Element where to display the Bar Graph
  20. ctx =

    $(

    '#chartCanvas'

    )

    ;
  21. // Initializing the bar graph data and options
  22. myChart =

    new

    Chart(

    ctx,

    {
  23. type:

    'bar'

    ,
  24. data:

    {
  25. labels:

    [

    '2017'

    ,

    '2018'

    ,

    '2019'

    ]

    ,
  26. datasets:

    [

    {
  27. label:

    'Sales'

    ,
  28. data:

    [

    randomNumber(

    )

    ,

    randomNumber(

    )

    ,

    randomNumber(

    )

    ]

    ,
  29. backgroundColor:

    "#1a93f0"
  30. }

    ,

    {
  31. label:

    'Expense'

    ,
  32. data:

    [

    randomNumber(

    )

    ,

    randomNumber(

    )

    ,

    randomNumber(

    )

    ]

    ,
  33. backgroundColor:

    "#e85454"
  34. }

    ,

    {
  35. label:

    'Profit'

    ,
  36. data:

    [

    randomNumber(

    )

    ,

    randomNumber(

    )

    ,

    randomNumber(

    )

    ]

    ,
  37. backgroundColor:

    "#74f29a"
  38. }

    ]
  39. }

    ,
  40. options:

    {
  41. scales:

    {
  42. y:

    {
  43. beginAtZero:

    true

    ,
  44. ticks:

    {
  45. // Customizing Y Axes Label
  46. callback:

    function

    (

    value)

    {
  47. parseFloat(

    value)
  48. if

    (

    value >=

    100000

    &&

    value <

    1000000

    )

    {
  49. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    3

    )

    +

    "K"
  50. }

    else

    if

    (

    value >=

    1000000

    &&

    value <

    1000000000

    )

    {
  51. if

    (

    String

    (

    value)

    .length

    ==

    7

    )
  52. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    1

    )

    +

    "M"
  53. if

    (

    String

    (

    value)

    .length

    ==

    8

    )
  54. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    2

    )

    +

    "M"
  55. if

    (

    String

    (

    value)

    .length

    ==

    9

    )
  56. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    3

    )

    +

    "M"
  57. }

    else

    if

    (

    value >=

    1000000000

    &&

    value <=

    1000000000000

    )

    {
  58. if

    (

    String

    (

    value)

    .length

    ==

    10

    )
  59. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    1

    )

    +

    "B"
  60. if

    (

    String

    (

    value)

    .length

    ==

    11

    )
  61. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    2

    )

    +

    "B"
  62. if

    (

    String

    (

    value)

    .length

    ==

    12

    )
  63. value =

    String

    (

    value)

    .substring

    (

    0

    ,

    3

    )

    +

    "B"
  64. }

    else

    {
  65. value =

    (

    value)

    .toLocaleString

    (

    'en-US'

    )
  66. }

  67. return

    "$ "

    +

    value;
  68. }
  69. }
  70. }
  71. }

    ,
  72. plugins:

    {
  73. legend:

    {
  74. display:

    true
  75. }

    ,
  76. title:

    {
  77. display:

    true

    ,
  78. text:

    'Annual Report'
  79. }
  80. }
  81. }
  82. }

    )

    ;
  83. }

    )

Result

bar-chart_0.png


DEMO VIDEO

That's the end of this tutorial. I hope this tutorial will help you and your future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,690

323,699

Top