Public
Edited
Mar 27, 2024
Insert cell
Insert cell
{
const data = [
{"year": 1940, "Democrat": 30, "Republican": 20, "Independent": 50},
// Add your data points here
{"year": 2014, "Democrat": 33, "Republican": 29, "Independent": 38}
];

// Parse the year
data.forEach(function(d) {
d.year = d3.timeParse("%Y")(d.year);
});

// Set the dimensions and margins of the graph
const margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

// Create the SVG element separately
const svg = d3.select(DOM.svg(width + margin.left + margin.right, height + margin.top + margin.bottom));

// Then append a 'g' element to offset for margins
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);

// Set the ranges
const x = d3.scaleTime().range([0, width]);
const y = d3.scaleLinear().range([height, 0]);

// Define the line
const valueline = d3.line()
.x(d => x(d.year))
.y(d => y(d.Democrat));

// Scale the range of the data
x.domain(d3.extent(data, d => d.year));
y.domain([0, d3.max(data, d => Math.max(d.Democrat, d.Republican, d.Independent))]);

// Add the valueline path using the 'g' element
g.append("path")
.data([data])
.attr("class", "line")
.attr("d", valueline)
.attr("fill", "none")
.attr("stroke", "steelblue");

// Add the X Axis using the 'g' element
g.append("g")
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(x));

// Add the Y Axis using the 'g' element
g.append("g")
.call(d3.axisLeft(y));

// Tooltip div for the chart
const tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);

