Chart.js – Useful Resources

Chart.js – Useful Resources ”; Previous Next The following resources contain additional information on Chart.js. Please use them to get more in-depth knowledge on this. Useful Video Courses JavaScript Masterclass: ES6 Modern Development 71 Lectures 4.5 hours Frahaan Hussain More Detail JavaScript fundamentals Course for Beginners: JavaScript ES6 37 Lectures 2 hours Laurence Svekis More Detail JavaScript Training Course – Practice building 5 applications 32 Lectures 2 hours Laurence Svekis More Detail JavaScript Objects and OOP Programming with JavaScript 23 Lectures 1.5 hours Laurence Svekis More Detail JavaScript Advanced Course – Useful methods to power up your code 26 Lectures 2 hours Laurence Svekis More Detail JavaScript Course for Beginner to Expert: Data Visualization Best Seller 77 Lectures 6 hours Metla Sudha Sekhar More Detail Print Page Previous Next Advertisements ”;

Chart.js – Category Axis

Chart.js – Category Axis ”; Previous Next Axes are an integral part of any chart or graph. Like Cartesian axes, category axes are also an essential part of a chart. The syntax of defining category axes globally is given below − let chart = new Chart(ctx, { type: … data: { labels: [”January”, ”February”, ”March”, ”April”, ”May”, ”June”], datasets: … } }); We can define category axes as a part of axis as follows − let chart = new Chart(ctx, { type: … data: … options: { scales: { x: { type: ”category”, labels: [”January”, ”February”, ”March”, ”April”, ”May”, ”June”] } } } }); Example Let’s take an example in which we will use category axes for creating a chart − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”300″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/helpers.esm.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”bar”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”yellow”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”gold”], borderColor: [”black”], borderWidth: 1, pointRadius: 4, }], }, options: { responsive: false, scales: { x: { min: ”CSS”, max: ”JQUERY” } } }, }); </script> </body> </html> Output Print Page Previous Next Advertisements ”;

Chart.js – Polar Area Chart

Chart.js – Polar Area Chart ”; Previous Next Chart.js polar area charts are like pie charts. The only difference is that in polar area charts, each segment has the same angle i.e., the radius of the segment differs according to the value. If you want to show a comparison between data but also want to show a scale of values for context, polar area chart is an excellent choice. Below are the namespaces to be used in polar area chart for dataset properties − data.datasets[index] − It provides options for this dataset only. options.datasets.polarArea − It provides options for all polarArea datasets. options.elements.arc − It provides options for all the arc elements. Options − It provides options for the whole chart We need to use type: “polar” for creating the polar area chart. Example Let’s take an example with the help of which we will create a polar area chart − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”polarArea”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 15, 35, 25, 38], backgroundColor: [”yellow”, ”aqua”, ”pink”, ”lightgreen”, ”gold”, ”lightblue”], }], }, options: { responsive: false, }, }); </script> </body> </html> Output Print Page Previous Next Advertisements ”;

Chart.js – Bar Chart

Chart.js – Bar Chart ”; Previous Next Chart.js bar chart, as the name implies, is a method to plot the data values as vertical bars. In most of the case, line charts are used to show the trend of data and a comparison between more than two data sets side by side. Below are the namespaces to be used in bar chart for dataset properties − data.datasets[index] − It provides options for this dataset only. options.datasets.bar − It provides options for all the bar datasets. options.elements.bar − It provides options for all the bar elements. Options − It provides options for the whole chart We need to use type: ’bar’ for creating the bar chart. Example Let’s take an example with the help of which we will create a bar chart − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/helpers.esm.min.js”> </script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”bar”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”yellow”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”gold”], borderColor: [”red”, ”blue”, ”fuchsia”, ”green”, ”navy”, ”black”], borderWidth: 2, }], }, options: { responsive: false, }, }); </script> </body> </html> Output By executing the above code, we will get the following output chart − Print Page Previous Next Advertisements ”;

Chart.js – Title

