score:7
Accepted answer
You could use a scatter chart that accepts the data as individual points, objects with properties x
and y
. To turn a scatter chart back to a line chart, simply define showLine: true
on each dataset
.
To product data as individual points, you can defined labels
outside of the chart and convert both value arrays using Array.map()
as follows.
valueArray.map((v, i) => ({ x: labels[i], y: v }))
The last thing to do is defining xAxis.ticks
as follows:
ticks: {
stepSize: 1,
autoSkip: false,
callback: value => value % 20 == 0 ? value : null,
min: labels[0],
max: labels[labels.length - 1],
maxRotation: 0
},
Please have a look at your amended code below:
const labels = [7, 14, 21, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 119, 126];
var myChart = new Chart('myChart', {
type: 'scatter',
data: {
datasets: [{
label: 'IC',
data: [9432.13209267, 1899.132847, 851.2122668, 3964.48968367, 9433, 8087.04121533, 1268.34642367, 825.29800317, 4271.00722867, 1157.57453933, 4928.214994, 783.88204033, 1846.623016, 4001.84214867, 709.70201067, 3917.61792167, 688.1571182].map((v, i) => ({ x: labels[i], y: v })),
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
showLine: true,
borderWidth: 1
},
{
label: 'Mtb',
data: [14241.41334667, 48348.08833, 5055.28134533, 7614.80689233, 14240, 24536.66203, 1083.99264, 144.72451603, 15968.94539333, 160.150183, 5127.77524033, 953.9963646, 0, 2989.36556, 21.32920023, 27.03159138, 60310.22952333].map((v, i) => ({ x: labels[i], y: v })),
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
showLine: true,
borderWidth: 1
}
]
},
options: {
scales: {
xAxes: [{
display: true,
position: 'bottom',
ticks: {
stepSize: 1,
autoSkip: false,
callback: value => value % 20 == 0 ? value : null,
min: labels[0],
max: labels[labels.length - 1],
maxRotation: 0
},
// afterBuildTicks: (axis, ticks) => [0, 20, 40, 60, 80, 100, 120],
scaleLabel: {
display: true,
labelString: 'Days from initial test',
fontSize: 16
}
}],
yAxes: [{
display: true,
position: 'left',
ticks: {
beginAtZero: true,
stepSize: 10000,
min: 0,
max: 70000
},
scaleLabel: {
display: true,
fontSize: 16
}
},
{
display: true,
position: 'right',
ticks: {
beginAtZero: true,
stepSize: 10000,
min: 0,
max: 70000
},
scaleLabel: {
display: true,
fontSize: 16
}
},
]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>
Source: stackoverflow.com
Related Query
- ChartJs - set x-axis ticks at specified/fixed intervals
- How to set ChartJS Y axis title?
- ChartJS: How to set fixed Y axis max and min
- ng2-charts: How to set fixed range for y axis
- Chartjs 2.7.3: Set Y data at the correct X position axis
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Set fixed label size for grouped bar chart in angular Chartjs
- How to set ChartJS Axis label with PrimeFaces
- Create ticks at certain time positions on a chartjs cartesian time axis
- How to set min max for x axis depend on data with Chartjs and Spring Boot?
- ChartJS fixed y axis on scroll
- ChartJS set default axis
- ChartJs - set background color of the space between ticks
- ChartJs line chart time cartesian axis number of ticks and wierd offset
- How to set min Value a step down of min value received from database in Y Axis in ChartJS
- Chart.JS - Set fixed X and Y axis values in time chart?
- Chartjs Bar Chart Y Axis set base starting value to not be 0
- How to set equal width between ticks - chartjs
- ChartJS set color of Line chart points depending on Y Axis
- How to set max and min value for Y axis
- In Chart.js set chart title, name of x axis and y axis?
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- chartjs : how to set custom scale in bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- show label in tooltip but not in x axis for chartjs line chart
- ChartJS - Set different hover-text than x-axis description
- Chart JS - set start of week for x axis time series
- ChartJS set yAxes "ticks"
- Chartjs change the specific label color in x axis in callback function
- How to show Y axis ticks for every point in the graph
More Query from same tag
- How do I resolve this error; 'document.getElementById('myChart').getContext' is undefined)?
- How to modify chartjs tooltip so i can add customized strings in tooltips
- Bootstrap grid not working with canvas
- How to insert arrays into objects that are inside an array? For ng2-charts / Charts.js
- React component wont re-render ChartJS chart with redux props
- How to set axes' step size in Chart.js 2?
- bootstrap ui+angular chart: is it possible to disable graph's auto refreshing?
- ChartJS - Why is the transition on the first dataset bad?
- Chartjs - Stacked bar chart data order causes invisible other values
- Chart JS: Ignoring x values and putting point data on first available labels
- How can I get suggestedMax number for yAxes?
- Show labels on each sector to polar chart using angular js chart
- Rails dual axis using Chartkick and chart js
- Chart js custom separate legend returns error when clicking legend
- How to place extra text on canvas (not using HTML) beside chart?
- Charts.js dynamic data call (Angular)
- Reduce spacing between bars in horizontal bar chart (chart.js)
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- Radar Chart, Chart.js v3.2 labels customization
- Chartjs v3 overlap stacked bar chart with groups
- How to show text likt this in Chartjs Dougnet chart
- Stepped Line with angular-chart.js
- Drawing line chart in chart.js with json response
- Chart.js (Customising tool tips of a stacked bar to represent each stacked data)
- How to show group by data just like sample image using Chart.js?
- How to keep rounded bar corners when data is filtered chartJs?
- Moving HTML with jQuery makes chart.js charts dissapear, no errors, unknown reason How can I redraw it?
- How to display data by current month and display no data message if data not exists
- My charts doesn't work after inserting X-axis