score:7
Accepted answer
The way to do this, to create multiple items in the datasets, each having data for only one category. You can have one top level label called stock and then create the individual data-sets for each type.
// create a chart.
const ctx = canvas.getContext("2d");
const chart = new Chart(ctx, {
type: "horizontalBar",
data: {
labels: ["Stock"],
datasets: [{
label: "Apples",
backgroundColor: "#AF7",
data: [Math.random() * 100],
},{
label: "Oranges",
backgroundColor: "#FA4",
data: [Math.random() * 100],
},{
label: "Mangos",
backgroundColor: "#FF7",
data: [Math.random() * 100],
},{
label: "Avocados",
backgroundColor: "#2A7",
data: [Math.random() * 100],
}]
},
options: {
responsive: false,
legend: { position: 'top'},
title: { display: true, text: 'Fruit in stock'}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id=canvas height=200 width=500></canvas>
Source: stackoverflow.com
Related Query
- ChartJS bar chart with legend which corresponds to each bar
- ChartJS horizontal chart display legend on each bar
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- ChartJS Bar chart with time scale - Bars overlap with each other
- Chartjs random colors for each part of pie chart with data dynamically from database
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chartjs Bar Chart Legend
- Chartjs - data format for bar chart with multi-level x-axes
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Chartjs with Vue, Bar Chart Border Radius not working
- Bar chart with min height and zero values - ChartJs
- How to set X coordinate for each bar with react chart js 2?
- Stacked bar chart with chartjs with included values
- Multi-colored legend stacked bar chart Chartjs
- Legend color not working with randomly generated background colors in chartjs pie chart
- Chartjs v2.7 - combo chart with time-series (financial) data; bar width's are irregular
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- Angular Chartjs Overlapping Bar Chart with Chartjs ver 2.7
- ChartJS Bar Chart not respecting disabled legend when using cdn
- how to write labels along with data on top and bottom of each stack in bar chart
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- ChartJS vertical bar chart - measure percentage of the data in each column based on a specified max
- How to know which bar is clicked on chart.js bar chart with multiple data?
- ChartJS 2.7.3 stacked bar chart with overlap
- Chartjs v3 overlap stacked bar chart with groups
- Draw stacked horizontal bar chart with Average line using ChartJS
- Grouped bar chart having each group with different data using chart.js
- Chartjs Custom Legend for Doughnut Chart Having Labelled Lines Sticking Out of Each Section
- Chart.js manipulate each bar in chart with different labels
- subcategories for each bar in multibar chart using chartjs
More Query from same tag
- How to create a chart with chartjs.org with data from an array?
- How can I set the scale with alphabet on chartjs?
- Time graphs chart.js
- chart.js d3js chart in time scale, data from CSV file
- How to Sum values from different objects in Django?
- Chart JS Replace All Dataset Data
- How to draw multiple lines in chart.js
- chart.js data forming though the API dynamically based on selects
- Can't resolve 'chart.js/auto'
- Chartjs Stacked bar have a stack id per x value
- Chart.js - How to push a collection of array into dataset
- chartjs creating equilvant background like in chartjs.org
- Aurelia - Unhandled rejection - Chart.js and Moment.js
- Using large values for y axis in Chart.js
- Options: plugins not functioning
- Get hash value Rails / Chart.JS
- Put chart.js chart inside JQuery tooltip?
- Applying Callback To One Y-Axis
- ChartJS - Show/hide data individually instead of entire dataset on bar/line charts
- ChartJS not working in Vue
- How to parse "hh:mm:ss" strings to time in Charts.js 3.x
- ChartJS with React: Only one value showing on time chart
- How to define chart.js time scale date formats
- React js with react-chart js, does not render doughnut chart
- chartjs bar chart filter using datetimepicker
- Chart.js: Can datasets be overlaid in a bar chart? (Not the same as stacked)
- Chart.js Show labels on Pie chart
- How to display k as thousand for the values of chart.js - Javascript
- Iterate over query for use in Charts.js using Coldfusion
- React & Chartjs: How can I best destroy my chart when re-rendering?