Chart.js – Title ”; Previous Next Chart.js title defines which text is going to be seen at the top of our chart or graph. The namespace for Title configuration is options.plugins.title whereas the global option for the chart title is defined in Chart.defaults.plugins.title. The table below gives the descriptions of various kinds of configuration options we can use with chart title − Name Type Default Description align string ”center” As name implies, with this configuration you can set the alignment of the title. color Color Chart.defaults.color It will define the color of text. display Boolean false If true, it will show the title, else NOT. fullSize Boolean true As name implies, if true, the box will take the full width/height of the canvas. position string ”top” It is used to set the position of the title. The default is top position. font Font {weight: ”bold”} You can use various fonts. The options are in “Chart.defaults.font”. padding Padding 10 It is use for padding to be applied around the title. text string|string[] ”” As name implies, it is use for the title text to display. Syntax The Chart Title syntax is given below − title: { display: true, text: ”write heading here”, color: ”navy”, position: ”bottom”, } The title enabled property must be true to display the data label. If it is set to false, then the title becomes deactivated. Example Let’s take an example in which we will be using various Title configurations − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”bar”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”coral”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”crimson”], borderColor: [”red”, ”blue”, ”fuchsia”, ”green”, ”navy”, ”black”], borderWidth: 2, }], }, options: { responsive: false, plugins: { title: { display: true, text: ”Web Application Technologies”, color: ”navy”, position: ”bottom”, align: ”center”, font: { weight: ”bold” }, padding: 8, fullSize: true, } } }, }); </script> </body> </html> Output The following output chart shows the various Title configurations − Print Page Previous Next Advertisements ”;

Chart.js – Line Chart

Chart.js – Line Chart ”; Previous Next Chart.js line chart, as name implies is a method to plot the data points on a line. In most of the case, line charts are used to show the trend of data or a comparison between data sets. Following are the namespaces to be used in line chart for dataset properties − data.datasets[index] − It provides options for this dataset only. options.datasets.line − It provides options for all the line datasets. options.elements.line − It provides options for all the line elements. options.elements.point − It provides options for all the point elements. Options − It provides options for the whole chart We need to use “type: line” for creating the line chart. Example Let’s take an example with the help of which we will create a line chart − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” heigth=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”line”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”yellow”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”gold”], borderColor: [”black”], borderWidth: 2, pointRadius: 5, }], }, options: { responsive: false, }, }); </script> </body> </html> Output The following output shows the creation of a line chart − Print Page Previous Next Advertisements ”;

Chart.js – Options

Chart.js – Options ”; Previous Next In Chart.js, using a context dependent route, the Options are resolved from top of the code to the bottom of it. We have different levels of options as shown below − Chart Level Options We have three chart level options − options overrides defaults Dataset Level Options Chart.js provides us below options to be used at dataset level − dataset options.datasets options overrides defaults.datasets defaults Dataset Animation Options Chart.js provides us below options to be used at dataset animation level − Dataset.animation options.datasets.animation options.animation overrides.datasets.animation defaults.datasets.animation defaults.animation Dataset Element Level Options Chart.js provides us below options to be used at dataset element level − Dataset options.datasets options.datasets.elements options.elements options overrides.datasets overrides.datasets.elements defaults.datasets defaults.datasets.elements defaults.elements defaults Scale Options Chart.js provides us below scale options − options.scale overrides.scales overrides.datasets.elements defaults.scales defaults.scale Plugin Options Chart.js provides us below plugin options that provides array of paths to additionally look for its options in − options.scale options.plugins[plugin.id] (options.[…plugin.additionalOptionScopes]) overrides[config.type].plugins[plugin.id] defaults.plugins[plugin.id] (defaults.[…plugin.additionalOptionScopes]) Example Let’s take an example in which we will use various Options in our chart − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”bar”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”yellow”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”gold”], borderColor: [”red”, ”blue”, ”fuchsia”, ”green”, ”navy”, ”black”], borderWidth: 2, }], }, options: { responsive: false, layout: { padding: { left: 40, right: 40, } }, plugins: { legend: { labels: { font: { size: 25, family: ”Helvetica”, style: ”bold”, } } } } }, }); </script> </body> </html> Output The following output chart shows padding of the element and font style − Print Page Previous Next Advertisements ”;

Chart.js – Installation

Chart.js – Installation ”; Previous Next Prerequisites Before installing and start using Chart.js library, you need to have the basic understanding of the following programs − Basics of Hypertext Markup Language (HTML) and Cascading Style Sheets (CSS) Basic understanding of JavaScript especially Object-Oriented Programming (OOP) concepts and Arrays. Installation Before start working with Chart.js, we need to first install it. Below are the couple of ways you can use to install Chart.js − Method 1 − Using NPM You can use NPM to install Chart.js. Copy and paste the following command to download Chart.js in your project − npm install chart.js –save Method 2 − Using a CDN Using a CDN to install and use Chart.js in your project is one of the fastest and easiest way. First grab the latest CDN link from CDN website: https://cdnjs.com . Now, copy the URL that has Chart.min.js at the end. Method 3 − Using GitHub You can use GitHub to download the latest version of the chart.js library. The link https://github.com helps to get the Chart.js library. Method 4 − Using jsDelivr You can also use jsDelivr to download the latest version of the chart.js library. The link https://www.jsdelivr.com/ helps to get the Chart.js built files. Setup Project with Chart.js Using CDN − To set up your project with chart.js, include a script tag in your HTML that links to the chart.js CDN. In other words, load the CDN into the body section as follows − <html> <body> <script type=”text/javascript” src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> </body> </html> Using jsDelivr − You can follow the below steps to setup chart.js for your project using jsDelivr − Step 1 − Create a file and save the name with the “js” keyword. For example: firstchart.js. Step 2 − Now download chart.js library from the jsDelivr. The downloaded library saves in the filename.js file. Copy and paste the link https://cdn.jsdelivr.net in the .js file. Step 3 − Next, create a HTML file and write the code. Put the script tag in the <body> </body> section. Step 4 − Last, add this file into the script tag with the path <script src = “path/folder/firstchart.js”></script> Print Page Previous Next Advertisements ”;

