Recently I am using the Chartjs library for creating graphs on my website and in that we need to set the title of the y-axis and graph, and I notice that some people also facing the same issue as there is no information about it in the documentation. So I decided to write this article for helping other developers.
How to set ChartJS set Chart Title, Name of X axis and Y axis
Heytoday’s article is about charts and I’m talking about charts. Javascript library a very powerful library to create, to easily create different kind of charts. Using below option you can easily set Chart Title, Name of X axis and Y axis.
options: {
plugins: {
title: {
display: true,
text: 'Visitor',//setting chart title
}
},
scales: {
x: {
display: true,
title: {
display: true,
text: 'Year'//setting Name of X axis
}
},
y: {
display: true,
title: {
display: true,
text: 'Visitor'//Name of Y axis
}
}
}
}
How to add axis titles in chart.js charts
<!DOCTYPE html>
<html>
<head>
<title>Chart.js-Multi line charts </title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.7.1/dist/chart.min.js"></script>
</head>
<body>
<div style="margin-left:5%;margin-right:5%">
<canvas id="myLineChart" style="width:100%;max-width:80%"></canvas>
</div>
<script>
var xValues = ["Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Aug", "Oct"];
const MultiLinechartData = {
type: "line",
data: {
labels: xValues,
datasets: [
{
label: 'Visitor',
lineTension: 1,//this property is used for setting the line curved
borderColor: 'red',
data: [300, 450, 450, 550, 700, 620, 1200, 1120, 1050, 1159, 1120]
}
]
},
options: {
plugins: {
title: {
display: true,
text: 'Visitor',
}
},
scales: {
x: {
display: true,
title: {
display: true,
text: 'Year'
}
},
y: {
display: true,
title: {
display: true,
text: 'Visitor'
}
}
}
}
};
new Chart("myLineChart", MultiLinechartData);
</script>
</body>
</html>
*Thank you so much if you have a question please comment
And then we can specify the new variable data one, where we can just specify the values of our answers to the question. So do you like Donuts? Yes, but in green and 69% said yes, and 31% said yes. Button-green. And then we can specify our
Let colors one equals two. And now lets for both answers, give a certain color. For the first one, let’s give 49 a nine EA. And for the second one, let’s hand over 36 cabs.
Basically, we could have put those stuff directly in here. So we could have put the labels directly here, the data directly here, and the background colors right here in the specification of the chart. But I like to separate it because I think it’s easier to change afterward. Let’s have a look at our index. Html file.
Here we are. Let’s refresh it. And you can see nothing because we got a syntax error in line 19. Let’s have a look. Line 19 displays truer.
I forgot a comma here. My bad. Let’s save it again, go back, refresh it, and here you can see our chart. So do you like Donuts? Yes, but in green.
And that’s it. Isn’t it easy? Right. All right, so our next chart will be a bar chart and we want to answer the question which airline carries? How many passengers?
So let’s Google a little bit passengers by airline and let’s check out Wikipedia here world’s largest Airlines passengers carrier and let’s have a look at 2022 and display what we see here. So those values are in a bar chart.
The post [Simple Trick]-In Chart.js set Chart Title, Name of X axis and Y axis appeared first on Software Development | Programming Tutorials.
Read More Articles
- [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax
- jQuery Ajax GET Example with Parameters
- How to Pass Parameters in AJAX POST?| JQuery Ajax Post Json Example
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- How to add, edit and delete rows of an HTML table with Jquery?
- Registration form with image upload in MVC using jquery Ajax
- How to make an Inline editable table in MVC using jquery?
- Insert Update Delete Using Jquery Ajax and Modal Popup in Mvc
- [Solved]-How to Upload pdf file using jquery MVC?
- Dynamically creating graphs with jQuery
- PHP jQuery value problem
- get an id of a specific form within several form on the same page (jquery)
- How To Retrieve Data from Json Array In Ajax
- jQuery infinite loop over RSS elements
- Why does using alert in Shopify cart/add API only add product in cart for firefox browser?
- Alternate SVG fill color on click
- asp.net html leaving an active effect under Navigation
- How to display 3 weeks in a row in jQuery UI DatePicker?
- nginx 1.19.0 : Getting 415 error when uploading a file
- Hyperlink in jquery booklet plugin
- Dynamic Classes forgotten on Firefox page refresh
- jQuery fadeToggle not working properly due to bug with delay?
- Optimising timers for modern browsers (Chrome) in Javascript
- speeding up the recursive function
- How to upload a file and display its information
- How to add class to navigation item by route path
- jquery auto scroller
- Firebug Error (missing ) after argument list)
- SVG mouseenter glitch
- Traversing html element from one view to another
- How can i make the last twoitems align at bottom?
- Calculating row totals in form using dynamically added rows
- Vanilla JavaScript / jQuery - Await results of multiple HTTP requests for function
- Various images sizes using fancybox
- Handle form submission in jQuery
- anonymous function passed from PHP and set specific `this` reference
- jQuery DataTable Column RenderLinkAsButton in C# ASP.NET MVC Project?
- Adding a darkmode stylesheet to localStorage with jQuery
- Ajax operation running later than javascript function
- How can I use jquery events to pass variables to php?
- Jquery Uncheck all checkboxes dynamically added after ajax request
- JSON Parse error when posting to MEAN API
- How to send Request only if string matches data
- Jquery datatables custom button for export
- How to get element value from array after values have been pushed into it in jquery?
- How to make multiple, but separate AJAX calls in JQuery
- jqPlot not plotting using dates
- Jquery show select box value based anchor clicked
- DataTables how to disable Request if table reaches breakpoint
- Bootstrap Modal not displaying without internet access
- Dynamically adding form elements depending on user selection
- help with chained selects and existing form values, ex: adding <option selected="selected"></option>
- how to select the nth-child of dynamically generated row
- Why I can't display an js error alert in updatepanel page from class?
- How do i add locale.json files in seperate folder in i18next?
- Preserving JavaScript data types in JQuery data objects
- Proper Syntax for Overloading an Function from External JS plugin
- NULL values while passing JSON data to controller via AJAX
- jquery file upload php database blueimp
- escape json to be read as array in php
- data sending issue with ajax
- Jquery ajaxFileUpload not working in IE
- fullCalendar using eventAllow to restrict event based on given DateTime
- jQuery: set/unset active links
- Updating a <tr> in Rails via Ajax/jQuery
- add tabs with dynamic content
- Rails 3.2 - jQuery-UI images are not displayed on production mode
- ON method won't trigger at first click on the page already loaded
- ajax list avaliable users small bug in javascript
- Binding autocomplete to a dynamically added form field
- filter "items" with Jquery via a Dropdown
- sticky :hover/:focus on touch devices
- jquery spinner widget up/down events
- Jquery improve .css() animation on click
- Kill setTimeout on Popover
- JQuery username validation - Ajax call
- Responsive iframe carousel modal?
- Dynamically writing up to 5 select items, but want to limit to 5 using javascript
- jquery image modal not loading image first time
- jQuery click event is not working in Internet Explorer
- jquery to unsave jobs on Linkedin
- search suggestions not working
- How to get id in table tr? jquery
- Grabbing Contents of h4 in same container as "Remove Button" - jQuery
- jQuery Multiple .ajaxSuccess, load different functions specifically for each ajaxSuccess
- calling javascript function with parameters in href
- Set cookie everytime user deletes it
- how to get the images changing by sliding the range slider?
- Positioning a js button under the content it affects
- How to append and replace text in URL with jQuery without reloading?
- How to create watermark on each image while they are uploading with Uploadify?
- jQuery SlideDown / SlideUp | "FAQ" | Opened
- PHP: Selecting a Table Row via JQuery
- css issue that I am struggling with?
- Random JavaScript Select code fix
- How can I bind many different click functions on one DOM element?
- Passing parameter from hyperlink in a Datatable Jquery
- Starting session on jquery mobile, without ajax
- Jquery Chosen with Parsley
- Using Inputmask jquery plugin default placeholder text not displayed
- JQuery UI AutoComplete Code Issue with Function as Source
- Date validation while typing in input field in Jquery
- jQuery - Get this/element within filter()
- Problem with add class on hover with equal data-id
- how to insert between multiple rows a button
- input range cannot working with jquery ui
- How can I make my jQuery-UI checkbox buttons detect the state of a checkbox on page load?
- Table 'Show More' Reset after Search
- Where do I find the url that the built in rails server provides to store data?
- How can I have my script only activated on mobile devices?
- use JSON variable in jQuery dynamically
- Any idea why draggable div not released on mouse up in IE?
- jQuery .load() and cache image conflict
- Vertically/Horizontally centering text over an img-container
- Change background of div based on value of div
- print text with typewriter effect, clear after each sentence on loop
- How to get a value from hidden inside tables selected row
- Checkbox toggle with two different properties
- Dropdown json data with JQuery displays "undefined" result
- jq toggle to do more work
- Datatables Multiple Filter is not working after replace with Jquery function
- In JQuery using animate function how do I make text bold, change its colour and make it larger?
- Internet Explorer won't trigger click function if display: none;
- Form Autosave using CKEditor
- How to add navigation to a modal
- Backbone app breaks in FF and IE9
- Conditionally change the 'targets' attribute of a struts2 form based on action result
- Jquery - Bootgrid Pre-selecting rows on load
- Codeigniter Ajax - Passing Filelist always get NULL
- Bootstrap validation. How can i treat zero as null?
- Post by jQuery serialize() / serializearray() excluding textarea
- How can I call a function independently on each element in a page?
- Need the responseText of jQuery.ajax returned instead of using the 'success' function
- textbox access for datepicker
- Reset glossary items when clicking "View All"
- Empty PHP Session Array after Redirect
- javascript and Internet Explorer 8
- jquery event on hashed div
- jQuery sortable() - How to check if two <li> elements are next to each other by class?
- Scrollify - Block mouse scroll when animation going
- Failing to create an ajax request to handelbars.js template
- How to hide a tag after given speciffic condition?
- Detecting clicks inside a div using wildcards in jQuery
- jquery check for long strings in a textarea
- How to get selected values from drop-down list in filterToolbar using jquery
- How to apply different css margin-top to each children in a list
- Prevent `document.execCommand("copy")` from replacing ampersands
- How to slide div over when showing another div of variable width with CSS and Jquery?
- Jquery slidedown toggle change button text and icon when clicked
- Cannot save cookie with jQuery and Internet Explorer 9
- jQuery tooltip offset
- Bootstrap Dropdown links not working mobile
- Events from AJAX-Source are not added to fullcalendar
- Add active class to "div" based on URL
- Jquery Dynatree , only the child node is selectable?
- Jquery/Ajax function does not work when clicking on icon for Django site
- How to check if JQuery libraries (and files with own code) are included correctly?
- Jquery selectric plugin update options values problems
- Javascript: Replace a Shown Div with another Hidden Div with Button onclick
- keep/change uploaded image size