score:9
Accepted answer
Your X axis scale was indeed invalid, you declared it as an array which is V2 syntax, in V3 all the scales are their own object where the key of the object is the scaleID. So removing the array brackets around the x axis scale object will resolve the issue.
Also your legend and tooltip config where wrong and in the wrong place, also V2 syntax.
For all changes please read the migration guide
const gainedChart = document.getElementById('gained-chart').getContext('2d');
const gain = [74.699997, 76.580002, 81.349998, 83.000000, 85.879997, 83.120003, 77.989998, 81.279999];
const dates = ["Jan 2, 20", "Feb 3, 20", "Mar 4, 20", "Apr 5, 20", "May 6, 20", "Jun 9, 20", "Nov 10, 20", "Dec 11, 20"];
let gainData = {
datasets: [{
label: "Gain (%)",
data: gain,
borderColor: 'rgba(255, 66, 66, 1)',
backgroundColor: 'rgba(255, 66, 66, 1)',
pointRadius: 1.2,
pointBackgroundColor: 'rgba(255, 66, 66, 0.8)',
pointHoverRadius: 6,
pointHitRadius: 20,
pointBorderWidth: 2,
}]
};
let gainConfig = {
plugins: {
legend: {
display: true,
position: 'top',
labels: {
boxWidth: 80,
color: 'black'
},
},
tooltip: {
callbacks: {
label: function(tooltipItem, data) {
return parseInt(tooltipItem.parsed.y)
}
}
},
},
scales: {
x: {
type: 'time',
time: {
minUnit: 'month'
}
},
y: {
suggestedMax: 45,
ticks: {
stepSize: 5,
//max: 100
},
},
},
maintainAspectRatio: false,
responsive: true,
};
let lineGainChart = new Chart(gainedChart, {
type: 'line',
data: gainData,
options: gainConfig,
plugins: [{
beforeInit: function(lineGainChart) {
for (let c = 0; c < dates.length; c++) {
let myMoment = moment(dates[c], 'MMM D, YYYY');
lineGainChart.data.labels.push(myMoment);
}
}
}]
});
<canvas class="graph-canvas" id="gained-chart" width="400" height="400"></canvas>
<script src="https://cdn.jsdelivr.net/npm/moment@2.29.1/moment.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-moment@^1"></script>
Source: stackoverflow.com
Related Query
- ChartJS 3+ x-axis only showing full moment object instead of just month
- Datalabels plugin chartjs showing '[object]' instead of value
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Using chartjs v2 to show categorical values on axis instead of numeric
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Only 1 out of 2 ChartJS i showing
- Chartjs only showing dots, line not showing
- Chartjs chart with only one axis
- Showing only entire units on ChartJS
- ChartJS time axis not plotting correctly | getting an hour instead of date
- VueJS + Chartjs - Chart only renders after code change
- My Chartjs is not updating after setstate full code attached Reactjs Update
- ChartJS with React: Only one value showing on time chart
- How to set ChartJS Y axis title?
- Chartjs Bar Chart showing old data when hovering
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- ChartJs title not showing
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chartjs linechart with only one point - how to center
- ChartJS : How to leave just points without lines
- show label in tooltip but not in x axis for chartjs line chart
- chartjs + Angular6 is not showing charts or any error
- Ng2-Charts Linechart only showing first 2 two values
- Using Chart.js - The X axis labels are not all showing
- ChartJs bar chart - keep bars left instead of equally spread across the width
- ChartJs canvas showing previous graph when changing Graph types
- Chartjs change the specific label color in x axis in callback function
- Chartjs 2: Multi level/hierarchical category axis in chartjs
- Line chart is showing under bar in combochart in chartjs
More Query from same tag
- Dynamically update chart in chartJs
- Chart Js Doughnut chart giving error
- how to display labels at top of charts(chart.js)
- Chartjs tooltip anchor point position on bar charts
- Chart.js not displaying Django Rest API data
- Chartjs bar min, max and avarage value
- chart.js how to force min and max y axis values
- Create PDF with mpdf and chart.js
- How to use Chart.js with Typescript without getting assignable errors?
- Chartjs change horizontal axis value
- Why are the default Chart.js legend boxes transparent rectangles?
- Use chart.js with freemarker tamplates
- Removing ChartJS 2 border and shadow from point style legend
- How to Convert an Array of Objects into an Object of Associative Arrays in Javascript
- chart.js cuts off canvas - but padding makes doughnut very small
- ChartJS automatically scaled chart has undefined min and max
- Vuejs - Chart.js wrapper vue3-chart-v2 not displaying labels when data are passed as object
- How to set a length difference in chart?
- react chart js skip zero value month
- Create Chart using $getJSON and ChartJS
- How to pass values to a chart (chart.js / morris.js)
- Chart js options not changing chart
- Chart.js horizontal bar width
- ChartJS replay chart animation when called by show()
- How can i get my chartjs to show up graphically?
- How to apply each color to each dataset
- Add space Between Columns in Bar chart. chartjs
- How to set the pointDot option on combo stacked bar-line chart
- Chart js show data on hovering legend
- How to display data from top to bottom in stacked bar-chart QuickChart?