score:3
Accepted answer
For the X axis on top you can just add another X axis and set position to top, for the labels between the Y axis best is to write a custom plugin for that.
Example:
var options = {
type: 'line',
data: {
labels: ["A", "B", "C", "D", "E", "F"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
scales: {
x: {
position: 'bottom',
grid: {
offset: true // offset true to get labels in between the lines instead of on the lines
}
},
x2: {
position: 'top',
grid: {
offset: true // offset true to get labels in between the lines instead of on the lines
}
},
y: {
ticks: {
count: (context) => (context.scale.chart.data.labels.length + 1)
}
}
},
plugins: {
labelsY: {
font: 'Arial',
size: '14px',
color: '#666',
align: 'right',
reverseLabels: false // true to make A start at top and F at bottom
}
}
},
plugins: [{
id: 'labelsY',
afterDraw: (chart, args, options) => {
const {
ctx,
scales: {
y,
x
},
data: {
labels
}
} = chart;
let dupLabels = JSON.parse(JSON.stringify(labels)); // remove pointer to internal labels array so you dont get glitchy behaviour
if (options.reverseLabels) {
dupLabels = dupLabels.reverse();
}
dupLabels.forEach((label, i) => {
ctx.save();
ctx.textAlign = options.align || 'right';
ctx.font = `${options.size || '20px'} ${options.font || 'Arial'}`;
ctx.fillStyle = options.color || 'black'
let xPos = x.getPixelForValue(labels[0]) - ctx.measureText(label).width;
let yPos = (y.getPixelForValue(y.ticks[i].value) + y.getPixelForValue(y.ticks[i + 1].value)) / 2;
ctx.fillText(label, xPos, yPos)
ctx.restore();
});
}
}]
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.3.0/chart.js"></script>
</body>
Source: stackoverflow.com
Related Query
- ChartJS multiple X axis and extra labels in y axis
- ChartJS - Scale x axis labels from single days to multiple months
- Grouping y-axis labels on multiple lines on a ChartJS with an extra dimension
- Adjusting the font size and family of extra fillText labels in ChartJS
- ChartJS Tooltips with time axis and multiple datasets
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to remove gridlines and grid labels in Chartjs Radar?
- Change font family of labels in piechart and linechart in chartjs
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Why is borderColor function in chartjs running multiple times, and how to reduce it?
- Chartjs with multiple y axes and different scales
- chartjs time cartesian axis adapter and date library setup
- In Chart.js >3.0, on axis of type time, how to show labels and ticks only for existing data points (make labels reflect data)?
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- ChartJS - Finding the minimum and maximum labels that are inside a pan (When zooming)
- Setting Common labels and background color common for all the charts in ChartJs
- How to draw multiple line on y axis for same x axis in chartjs v2?
- ChartJS shows date, but not time, when displaying labels in time axis
- Is there any way to change the font color and size of labels in Chartjs 3.0.0
- How to display multiple y axis titles on seperate lines and rotated 90 degrees clockwise in chart.js
- How to change fonts and axis labels for Chart.js image rendering with QuickChart?
- Chartjs React Typescript moving x axis labels above chart
- ChartJS multiple sub labels for label
- ChartJS adding shadow color to grid and custom x-axis labels
- ChartJS have xAxes labels match data source
- Chartjs removing labels and begin number
- ChartJs - Labelling minimum and maximum value of the Y Axis
- ChartJS - x axis labels orientation
More Query from same tag
- Creating Chart.js using API data in react
- How to skip labels of a line in multiline graph in chartjs?
- Chart js options not changing chart
- How to assign a logarithmic scale to y axis in chart.js?
- Chart.js add icons like <i class='icon-peso'></i> to y-axis labels
- how to hide specific dataset based on condition chartjs angular
- Getting the HTML code of a chart created by chart.js
- Stacked bar chart with chartjs with included values
- How to solve Uncaught ReferenceError in console?
- Chart JS not working with Phonegap
- Referencing locally declared variable into an array for data for chartjs
- How can I debug maxTicksLimits in charts.js?
- Chart load time and animation slow on mobile device
- Chart.js, adding footer to chart
- Space between start and end of axis
- Getting a Chart is not defined ReferenceError with in React + React-chartjs
- Create bar chart with chart.js where space per bar is the same, overall chart size adjusted
- Create Chart using Reactjs Chartjs and axios
- when showing large amount of data graph not displaying from middle
- Chart.js line graph-removing labels on the line
- Convert a dynamic piechart from CanvasJS to Google Charts?
- Group Bar Chart Dinamic Data Update By Button Fuction ChartJS
- chart.js onAnimationProgress progress percentage
- Is it possible to shorten outer labels on Radar graph using Chart.js, without affecting the other labels?
- Using chart js options with react-chartjs-2 and typescript
- Show multiple line graphs on the same chart using chart JS
- How Export an Editable Chart with content to PPT and Excel
- ChartJs Skip few category, possible?
- How to 1. pass two datasets and 2.have permanent label on charts in Chartjs
- How to create a chart-js pie chart with PHP data variables?