In This article, we will discuss how to Create grouped and stacked charts in Chart.js. First, let you know that it is possible to create a grouped and stacked chart in Chart js.
Stacked and Grouped Bar Chart Chart.js Example
The StackGroupbarChart is the variable that contains all the datasets and StackGroupbarChartOptions variable contains all the styling properties of the Stack Group barChart.
StackGroupbarChartOptions is an object with multiple properties used that would be required to display our data in the form of a Bar chart.
The labels property in StackGroupbarChart variable is an array that is used to assign the names to each bar, chart and the datasets property is also an array that contains information such as label title, bar backgroundColor, bar borderColor, borderWidth data.
<!DOCTYPE html>
<html>
<head>
<title>Stacked & Group Bar Chart Using Chart.js </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="StackGroupbarChartcanvas" style="width:100%;"></canvas>
</div>
<script>
var StackGroupbarChart = {
labels: [
"2016",
"2017",
"2018",
"2019",
"2020",
"2021",
"2023",
"2022"
],
datasets: [
{
label: "App Developer",
backgroundColor: "pink",
borderColor: "pink",
borderWidth: 1,
data: [15, 22, 35, 86, 42, 62, 52, 58],
stack: 'Stack 0'
},
{
label: "Andriod Developer",
backgroundColor: "blue",
borderColor: "blue",
borderWidth: 1,
data: [48, 38, 39, 42, 42, 69, 39, 68],
stack: 'Stack 0'
},
{
label: "C# Developer",
backgroundColor: "green",
borderColor: "green",
borderWidth: 1,
data: [52, 29, 39, 68, 67, 66, 74, 77],
stack: 'Stack 1'
},
{
label: "Java Developer",
backgroundColor: "red",
borderColor: "red",
borderWidth: 1,
data: [52, 29, 39, 68, 67, 66, 74, 77],
stack: 'Stack 2'
}
]
};
var StackGroupbarChartOptions = {
responsive: true,
legend: {
position: "top"
},
interaction: {
intersect: false,
},
scales: {
x: {
stacked: true,
},
y: {
stacked: true
}
},
title: {
display: true,
text: "Chart.js Bar Chart"
}
}
window.onload = function () {
var chartData = {
type: "bar",
data: StackGroupbarChart,
options: StackGroupbarChartOptions
}
new Chart("StackGroupbarChartcanvas", chartData);
};
</script>
</body>
</html>
Code Editor
Code Explanation
As you can see in the above code we have three sets of data and each set has a number of developers belonging to a particular technology i. e App Developer, Andriod Developer, C# Developer, and Java Developer. now we want to create a ‘stack’ chart these using App Developer and Andriod Developer datasets and a ‘group’ bar chart using C# Developer and Java Developer dataset.
Now what’s awesome about this is that this is HTML, this is responsive, it’s using canvas. There are eight different chart types and it’s totally open source. So I can’t imagine a reason why you wouldn’t want to use this project over some other charting solutions. Now there are things like D3, D3is an amazing package in the ecosystem. However, D3 is going to be overkill if you need a simple chart like one of these.
And these charts aren’t even that simple, to be honest. if you need a chart, you know, learning D3 is great. But if you need advanced visualizations, then D3 is probably going to be more up your alley. But if you’re just doing beautiful charts, check out chart js.
It’s awesome. Okay, so let’s head to documentation here. And as you can see, we have some pretty sweet docs. We have a line chart bar chart. And if you select the heading of any of these, it gives you not only just like an example, but a working example.
Now we also have pie and donut charts.Now a doughnut chart is just a sort of hipper way of showing a pie chart at this point it’s showing the exact same data but you see these used all over the place like Mint.com uses them and stuff like that and here we have scales which allows you to fully scale data whether it’s things like logarithmically and stuff like that.
But as you can see here, these are some of the best docs around so you can head to chartjs.org docs and just head in here and just see what’s available get inspired and in the next video we’re going to create our very first line chart right up top here and I’m going to talk a little bit about Chart JS while we go along so the other charts are a little easier once we get into them. Cool. So this is Chart JS. Hope you’re excited, we’re going to be building some really cool stuff.
The post [Simple Trick]-Stacked and Grouped Bar Chart Chart.js Example 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
- How can I fake a "Change" on an input in Javascript/Jquery
- javascript: How to destory class Object and make calling variable undefined?
- how to search an array full of objects based on a key and return a specific object
- JQuery .toggle() Not understood
- Materialize css dropdown in input field issue
- How to bind labelfor text to dropdown in jquery
- Toggling many div id
- Change text of a select option while the menu is open
- issue with __construct in js file
- jquery click vs button executed function to find length of checkboxes
- How to manipulate each item in json array to go to a specific element class, href, id, text..etc?
- jQuery: animate to zero width if there is some padding inside
- Delete multiple ng-repeat rows not working using datatables
- How to drag and drop into text
- Codeigniter session is not saving userdata and creating duplicate sessions
- Anything like window.load that can be used after ajax call?
- Select some checkboxes based on selection
- Laravel How to reset modal ajax data values upon closing the modal
- Input value returning undefined
- jQuery Mobile trigger filterablebeforefilter
- automate color pick for the stacked bar plots
- What is the correct way to load datatables.net plugin into a Vue Webpack app?
- Simple form for remote true responding with html
- How to print an div with emoji "❤️" to pdf
- filling the text area using PHP
- Get the numbers inside two spans and do a calculation?
- passing javascript parameter word to a custom function
- How to remove scrollbar in popup form
- Unwrap HTML tags keeping only first level
- How to not generate context menu for toolbar select element in free jqgrid
- Typescript how to assign ajax object into property?
- How to do floating div in combination with Fancybox?
- KnockoutJs Computed - 'Is not a function'
- GIF picture won't animate when displaying a loader/spinner in jquery
- Place absolute element always under other element
- Internet Explorer CSS Transition issues, Flipping Boxes with CSS. Disable effect?
- Use jQuery to add data to elasticsearch index
- Send Form Using jQuery after form is generated using Ajax
- javascript settimeout not working inside recursive function
- radio button change event not working in jquery && mvc
- jquery count first level elements from within .each() loop
- How to set to tag width and height of included image
- How to fix smooth navigation fixed in Jquery?
- Check if hash is present, if not then add hash
- Add rows to gridview using jquery
- Show/Hide Divs using jQuery and ACF
- Running multiple deferred groups sequentially
- Uncaught SyntaxError: Unexpected token }, no visible error?
- Performance of different method of calling JavaScript/jQuery function
- Filter JSON key value and assign names to rendered elements
- jQuery on click prepend divs to the front of a 'list'
- Change html in iframe using jQuery
- Dynamically generated html form don't allow jQuery bind("submit") method
- Highlight the minimize the browser window
- Detail section in row of `ng-repeat`
- Dialog not firing on JSON callback
- materialize css double modal
- Custom search in jquery datatable
- Growl works easy way but not hard way, blockUI
- Refinement jquery files for mouseover change content
- jQuery show/hide div with dropdown more levels
- javascript html not working on chrome
- Using jQuery, how can I clone forms elements and increment the name and id of the cloned elements?
- jQuery Validate on a Complex Rule: Turn off automatically when not applicable but not on startup
- Change Jquery UI datepicker positioning to top
- Getting all the content of table and append it using ajax with jquery
- How to show the content by tab selection using jQuery?
- jQuery removeClass called at end of slideUp removes from all elements not just this
- How to disable or hide submit button after form validate and submited by ajax?
- $.ajax response is empty
- Target css:after via Jquery?
- on Click of Backspace Button jQuery Filter is not working
- Change cursor while moving mouse
- jQuery click anywhere except a div
- when scroll to top all my objects have visibility at the same time
- Angular programmatically binding ng-model
- jquery - padding to match height of another div
- infinite loop a set of images
- Using jquery with Ajax in a Joomla 2.5 component
- HighChart timestamp highlight the time now
- Cancel option of Draggable preventing drag of children elements
- Saving data to chrome storage via created button
- Unable to get autoId value into jobId object, here the localStorage autoId is a string
- jQuery post not passing parameters
- How to select last flexbox element?
- jQuery.mmenu dragOpen disable dragging at desktop
- jQuery autocomplete: filter by first letter
- jqgrid return error 404 when pass long parameter
- How to make sure an element is on top of all other elements in a page?
- Wijmo Grid. Get rownumber for row with matching Id
- jQuery: children count = 0 and ajax
- Combine multiple jQuery events based upon ID's
- Break out from nested loop: SyntaxError: label not found
- Uncaught reference error in simple onclick function
- Sorting Divs Contents With Letter Selection
- Interesting logo use to show websites
- Laravel - Uncaught SyntaxError: Unexpected identifier in Laravel JQuery
- Getting string data returned from asmx webservice with AJAX
- DataTables warning: table id = table - Ajax error
- Raphael.js Qtip tooltip disappears when trying to set id
- JavaScript error “is not a function”
- Get the users location with ipaddress in Angularjs
- Get Rails 4 Enum Values Available In Jquery Slider Input
- Update global variable from jQuery event
- Can I click the same gylphicon class and toggle between two functions?
- passing data between two jsp pages using jquery
- How can I set different themes to multiple (jQuery) jTable on the same page?
- Setting height of surrounding DIV after inner DIV is expanded/collapsed
- Circular reference was detected while serializing an object of type
- jQuery: stop absolute positioning of dragged elements?
- Jquery Slider UI for displaying Overlay Layer in OpenLayers based on Opacity
- Why do i have to enclose jQuery commands in a function
- clone element using next in js
- Error appear related to JS for Toggle Button
- Check for window.location.href.indexOf doesn't work
- chrome extension Get the value of generated input
- How to make div disappear when image is placed over it?
- Passing Ajax Request to Django View on Multiple Button Click
- Twitter Bootstrap 3 modal dialog not displaying
- what is and where is DOM relation between DOM and Window?
- Changing many same class DIV's color based on response value
- Applying CSS to dynamically named elements
- List of Words to Ignore in JavaScript?
- DataTables Add Row with special data fields
- Dijit Dialog Button Event Not Executing
- Angular Material Datepicker - select monday by default when other days of the week is chosen
- Adding css to a div when something is not present jquery
- To slecct the node in the TreeView ,but the node,s children automatically be selected ~
- How do I add a global javascript and css to a whole page that uses a predefined template in Adobe CQ5?
- Storing data inside jQuery's data attribute VS javascript string
- jQuery not working on onclick change span text and display hidden table
- accing dynamic element through jquery
- centering the right does not work
- Passing list of objects from JSP to server in SPRING MVC
- How to pass div's html to @Url.Action in Ajax post
- jquery plugin issues with counting chars when returns are used
- Jquery Form Validation Difficulty
- ExtendedRenderKitService not applying javascript scrollTop in modeless popup dialog
- Ajax search functionality in Wordpress
- live each jquery?
- Jquery scroll problems
- Show comment after submit
- Javascript Select option in Select box based on class
- Animated sticky header that changes WITH scroll
- Media Queries for printing not working
- How to check whether buttons are pressed in the correct order?
- jquery - creating interactive elements
- Using a text string search function on a page, is there a way to have the search ignore a specific div?
- how to use JQuery ReplaceWith with materials.io icons
- How do you create a Hight Charts chart that updates every second with real data?