Chart.js-Streudiagramm
Ein Streudiagramm besteht aus mehreren Koordinatenpunkten, die durch zwei Datensätze gebildet werden. Durch die Untersuchung der Verteilung der Koordinatenpunkte wird ermittelt, ob ein Zusammenhang zwischen den beiden Variablen besteht, oder das Verteilungsmuster der Koordinatenpunkte zusammengefasst.
Datenstruktur:
data: [{
x: 10,
y: 20
}, {
x: 15,
y: 10
}]
StreudiagrammtypeDas Attribut istscatter。
const config = {
type: 'scatter',
data: data,
options: {
scales: {
x: {
type: 'linear',
position: 'bottom'
}
}
}
};
Als Nächstes erstellen wir ein einfaches Streudiagramm:
Beispiel
const ctx = document.getElementById('myChart');
const data = {
datasets: [{
label: 'Streudiagramm-Beispiel',
data: [{
x: -10,
y: 0
}, {
x: 0,
y: 10
}, {
x: 10,
y: 5
}, {
x: 0.5,
y: 5.5
}],
backgroundColor: 'rgb(255, 99, 132)'
}],
};
const config = {
type: 'scatter',
data: data,
options: {
responsive: true, // Diagramm responsiv machen, sodass es sich an den Bildschirm anpasst
maintainAspectRatio: false,// Das ursprüngliche Seitenverhältnis des Diagramms beibehalten
scales: {
x: {
type: 'linear',
position: 'bottom'
}
}
}
};
const myChart = new Chart(ctx, config);
const data = {
datasets: [{
label: 'Streudiagramm-Beispiel',
data: [{
x: -10,
y: 0
}, {
x: 0,
y: 10
}, {
x: 10,
y: 5
}, {
x: 0.5,
y: 5.5
}],
backgroundColor: 'rgb(255, 99, 132)'
}],
};
const config = {
type: 'scatter',
data: data,
options: {
responsive: true, // Diagramm responsiv machen, sodass es sich an den Bildschirm anpasst
maintainAspectRatio: false,// Das ursprüngliche Seitenverhältnis des Diagramms beibehalten
scales: {
x: {
type: 'linear',
position: 'bottom'
}
}
}
};
const myChart = new Chart(ctx, config);
Ausprobieren »
Das Ausgabeergebnis des obigen Beispiels ist:
Andere Erweiterungen