// Adding circles for each data point for interactivity, using the 'g' element
g.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("r", 5)
.attr("cx", d => x(d.year))
.attr("cy", d => y(d.Democrat))
.on("mouseover", (event, d) => {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(`Year: ${d3.timeFormat("%Y")(d.year)}<br>Democrat: ${d.Democrat}%`)
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", d => {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});

// Now, correctly return the SVG element
return svg.node();}

Insert cell
data_areaRange = [
[1483232400000, 1.4, 4.7],
[1483318800000, -1.3, 1.9],
[1483405200000, -0.7, 4.3],
[1483491600000, -5.5, 3.2],
[1483578000000, -9.9, -6.6],
[1483664400000, -9.6, 0.1],
[1483750800000, -0.9, 4.0],
[1483837200000, -2.2, 2.9],
[1483923600000, 1.3, 2.3],
[1484010000000, -0.3, 2.9],
[1484096400000, 1.1, 3.8],
[1484182800000, 0.6, 2.1],
[1484269200000, -3.4, 2.5],
[1484355600000, -2.9, 2.0],
[1484442000000, -5.7, -2.6],
[1484528400000, -8.7, -3.3],
[1484614800000, -3.5, -0.3],
[1484701200000, -0.2, 7.0],
[1484787600000, 2.3, 8.5],
[1484874000000, 5.6, 9.5],
[1484960400000, 0.4, 5.8],
[1485046800000, 0.1, 3.1],
[1485133200000, 1.5, 4.1],
[1485219600000, -0.2, 2.8],
[1485306000000, 2.3, 10.3],
[1485392400000, -0.8, 9.4],
[1485478800000, -1.3, 4.6],
[1485565200000, -0.6, 5.3],
[1485651600000, 1.4, 5.8],
[1485738000000, -3.6, 0.9],
[1485824400000, -5.4, -2.6],
[1485910800000, -5.5, 0.8],
[1485997200000, -0.8, 2.5],
[1486083600000, 1.6, 4.1],
[1486170000000, 1.3, 4.8],
[1486256400000, 0.7, 4.6],
[1486342800000, -1.1, 4.5],
[1486429200000, -5.5, -0.9],
[1486515600000, -8.6, -4.2],
[1486602000000, -8.3, -1.6],
[1486688400000, -4.0, 0.9],
[1486774800000, -5.3, -0.5],
[1486861200000, -5.8, 0.4],
[1486947600000, -4.4, 0.4],
[1487034000000, -4.3, 0.6],
[1487120400000, -4.6, 2.7],
[1487206800000, 1.6, 3.2],
[1487293200000, 2.3, 6.2],
[1487379600000, 2.4, 4.1],
[1487466000000, 2.3, 8.5],
[1487552400000, 3.8, 6.8],
[1487638800000, 2.4, 4.9],
[1487725200000, 0.4, 4.5],
[1487811600000, -5.9, 4.0],
[1487898000000, -5.9, 1.5],
[1487984400000, -6.2, -1.7],
[1488070800000, -4.5, 2.3],
[1488157200000, 0.9, 5.6],
[1488243600000, 1.6, 4.9],
[1488330000000, -0.2, 5.5],
[1488416400000, -1.8, 4.4],
[1488502800000, -1.9, 3.3],
[1488589200000, -4.4, 2.5],
[1488675600000, 0.7, 4.1],
[1488762000000, -6.0, 2.7],
[1488848400000, -8.1, -0.1],
[1488934800000, -10.2, -0.5],
[1489021200000, -2.3, 3.7],
[1489107600000, 0.3, 4.7],
[1489194000000, -1.9, 5.2],
[1489280400000, 1.1, 3.1],
[1489366800000, 2.3, 7.0],
[1489453200000, 3.1, 7.7],
[1489539600000, 2.8, 6.3],
[1489626000000, 1.9, 8.9],
[1489712400000, 0.8, 5.5],
[1489798800000, -1.6, 4.3],
[1489885200000, -1.9, 3.3],
[1489971600000, 0.5, 4.4],
[1490058000000, 1.7, 6.6],
[1490144400000, 0.4, 5.1],
[1490230800000, -1.2, 3.5],
[1490317200000, 0.9, 7.4],
[1490403600000, 3.3, 8.4],
[1490490000000, 4.6, 10.1],
[1490572800000, 3.9, 11.8],
[1490659200000, 1.3, 8.9],
[1490745600000, 1.4, 9.8],
[1490832000000, -0.1, 2.4],
[1490918400000, 1.9, 6.8],
[1491004800000, 4.3, 12.2],
[1491091200000, 5.4, 9.3],
[1491177600000, 2.5, 11.1],
[1491264000000, 5.6, 10.2],
[1491350400000, 3.7, 7.6],
[1491436800000, 3.3, 7.4],
[1491523200000, 6.9, 12.0],
[1491609600000, 5.8, 10.9],
[1491696000000, 7.5, 12.1],
[1491782400000, 2.3, 9.7],
[1491868800000, 2.1, 7.1],
[1491955200000, 0.4, 6.9],
[1492041600000, 3.0, 7.0],
[1492128000000, -1.2, 8.9],
[1492214400000, 0.4, 7.5],
[1492300800000, 0.9, 4.4],
[1492387200000, -0.9, 8.2],
[1492473600000, -1.7, 9.6],
[1492560000000, 2.2, 7.5],
[1492646400000, 3.9, 9.1],
[1492732800000, 2.3, 6.5],
[1492819200000, 2.0, 7.4],
[1492905600000, 1.6, 6.9],
[1492992000000, 1.4, 4.7],
[1493078400000, -0.9, 9.7],
[1493164800000, 1.8, 7.9],
[1493251200000, 0.5, 10.6],
[1493337600000, 0.8, 11.0],
[1493424000000, 4.3, 11.6],
[1493510400000, -1.6, 11.7],
[1493596800000, -0.6, 14.3],
[1493683200000, 1.1, 16.1],
[1493769600000, 1.7, 17.1],
[1493856000000, 3.1, 17.6],
[1493942400000, 5.3, 19.2],
[1494028800000, 5.6, 20.3],
[1494115200000, 5.4, 18.0],
[1494201600000, 2.9, 12.1],
[1494288000000, 0.9, 8.6],
[1494374400000, 1.2, 7.7],
[1494460800000, 3.3, 10.1],
[1494547200000, 2.7, 14.1],
[1494633600000, 2.5, 16.6],
[1494720000000, 9.0, 12.7],
[1494806400000, 8.2, 17.0],
[1494892800000, 9.0, 11.8],
[1494979200000, 9.3, 16.9],
[1495065600000, 10.2, 12.1],
[1495152000000, 9.9, 20.3],
[1495238400000, 9.0, 23.1],
[1495324800000, 9.9, 16.9],
[1495411200000, 7.2, 12.5],
[1495497600000, 3.3, 18.1],
[1495584000000, 8.3, 12.9],
[1495670400000, 8.5, 13.1],
[1495756800000, 10.5, 16.1],
[1495843200000, 10.3, 22.3],
[1495929600000, 9.0, 18.2],
[1496016000000, 12.3, 15.6],
[1496102400000, 12.5, 16.3],
[1496188800000, 9.9, 13.6],
[1496275200000, 3.7, 14.8],
[1496361600000, 8.6, 14.3],
[1496448000000, 10.0, 18.3],
[1496534400000, 9.7, 13.0],
[1496620800000, 9.0, 14.3],
[1496707200000, 8.3, 14.3],
[1496793600000, 11.1, 18.4],
[1496880000000, 10.7, 16.6],
[1496966400000, 7.2, 16.3],
[1497052800000, 11.5, 15.3],
[1497139200000, 11.1, 15.1],
[1497225600000, 10.4, 16.5],
[1497312000000, 11.3, 14.6],
[1497398400000, 9.1, 17.6],
[1497484800000, 9.9, 15.5],
[1497571200000, 12.3, 14.9],
[1497657600000, 10.5, 15.1],
[1497744000000, 11.4, 18.0],
[1497830400000, 9.9, 14.8],
[1497916800000, 8.1, 12.4],
[1498003200000, 8.6, 15.5],
[1498089600000, 9.4, 13.0],
[1498176000000, 11.2, 13.0],
[1498262400000, 9.0, 15.3],
[1498348800000, 7.7, 13.6],
[1498435200000, 10.3, 13.6],
[1498521600000, 6.3, 18.0],
[1498608000000, 5.5, 21.7],
[1498694400000, 9.1, 23.2],
[1498780800000, 12.6, 25.4],
[1498867200000, 10.6, 19.6],
[1498953600000, 11.3, 14.5],
[1499040000000, 9.2, 16.1],
[1499126400000, 8.7, 17.9],
[1499212800000, 6.9, 19.5],
[1499299200000, 6.6, 20.0],
[1499385600000, 9.7, 15.7],
[1499472000000, 11.9, 14.2],
[1499558400000, 8.7, 14.2],
[1499644800000, 9.7, 18.4],
[1499731200000, 10.3, 16.8],
[1499817600000, 10.9, 15.5],
[1499904000000, 10.2, 16.8],
[1499990400000, 6.0, 18.9],
[1500076800000, 9.1, 19.1],
[1500163200000, 11.6, 15.7],
[1500249600000, 8.8, 12.4],
[1500336000000, 9.2, 16.3],
[1500422400000, 6.2, 23.0],
[1500508800000, 8.2, 26.2]
]
Insert cell
Highcharts = {
const Highcharts = await require("highcharts");
await require("highcharts/highcharts-more.js");
await require("highcharts/highcharts-3d.js");

await require("highcharts/modules/exporting.js");
await require("highcharts/modules/export-data.js");
await require("highcharts/modules/accessibility.js");

await require("highcharts/modules/arc-diagram.js");
await require("highcharts/modules/histogram-bellcurve.js");
await require("highcharts/modules/bullet.js");
await require("highcharts/modules/cylinder.js");
await require("highcharts/modules/dependency-wheel.js");
await require("highcharts/modules/dumbbell.js");
await require("highcharts/modules/funnel.js");
await require("highcharts/modules/funnel3d.js");
await require("highcharts/modules/heatmap.js");
await require("highcharts/modules/item-series.js");
await require("highcharts/modules/lollipop.js");
await require("highcharts/modules/networkgraph.js");
await require("highcharts/modules/organization.js");
await require("highcharts/modules/pareto.js");
await require("highcharts/modules/pyramid3d.js");
await require("highcharts/modules/sankey.js");
await require("highcharts/modules/solid-gauge.js");
await require("highcharts/modules/streamgraph.js");
await require("highcharts/modules/sunburst.js");
await require("highcharts/modules/tilemap.js");
await require("highcharts/modules/timeline.js");
await require("highcharts/modules/treemap.js");
await require("highcharts/modules/variable-pie.js");
await require("highcharts/modules/variwide.js");
await require("highcharts/modules/vector.js");
await require("highcharts/modules/venn.js");
await require("highcharts/modules/windbarb.js");
await require("highcharts/modules/wordcloud.js");
await require("highcharts/modules/xrange.js");
return Highcharts;
}
Insert cell
maxWidth = 700
Insert cell
viewof areaRangeLine = Inputs.radio(["arearange", "areasplinerange"], {label: "Line type:", value:"arearange"})
Insert cell
areaRange= {
const figure = html`<div style="height: 400px; width:${Math.min(width, maxWidth)+'px'};">`;
Highcharts.chart(figure, {
credits: false, // removes watermark,
chart: {
type: areaRangeLine,
zoomType: 'x',
scrollablePlotArea: {
minWidth: 600,
scrollPositionX: 1
}
},

title: {
text: 'Temperature variation by day'
},

xAxis: {
type: 'datetime',
accessibility: {
rangeDescription: 'Range: Jan 1st 2017 to Dec 31 2017.'
}
},

yAxis: {
title: {
text: null
}
},

tooltip: {
crosshairs: true,
shared: false,
valueSuffix: '°C',
xDateFormat: '%A, %b %e'
},

legend: {
enabled: false
},

series: [{
name: 'Temperatures',
data: data_areaRange
}]

});
return figure;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more