Ternary Contour Plots in JavaScript
How to create D3.js-based ternary contour plots. Examples of Ternary Contour Plots with plotly.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
var url = 'https://gist.githubusercontent.com/davenquinn/988167471993bc2ece29/raw/f38d9cb3dd86e315e237fde5d65e185c39c931c2/data.json';
var colors = ['#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3','#fdb462','#b3de69','#fccde5','#d9d9d9','#bc80bd','#ccebc5','#ffed6f'];
d3.json(url, function(err, rawData) {
if(err) throw err;
plot(rawData);
});
function plot(rawData) {
var data = Object.keys(rawData).map(function(k, i) {
var pts = rawData[k];
pts = pts.concat(pts[0]);
return {
type: 'scatterternary',
mode: 'lines',
name: k,
a: pts.map(function(d) { return d.clay }),
b: pts.map(function(d) { return d.sand }),
c: pts.map(function(d) { return d.silt }),
line: { color: '#444' },
fill: 'toself',
fillcolor: colors[i],
hoveron:'fills+points'
};
});
var layout = {
ternary: {
sum: 100,
aaxis: makeAxis('Clay'),
baxis: makeAxis('Sand'),
caxis: makeAxis('Silt')
},
showlegend: false,
width: 700,
annotations: [{
showarrow: false,
text: 'Soil Types Fill Plot',
x: 0.15,
y: 1.1
}]
};
Plotly.newPlot('myDiv', data, layout);
}
function makeAxis(title) {
return {
title: {
text: title
},
ticksuffix: '%',
min: 0.01,
linewidth: 2,
ticks: 'outside',
ticklen: 8,
showgrid: true,
};
}
Click to copy
Inspired from Daven Quinn's block
