score:7
Accepted answer
Edit: Below Accepted version no longer works above version ^2.7.
- For latest version 2.9 scatter series no longer display lines. So you need to use Line series with linear scale. So your options would like be below:
var randomColor = function(opacity) {
return 'rgba(' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + (opacity || '.3') + ')';
};
var scatterChartData = {
datasets: [{
label: "My First dataset",
fill: false,
lineTension: 0,
borderWidth: 3,
data: [{
x: 0.328,
y: 0.2
},{
x: 0.328,
y: 1.0
},
{
x: -0.328,
y: 1.0
},
{
x: -0.328,
y: 0.2
},
{
x: 0.0,
y: 0.0
},
]
}]
};
$.each(scatterChartData.datasets, function(i, dataset) {
dataset.borderColor = randomColor(0.4);
dataset.backgroundColor = randomColor(0.1);
dataset.pointBorderColor = randomColor(0.7);
dataset.pointBackgroundColor = randomColor(0.5);
dataset.pointBorderWidth = 1;
});
window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
window.myScatter = Chart.Line(ctx, {
data: scatterChartData,
options: {
title: {
display: true,
text: 'Chart.js Scatter Chart'
},
scales: {
xAxes: [{
type: 'linear',
position: 'top',
gridLines: {
zeroLineColor: "rgba(0,255,0,1)"
},
scaleLabel: {
display: true,
labelString: 'x axis'
}
}],
yAxes: [{
type: 'linear',
position: 'right',
gridLines: {
zeroLineColor: "rgba(0,255,0,1)"
},
scaleLabel: {
display: true,
labelString: 'y axis'
}
}]
}
}
});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script>
<div style="width:75%">
<div>
<canvas id="canvas"></canvas>
</div>
</div>
- From 2.1.3 to 2.7 you need to use
showLines: true
in your options object in the original answer.
Original answer:
This is possible out of the box with chart.js 2.x using the ability to pass x
and y
attributes with the data.
Here is an exmaple taken from the chart.js examples
var randomScalingFactor = function() {
return Math.round(Math.random() * 10);
};
var randomColor = function(opacity) {
return 'rgba(' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + (opacity || '.3') + ')';
};
var scatterChartData = {
datasets: [{
label: "My First dataset",
data: [{
x: 0.4,
y: randomScalingFactor(),
}, {
x: 1.6,
y: randomScalingFactor(),
}, {
x: 4.7,
y: randomScalingFactor(),
}, {
x: 9.2,
y: randomScalingFactor(),
}, {
x: 20.1,
y: randomScalingFactor(),
}, {
x: 44.5,
y: randomScalingFactor(),
}, {
x: 155.3,
y: randomScalingFactor(),
}]
}]
};
$.each(scatterChartData.datasets, function(i, dataset) {
dataset.borderColor = randomColor(0.4);
dataset.backgroundColor = randomColor(0.1);
dataset.pointBorderColor = randomColor(0.7);
dataset.pointBackgroundColor = randomColor(0.5);
dataset.pointBorderWidth = 1;
});
window.onload = function() {
var ctx = document.getElementById("canvas").getContext("2d");
window.myScatter = Chart.Scatter(ctx, {
data: scatterChartData,
options: {
title: {
display: true,
text: 'Chart.js Scatter Chart'
},
scales: {
xAxes: [{
position: 'bottom',
gridLines: {
zeroLineColor: "rgba(0,255,0,1)"
},
scaleLabel: {
display: true,
labelString: 'x axis'
}
}],
yAxes: [{
position: 'left',
gridLines: {
zeroLineColor: "rgba(0,255,0,1)"
},
scaleLabel: {
display: true,
labelString: 'y axis'
}
}]
}
}
});
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.3/Chart.bundle.js"></script>
<div style="width:75%">
<div>
<canvas id="canvas"></canvas>
</div>
</div>
Source: stackoverflow.com
Related Query
- How to have chart.js automatically build x-axis labels based on x,y dataset?
- How to display Line Chart dataset point labels with Chart.js?
- How to dynamically set ChartJs line chart width based on dataset size?
- how to minimize x axis labels to day hours in chart js
- chart.js: How do I make the radar chart axis labels bigger?
- How to change text colour of specific labels of axis in Chart JS?
- ng2 scatter chart how to put labels in x axis
- How can I automatically rescale the y axis of my log chart using ChartJS?
- Chart.js - How to set a line chart dataset as disabled on load
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart.js how to show cursor pointer for labels & legends in line chart
- How to add an offset to a dataset in Chart js
- How to wrap X axis labels to multi-lines (X axis label formatting) in ng2-Charts?
- How do I change the 'months' language displayed on the date axis in Chart JS?
- Changing x axis labels in Chart.js line chart
- How to display data labels outside in pie chart with lines in ionic
- Line chart with large number of labels on X axis
- How to align Chart.JS line chart labels to the center
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to display the labels in doughnut chart using ng2 charts?
- How to have solid colored bars in angular-chart bar chart
- how to change Y axis value dynamically based on user input in Chartjs for Line chart?
- Chart.js Radar Chart How to Remove Outer Labels
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- How put dataset labels into multiTooltipTemplate?
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to add images to chart labels with vue-chartjs?
- Chart.js - bind data in dataset to specific chart labels
More Query from same tag
- Uncaught ReferenceError: Chart is not defined in Laravel mix
- chart.js chart-click pass parameter in Angular controller
- How to display the more then one value inside tooltip in bar chart.js?
- Chart.JS Bar Chart Throwing Error About Missing )
- Chart Js , repeated labels yAxis
- chart.js Multiple barchart in time scale won't display all
- Chart.js piechart, only have border on the outside of arc
- Chart.js: different dataset size
- Create a arc like doughnut chart with Chart js plugins
- Remove tooltip if has no label
- Chart js not working properly in Safari
- Getting (X,Y) point on mouse click on chart
- Chart.js returns a console error and does not display the chart when using variables as data input
- Chart.js get the nearest point when clicked on canvas
- chart.js Line chart doesn't display line past a certain point in the chart
- How to install plugin for chartjs in javascript
- Chart.js - Add text/label to bubble chart elements without using tooltips?
- Chart section is empty
- Chart.js is it possible to style the labels?
- How to draw multiple lines in chart.js
- How to address type errors when removing data with chart.js?
- Trying to call API and plot a line chart but it does not show
- Can JS lib can be use in Native Tizen App?
- How to change draw order by line property with chart.js
- Plot Multiple Line Chart in Ionic 3 with ChartJS
- Click on chart to expand more information - Chart.js
- Reading converted rss to json file with chart.js not working
- How to make bars dashed in chart js?
- Chart.js: Creating a Barchart with overlaying and offset Bars
- Pass data to Chart Js Laravel