Chart.js – Tooltip

Chart.js – Tooltip ”; Previous Next Chart.js Tooltip provides us an option to show tooltip text in our chart. Tooltip is a graphical UI element that provides extra information as we hover the mouse over the chart elements. The namespace for Animation configuration is options.plugins.tooltip. Syntax The Chart Tooltip syntax is given below − options: { plugins: { tooltip: { enabled: true, (Write tooltip”s style element) }, } } The tooltip enabled property must be true to display the data label. If it sets to false, then the tooltip becomes deactivated. Example Let’s take an example in which we will be using various Tooltip configurations − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/helpers.esm.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”bar”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”coral”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”crimson”], borderColor: [”red”, ”blue”, ”fuchsia”, ”green”, ”navy”, ”black”], borderWidth: 2, }], }, options: { responsive: false, plugins: { tooltip: { enabled: true, usePointStyle: true, titleAlign: ”center”, titleColor: ”gold”, titleSpacing: 3, TitleFont: { weight: ”bold” }, backgroundColor: ”midnightblue”, bodyColor: ”orange”, bodyAlign: ”center”, bodyFont: { weight: ”italic” }, callbacks: { labelPointStyle: function(context) { return { pointStyle: ”circle”, rotation: 0 }; }, } } } }, }); </script> </body> </html> Use the “Edit and Run” option to execute the code online and then hover the mouse over the bars and observe the styling of the tooltips. Output The following output chart shows the various Tooltip configurations − Print Page Previous Next Advertisements ”;

Chart.js – Interactions

Chart.js – Interactions ”; Previous Next Chart.js interactions help us to communicate with the elements of our chart. You can apply these interactions using both the hover and tooltip. The table below gives the descriptions of various kinds of interactions we can use − Name Type Default Description Mode string ”nearest” It will set which elements appear in the interaction. Intersect Boolean true Intersect can be used to set the mouse position. If you keep this true, the interaction mode only applies when the mouse position intersects an item on the chart else not. Axis string ”x” Axis can be set to ”x”, ”y”, ”xy” or ”r”. It defines the directions are used in calculating distances. For index mode the default value is ”x”. Whereas ”xy” is the default value for ”nearest” modes. includeInvisible Boolean false if false, the invisible points that are outside of the chart area will not be included when evaluating interactions. The namespace to use interactions in our charts is “options.interactions”. On the similar note, the following table gives the descriptions how the chart will interact with events − Name Type Default Description Events string[] [”mousemove”, ”mouseout”, ”click”, ”touchstart”, ”touchmove”] This option defines the browser events that the chart should listen. onHover function Null This event will be called when any of the events fire over our chartArea.). onClick function Null This event will be called if the event is of type ”mouseup”, ”click” or ”contextmenu” over our chartArea. Syntax The interaction syntax is given below − interaction: { mode: ”nearest”, } And the Chart.js event syntax is given below − events: [”click”], Example Let’s take an example in which we will use various interactions and events in our chart − <!DOCTYPE> <html> <head> <meta charset- “UTF-8″ /> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <title>chart.js</title> </head> <body> <canvas id=”chartId” aria-label=”chart” height=”350″ width=”580″></canvas> <script src=”https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.min.js”></script> <script> var chrt = document.getElementById(“chartId”).getContext(“2d”); var chartId = new Chart(chrt, { type: ”bar”, data: { labels: [“HTML”, “CSS”, “JAVASCRIPT”, “CHART.JS”, “JQUERY”, “BOOTSTRP”], datasets: [{ label: “online tutorial subjects”, data: [20, 40, 30, 35, 30, 20], backgroundColor: [”coral”, ”aqua”, ”pink”, ”lightgreen”, ”lightblue”, ”crimson”], borderColor: [”red”, ”blue”, ”fuchsia”, ”green”, ”navy”, ”black”], borderWidth: 2, }], }, options: { responsive: false, events: [”click”], interaction: { mode: ”nearest”, } }, }); </script> </body> </html> Output It will produce the following chart having various interactions and events − Print Page Previous Next Advertisements ”;