score:1
I was able to accomplish this in Chart.js 2.8.0:
Change data to a list of objects with
x
andy
. Since the intention is to swap x and y axis, put the "x data" in the y variable and vise verse. eg.[{x: 3, y: 2}, {x: 5, y: 7}, {x: 9, y: 10}]
Set the chart type to 'scatter'
Add
showLines: true
to theoptions
The outcome is a line chart where the line can be horizontal, vertical, or even double back on itself. The orientation of the line is defined by which values you put in x
and which you put in y
.
Here's an example configuration:
new Chart(document.getElementById('myChart'), {
type: 'scatter',
data: {
datasets: [
{
label: 'My Dataset',
data: [{x: 3, y: 2}, {x: 5, y: 7}, {x: 9, y: 10}],
},
],
},
options: {
showLines: true,
scales: {
xAxes: [
{
type: 'linear',
position: 'bottom',
},
],
yAxes: [
{
type: 'linear',
},
],
}
}
})
If you're using an older version of Chart.js that doesn't have scatter plots, you can use this plugin: http://dima117.github.io/Chart.Scatter/
score:2
The proposal by @dralth works fine with version 2.8.0, but for some reason showLines: true
does not work anymore since version 2.9.0.
It is still possible showing the lines by using the showLine
property for each dataset.
In case of @dralth's example it works as follows (tested with version 2.9.3):
new Chart(document.getElementById('myChart'), {
type: 'scatter',
data: {
datasets: [
{
label: 'My Dataset',
data: [{x: 3, y: 2}, {x: 5, y: 7}, {x: 9, y: 10}],
showLine: true
},
],
},
options: {
scales: {
xAxes: [
{
type: 'linear',
position: 'bottom',
},
],
yAxes: [
{
type: 'linear',
},
],
}
}
})
Source: stackoverflow.com
Related Query
- How do I remove cartesian axes from chart js?
- how to add second axes to chart JS
- How to print a chart rendered by code
- How do I destroy/update Chart Data in this chart.js code example?
- How to invert scale display on radar chart (chart.js)
- How to run Chart.js samples using source code
- how to not repeat code while creating multiple charts in chart js
- how to only show zero grid axes at center and hide all other gridlines in chart js
- How to add text inside the doughnut chart using Chart.js?
- How to clear a chart from a canvas so that hover events cannot be triggered?
- How to use two Y axes in Chart.js v2?
- Chart.js - How to set a line chart dataset as disabled on load
- chart js 2 how to set bar width
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- How can I make two of my lines in Chart JS thicker
- How to prevent first/last bars from being cut off in a chart with time scale
- How set color family to pie chart in chart.js
- How do I invert an y-axis using chart.js (v2.0-dev)
- Chart.js Bar Chart - how to chart bars from 0
- How to add an on click event to my Line chart using Chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- How do you hide the title of a chart tooltip?
- How to put rounded corners on a Chart.js Bar chart
- chartjs : how to set custom scale in bar chart
- How can I make a stepline or stepped chart in chart.js or D3?
- How to save Chart JS charts as image without black background using blobs and filesaver?
- How to Draw Gantt chart using chart js or other libraries
- How to fix chart Legends width-height with overflow scroll in ChartJS
- How to display Line Chart dataset point labels with Chart.js?
More Query from same tag
- Charts in Ionic 2
- Starting a Chart.js Time Scale at 0
- How to display date as label on x-axis in chart.js
- Getting dynamic data for chart.js from Django model
- Angular Chart.js - Remove Moment.js as Dependency / Reduce Bundle Size
- How to draw horizontal Lines using chart.js 3.x ? Cannot get it working
- How to display the labels in doughnut chart using ng2 charts?
- increasing php variable in javascript loop
- Chart.js multi line chart that has different labels
- Chart.js on web-app
- How to add a coloured legend box to a pie chart with Chart.js v1?
- chart.js label color change based on value
- Catch event of show and hide dataset at chart.js
- How to get tooltip on chart.js v1.x?
- Chart.js canvas : can't center it
- Adding object data to data structure of chart.js chart does not show any data
- Vue2 trying to set chart title
- chart looking strange on phone´s browser using chart.js
- ChartJS right Y-Axe ticks issue
- React COREUI CChart problem not rendering
- Displaying a chart using Chart.js
- Show the latest labels in a bar chart with React.js using react-chartjs
- ChartJS Annotation Hide /Show
- How to correctly passed php mysql data to ChartJs
- Real-time line chart with ChartJS using Ajax data
- Chart.js tooltipTemplate not working
- JavaScript variable in an object
- Have the time on x axes to show the last 24 hours from current time
- Send Chart.js as HTML and then render via ajax
- Animate Chartist on show