score:4
Accepted answer
You can convert your target values into floating bars where individual bars are specified with the syntax [min, max]
. Given an array of values named targetData
, it can be converted with Array.map()
as follows.
targetData.map(v => [v - 1, v + 1])
Further you need to define individual stacked xAxis
for both datasets. In order to have the original target values displayed in the tooltips, a toolips.callback.label
function is also needed.
Please have a look at below runnable code snippet.
const actualData = [100, 180, 120, 80, 140, 170, 160];
const targetData = [110, 150, 140, 100, 120, 110, 170];
new Chart("chart", {
type: "bar",
data: {
labels: ["KPI 1", "KPI 2", "KPI 3", "KPI 4", "KPI 5", "KPI 6", "KPI 7"],
datasets: [{
label: "Actual",
backgroundColor: 'rgba(0, 0, 255, 0.2)',
data: actualData,
xAxisID: "x-axis-actual"
},
{
label: "Target",
backgroundColor: 'rgba(255, 0, 128, 1)',
data: targetData.map(v => [v - 1, v + 1]),
xAxisID: "x-axis-target"
}
]
},
options: {
tooltips: {
callbacks: {
label: (tooltipItem, data) => {
const v = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
return Array.isArray(v) ? (v[1] + v[0]) / 2 : v;
}
}
},
scales: {
xAxes: [{
id: "x-axis-target",
stacked: true
},
{
display: false,
offset: true,
stacked: true,
id: "x-axis-actual",
gridLines: {
offsetGridLines: true
}
}
],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="120"></canvas>
Source: stackoverflow.com
Related Query
- chart.js - Horizontal lines per bar
- Horizontal stacked bar chart with chart.js
- Horizontal bar chart in chart.js
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Creating a horizontal bar chart extension on Chart.js
- How to draw Horizontal line on Bar Chart Chartjs
- Stacked horizontal bar chart along total count with chart.js
- Chart.js: Minimum value for x-axis at horizontal stacked bar chart
- two xAxes horizontal bar chart Charts.js
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- How to change Chart.js horizontal bar chart Width?
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- to increase space between x axis and first horizontal bar in chart js
- Draw a horizontal bar chart from right to left
- Angular-chart / line chart with multiple horizontal lines (margins)
- Remove padding from chartJs horizontal bar chart
- Horizontal bar with two yaxes chart js
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- ChartJS horizontal chart display legend on each bar
- Chart.js horizontal line chart or modified horizontal bar chart
- ChartJS: how to make a bar chart with a horizontal guideline:
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Stacked Mixed Horizontal Bar Chart in Charts.Js Axes Positioning
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- change space between horizontal (grid) lines for line chart
- Chart.js - Horizontal line on Bar chart interferes with tooltip
- Chart.js - draw horizontal line in Bar Chart (type bar)
- Create stacked bar chart with a single dataset per stack
- chart.js-plugin-annotations multiple horizontal lines in one chart
- Horizontal gradient for every single bar in group chart js
More Query from same tag
- chart.js version 3 - How to make an overlapping bar chart?
- Regrouping data chartjs
- Creating charts dynamically using ChartJS and AngularJS
- Chartjs removing labels and begin number
- Remove radar chart labels in chart.js
- How to make chartjs pie chart responsive
- How do i programmatically set bar fillColor in chartJs + angular
- Take image of a chart.js from canvas and display it as an image
- connect filter to multiple charts asp.net core mvc
- ChartJS: Custom legend not showing Labels for Multi-Pie chart
- Bootstrap 3 tabs & Chart.js - charts not loading on tabs
- The data that is called in chart does not match the database
- Plotting Dashed Vertical and Horizontal lines on line graph for single point
- Chart.js add padding to scales
- trouble passing API result to chart
- Link in ChartJS tooltip not clickable
- ChartJS doesnt display labels provided by Jinja variable
- Is it possible to shorten outer labels on Radar graph using Chart.js, without affecting the other labels?
- Chart.js Formatting dual axis and labels
- Sending 2 fetched result into AJAX
- chartjs dataset from single line to multiple line
- How to combine lines with different starting points in chartjs
- Javascript not populate chart after retrieve json data
- Chart.js mouseover tooltips appears when cursor in wrong position
- everything i put in the options of the Line charts doesnt work
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Chart.js 3.x custom positioners: cannot use them in TypeScript
- How to put dynamic value in Doughnutchart Using chartjs?
- How to make bars change color based on value in chartsjs
- Can't display data labels in chartJS