score:0
since version 2.9.0, chart.js supports floating bars. individual bars can now be specified with the syntax [min, max]
.
this makes it possible to draw a gantt chart.
var dates = [new date("2019-12-24"), new date("2019-12-26"), new date("2019-12-30"), new date("2020-1-2"), new date("2020-1-4"), new date("2020-1-9"), new date("2020-1-10") ];
var ctx = document.getelementbyid("mychart").getcontext('2d');
var mychart = new chart(ctx, {
type: 'horizontalbar',
data: {
labels: dates.map((d, i) => "task " + i).splice(1),
datasets: [
{
label: 'tasks',
data: dates.map((d, i) => i == 0 ? null : [dates[i - 1], d]).slice(1),
backgroundcolor: 'lightblue',
borderwidth: 1
}
]
},
options: {
responsive: true,
scales: {
xaxes: [{
type: 'time',
time: {
displayformats: {
day: 'yyyy-mm-dd'
}
},
ticks: {
min: dates[0].gettime(),
max: dates[dates.length - 1].gettime()
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.1.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>
<canvas id="mychart"></canvas>
score:1
unfortunately, there is no way to configure chart.js to produce a gantt chart because each dataset that you plot on the graph either starts at the origin (the min value set on the scale) or is stacked right on top of each other. you cannot configure a chart to stack bars but show them in different "lanes" (which is what a gantt chart would require).
even if you try to create multiple scales (which is looks like you are attempting), it will not work because the 2nd bar will still start at the origin (however the 2nd scale's origin would be the min value you specified). in other words, both bars will still touch the y axis.
you had several problems with you chart.js configuration. first, you cannot define scale config in a dataset. second, you tried to use several time scale options, but they must be inside the time
object.
i cleaned up everything just so you can see how to correctly define a chart. here is a codepen demonstrating this.
score:1
currently [november 2018] chart.js is not able todo so.
however if you are willing to apply a patch, there is a pull request pending, which would implement exactly this feature: https://github.com/chartjs/chart.js/pull/5262
for my purposes i adapted the code of https://github.com/chartjs/chart.js/blob/master/src/controllers/controller.bar.js
@@ -453,10 +453,12 @@ module.exports = function(chart) {
helpers.canvas.cliparea(chart.ctx, chart.chartarea);
- for (; i < ilen; ++i) {
- if (!isnan(scale.getrightvalue(dataset.data[i]))) {
- rects[i].draw();
- }
+ if (dataset.label != 'noshow') {
+ for (; i < ilen; ++i) {
+ if (!isnan(scale.getrightvalue(dataset.data[i]))) {
+ rects[i].draw();
+ }
+ }
}
helpers.canvas.uncliparea(chart.ctx);
utilizing bar chart like this:
var gapdemo= new chart(ctx, {
type: 'horizontalbar',
data: {
labels: ["todo1", "todo2", "todo3", "todo4"],
datasets: [{
type: 'horizontalbar',
label: 'noshow',
data: [1,2,4,3],
},{
type: 'horizontalbar',
label: 'showme',
data: [3,2,2,1],
},{
type: 'horizontalbar',
label: 'noshow',
data: [1,5,3,4],
},{
type: 'horizontalbar',
label: 'showme',
data: [2,2,1,4],
}]
},
options: {
scales: {
xaxes: [{
id: 'x-axis-1',
stacked: true
}],
yaxes: [{
id: 'y-axis-1',
stacked: true,
ticks: {beginatzero: true}
}]
}
}
});
this results in a chart like that: barchart with gaps
Source: stackoverflow.com
Related Query
- Chartjs - Set start and end value into a Bar Chart
- Chartjs Bar Chart Y Axis set base starting value to not be 0
- chartjs : how to set custom scale in bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Remove the label and show only value in tooltips of a bar chart
- Why would a ChartJS chart end up with width 0 and height 0?
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- Click on interactive chart.js bar chart and get value for labels and groups in JS
- How to set the chartjs bar graph scale to the highest value in the result data
- Bar chart with min height and zero values - ChartJs
- Set fixed label size for grouped bar chart in angular Chartjs
- Chart Js - Globally set bar chart color based on value
- Set max value of chart.js bar chart
- ChartJS : Hook before values are given to doughnut (absolute value in chart and real values in the tooltips)
- Chartjs : Can I overwrite bar value on hover for every bar chart
- Chartjs bar min, max and avarage value
- how can i show labels and value in both on bar chart
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- set scroll on bar chart in chartjs
- How to show symbols after the label and before the numeric value using chart.js Bar chart in Angular
- How to start the chart from specific time and offest hour and then show the data on chart from target datetime in chartjs
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- how to insert dynamic data from sql into chartjs stacked bar chart javascript
- Angular and ChartJS to create bar chart
- How to set minimal value for bar chart in Chart.js? (ver. 2.0.2)
- Chartjs - Doughnut chart with multi layer and running value
- Chartjs Bar Chart add background color to value labels
- How to set up an initial Zoom value for a chart (ChartJS + ChartJS Zoom plugin)?
- ChartJs bar chart bar value displaying lower then Y axis value in pdf?
More Query from same tag
- Chart.js nuget Package with angularJS
- Can Chart.js Horizontal bar work with time series data?
- Two chartjs charts on page throwing data undefined error
- Is it possible to add a drop shadow to chart.js line chart?
- How do I manipulate a specific tooltip value of a Chart.js pie chart?
- Chart.js Bar chart : display labels
- Chart.js pie chart overflowing div
- Is there a way to represent date hole in chartjs time series?
- Bar charts flickering issue in charts js
- Can't get Chart.js to run in a Vue.js component
- chart.js dynamically set values of suggestedMin and suggestedMax based on values of array
- displaying bar chart in laravel
- How scale sector in doughnut chartjs on hover?
- ChartJs x-axis label to be in dateTime format issue: DD-MMM-YYYY, but displayed as MMM-YYYY-DD
- Responsive ChartJS in Bootstrap Table Cell
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to fix bars in Chart.js with long labels
- Chart.js configuration
- How to add index labels(on top of bar charts) in Chart.js
- Chart JS is using 1,2,3 as y values for my points for the second dataset instead of their passed in labels
- Asp.net MVC and Chart js
- Regarding stacked chart in chart.js
- chartjs : uncaught exception: 0 and X are too far apart with stepSize of Y
- How to ignore same values on ChartJs?
- chart.js Hide empty Chart and keep previous data if no data? .net MVC
- Chart.js V3 Fill Below 0
- How to get the database data into ChartJS using codeigniter
- Chart.js multiple include prevention
- Destroy ChartJS rendered with Symfony
- Chart.js Multiple charts with one common legend