I´m noob using d3js I´m trying add a new vertical on Basic Horizontal Bar Chart of d3js. To test I´m using codepen but the vertical line is not shown.
HTML file
<head>
<!-- Plotly.js -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="myDiv" style="width: 480px; height: 400px;"><!-- Plotly chart will be drawn inside this DIV --></div>
<script>
<!-- JAVASCRIPT CODE GOES HERE -->
</script>
</body>
JS file
var trace1 = {
x: [20],
y: ['giraffes'],
name: 'SF Zoo',
orientation: 'h',
marker: {
color: 'rgba(55,128,191,0.6)',
width: 1
},
type: 'bar'
};
var data = [trace1];
var layout = {
title: 'Colored Bar Chart',
barmode: 'stack'
};
Plotly.newPlot('myDiv', data, layout);
var y = d3.scale.linear().range([0, 400]);
var svg = d3.select(document.getElementById('myDiv'));
svg.append("line")
.attr("x1", 10)
.attr("y1", y(0))
.attr("x2", 10)
.attr("y2", y(1))
.style("stroke", "black");
Your svg variable is a HTML <div> element:
<div id="myDiv"></div>
//"myDiv" is the ID of the div
var svg = d3.select(document.getElementById('myDiv'));
//you're getting the div here -----------------^
However, you cannot append a SVG line to a HTML div.
Solution: select the svg itself:
var svg = d3.select("svg");
Now your svg variable points to the SVG, in which you can append a SVG line.
Here is your code with that change:
var trace1 = {
x: [20],
y: ['giraffes'],
name: 'SF Zoo',
orientation: 'h',
marker: {
color: 'rgba(55,128,191,0.6)',
width: 1
},
type: 'bar'
};
var data = [trace1];
var layout = {
title: 'Colored Bar Chart',
barmode: 'stack'
};
Plotly.newPlot('myDiv', data, layout);
var y = d3.scale.linear().range([0, 400]);
var svg = d3.select("svg");
svg.append("line")
.attr("x1", 10)
.attr("y1", y(0))
.attr("x2", 10)
.attr("y2", y(1))
.style("stroke", "black");
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<body>
<div id="myDiv" style="width: 480px; height: 400px;"><!-- Plotly chart will be drawn inside this DIV --></div>
PS: You don't need getElementById in a D3 code. To select that div, for instance, just do d3.select("#myDiv").
Related
TLDR: I have an NVD3 graph that shows tick lines all across the axis, but I would like to change it so it only displays on the axis lines if possible.
Here is a live example:
var app = angular.module('plunker', ['nvd3']);
app.controller('MainCtrl', function($scope) {
$scope.options = {
chart: {
type: 'lineChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 80,
left: 55
},
x: function(d){ return d.x; },
y: function(d){ return d.y; },
useInteractiveGuideline: true,
xAxis: {
axisLabel: 'Timeline',
tickFormat: function(d) {
return d3.time.format('%B %d')(new Date(d))
},
ticks: 6,
showMaxMin: false
},
yAxis: {
axisLabel: 'Molecular density (kg/m^3)',
tickFormat: function(d){
return d3.format('.02f')(d);
},
axisLabelDistance: -10,
showMaxMin: false
}
}
};
$scope.data = [{"key":"K7 molecules","values":[{"x":1435708800000,"y":8},{"x":1435795200000,"y":9},{"x":1435881600000,"y":8},{"x":1435968000000,"y":8},{"x":1436054400000,"y":9},{"x":1436140800000,"y":9},{"x":1436227200000,"y":8},{"x":1436313600000,"y":8},{"x":1436400000000,"y":9},{"x":1436486400000,"y":9},{"x":1436572800000,"y":7},{"x":1436659200000,"y":8}],"area":true,"color":"#0CB3EE"},{"key":"N41 type C molecules","values":[{"x":1435708800000,"y":8},{"x":1435795200000,"y":7},{"x":1435881600000,"y":8},{"x":1435968000000,"y":9},{"x":1436054400000,"y":7},{"x":1436140800000,"y":9},{"x":1436227200000,"y":8},{"x":1436313600000,"y":9},{"x":1436400000000,"y":9},{"x":1436486400000,"y":9},{"x":1436572800000,"y":9},{"x":1436659200000,"y":8}],"area":true,"color":"#383838"}];
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>Angular-nvD3 Line Chart</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.css"/>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.min.js"></script>
<script src="https://rawgit.com/krispo/angular-nvd3/v1.0.1/dist/angular-nvd3.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<nvd3 options="options" data="data" class="with-3d-shadow with-transitions"></nvd3>
</body>
</html>
Is there any way I could make the tick lines appear just on the axes line only? To make it clear, this is what it looks like:
I used a different library to generate the following plot, and I would like the tick lines to appear just on the axis lines like this example instead:
It appears that there is no real way to do this with NVD3 as it does not provide a way to show tick marks on the axis. However, we could add our own tick marks by fetching the chart SVG and then modifying it.
I've attached an example that adds tick marks to X-Axis, and it is basically slightly modified based on this jsfiddle here: http://jsfiddle.net/3r88bgjw
var data;
data = [{
values: [],
}, ];
var i, x;
var prevVal = 3000;
var tickCount = 2000;
for (i = 0; i < tickCount; i++) {
x = 1425096000 + i * 10 * 60; // data points every ten minutes
if (Math.random() < 0.8) { // add some gaps
prevVal += (Math.random() - 0.5) * 500;
if (prevVal <= 0) {
prevVal = Math.random() * 100;
}
data[0].values.push({
x: x * 1000,
y: prevVal
});
}
}
var chart;
nv.addGraph(function() {
chart = nv.models.historicalBarChart();
chart.xScale(d3.time.scale()) // use a time scale instead of plain numbers in order to get nice round default values in the axis
.color(['#68c'])
.useInteractiveGuideline(true) // check out the css that turns the guideline into this nice thing
.margin({
"left": 80,
"right": 50,
"top": 20,
"bottom": 30,
})
.noData("There is no data to display.")
.duration(0);
var tickMultiFormat = d3.time.format.multi([
["%-I:%M%p", function(d) {
return d.getMinutes();
}], // not the beginning of the hour
["%-I%p", function(d) {
return d.getHours();
}], // not midnight
["%b %-d", function(d) {
return d.getDate() != 1;
}], // not the first of the month
["%b %-d", function(d) {
return d.getMonth();
}], // not Jan 1st
["%Y", function() {
return true;
}]
]);
chart.xAxis
.showMaxMin(false)
.tickPadding(10)
.tickFormat(function(d) {
return tickMultiFormat(new Date(d));
});
chart.yAxis
.tickFormat(d3.format(",.0f"));
var svgElem = d3.select('#chart svg');
svgElem
.datum(data)
.transition()
.call(chart);
// make our own x-axis tick marks because NVD3 doesn't provide any
var tickY2 = chart.yAxis.scale().range()[1];
var lineElems = svgElem
.select('.nv-x.nv-axis.nvd3-svg')
.select('.nvd3.nv-wrap.nv-axis')
.select('g')
.selectAll('.tick')
.data(chart.xScale().ticks())
.append('line')
.attr('class', 'x-axis-tick-mark')
.attr('x2', 0)
.attr('y1', tickY2 + 7)
.attr('y2', tickY2)
.attr('stroke-width', 3);
// set up the tooltip to display full dates
var tsFormat = d3.time.format('%b %-d, %Y %I:%M%p');
var contentGenerator = chart.interactiveLayer.tooltip.contentGenerator();
var tooltip = chart.interactiveLayer.tooltip;
tooltip.contentGenerator(function(d) {
d.value = d.series[0].data.x;
return contentGenerator(d);
});
tooltip.headerFormatter(function(d) {
return tsFormat(new Date(d));
});
return chart;
});
<div>Try resizing the panel to see the various types of time labels.</div>
<br>
<div id="chart">
<svg></svg>
</div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.4/nv.d3.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.4/nv.d3.min.js"></script>
The first time I load data, the graph draws correctly, but when I load a different data set, the graph remains unchanged.
I switch between datasets using buttons. The first click always draws the graph correctly, no matter what button I click. But I can't update the graph after it is drawn by clicking on the other button. Any help is very much appreciated,thank you!
const dataA = [
{ population: 50, size: 100 },
{ population: 100, size: 100 },
];
const dataB = [
{ money: 4, currency: "usd" },
{ money: 10, currency: "eur" },
];
function drawChart(dataSet, prop) {
let width = 900;
let height = 200;
let x = d3.scale.ordinal().rangeRoundBands([0, width], 0.9);
let y = d3.scale
.linear()
.domain([dataSet[0][prop] - 39, dataSet[dataSet.length - 1][prop]])
.range([height, 0]);
let chart = d3.select("#chart").attr("width", width).attr("height", height);
let barWidth = width / dataSet.length;
let div = d3.select("body").append("div").attr("class", "tooltip");
let bar = chart
.selectAll("g")
.data(dataSet)
.enter()
.append("g")
.attr("transform", function (d, i) {
return "translate(" + i * barWidth + ",0)";
});
bar
.append("rect")
.attr("y", function (d) {
return y(d[prop]);
})
.attr("height", function (d) {
return height - y(d[prop]);
})
.attr("width", barWidth);
}
function drawDataA() {
drawChart(dataA, "population");
}
function drawDataB() {
drawChart(dataB, "money");
}
d3.select("#dataA").on("click", drawDataA);
d3.select("#dataB").on("click", drawDataB);
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
<script src="https://d3js.org/d3.v4.js"></script>
</head>
<body>
<div id="app">
<svg class="chart" id="chart"></svg>
<button id="dataA">data1</button>
<button id="dataB">data2</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="./index.js"></script>
</body>
</html>
CodePen: https://codepen.io/rfripp2/pen/porpaLL
This is the expected behavior. Let's look at your code:
let bar = chart
.selectAll("g")
.data(dataSet)
.enter()
.append("g")
The select all statement selects all existing elements matching the selector that are children of elements in the selection chart.
The data method binds a new data array to this selection.
The enter method returns a new selection, containing a placeholder for every item in the data array which does not have a corresponding element in the selection.
The append method returns a newly appended child element for every element in the selection it is called on.
Running the code
The first time you call the draw function you have no g elements, so the selection is empty. You bind data to this empty selection. You then use the enter selection. Because there are two data items and no elements in the selection, enter contains two placeholders/elements. You then use append to add those elements.
The second time you call the draw function you have two g elements, so the selection has two elements in it. You bind data to this selection. You then use the enter selection. Because you already have two elements and you only have two data points, the enter selection is empty. As a consequence, append does not create any new elements.
You can see this by using selection.size():
const dataA = [
{ population: 50, size: 100 },
{ population: 100, size: 100 },
];
const dataB = [
{ money: 4, currency: "usd" },
{ money: 10, currency: "eur" },
];
function drawChart(dataSet, prop) {
let width = 900;
let height = 200;
let x = d3.scale.ordinal().rangeRoundBands([0, width], 0.9);
let y = d3.scale
.linear()
.domain([dataSet[0][prop] - 39, dataSet[dataSet.length - 1][prop]])
.range([height, 0]);
let chart = d3.select("#chart").attr("width", width).attr("height", height);
let barWidth = width / dataSet.length;
let div = d3.select("body").append("div").attr("class", "tooltip");
let bar = chart
.selectAll("g")
.data(dataSet)
.enter()
.append("g")
.attr("transform", function (d, i) {
return "translate(" + i * barWidth + ",0)";
});
console.log("The enter selection contains: " + bar.size() + "elements")
bar
.append("rect")
.attr("y", function (d) {
return y(d[prop]);
})
.attr("height", function (d) {
return height - y(d[prop]);
})
.attr("width", barWidth);
}
function drawDataA() {
drawChart(dataA, "population");
}
function drawDataB() {
drawChart(dataB, "money");
}
d3.select("#dataA").on("click", drawDataA);
d3.select("#dataB").on("click", drawDataB);
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
<script src="https://d3js.org/d3.v4.js"></script>
</head>
<body>
<div id="app">
<svg class="chart" id="chart"></svg>
<button id="dataA">data1</button>
<button id="dataB">data2</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="./index.js"></script>
</body>
</html>
Solution
We want to use both the update and the enter selection (if the dataset ever changes in size, we'd likely want the exit selection too). We can use .join() to simplify this, join removes elements in the exit selection (surplus elements which don't have a corresponding data item), and returns the merged enter selection (new elements for surplus data items) and update selection (preexisting elements).
The nesting of your elements into a parent g and child rect is unnecessary here - and requires additional modifications. By positioning the bars directly we avoid the need for the parent g:
const dataA = [
{ population: 50, size: 100 },
{ population: 100, size: 100 },
];
const dataB = [
{ money: 4, currency: "usd" },
{ money: 10, currency: "eur" },
];
function drawChart(dataSet, prop) {
let width = 400;
let height = 200;
let x = d3.scaleBand().range([0, width], 0.9);
let y = d3.scaleLinear()
.domain([dataSet[0][prop] - 39, dataSet[dataSet.length - 1][prop]])
.range([height, 0]);
let chart = d3.select("#chart").attr("width", width).attr("height", height);
let barWidth = width / dataSet.length;
let div = d3.select("body").append("div").attr("class", "tooltip");
let bar = chart
.selectAll("rect")
.data(dataSet)
.join("rect")
.attr("transform", function (d, i) {
return "translate(" + i * barWidth + ",0)";
}).attr("y", function (d) {
return y(d[prop]);
})
.attr("height", function (d) {
return height - y(d[prop]);
})
.attr("width", barWidth);
}
function drawDataA() {
drawChart(dataA, "population");
}
function drawDataB() {
drawChart(dataB, "money");
}
d3.select("#dataA").on("click", drawDataA);
d3.select("#dataB").on("click", drawDataB);
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
</head>
<body>
<div id="app">
<svg class="chart" id="chart"></svg>
<br />
<button id="dataA">data1</button>
<button id="dataB">data2</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.0.0/d3.min.js"></script>
<script src="./index.js"></script>
</body>
</html>
This requires updating your version of D3 from v3 (you are actually using two versions of D3, v3 and v4, both fairly outdated, and both with different method names, actually with different ways of handling the enter selection).
If you wish to use d3v4, then the join method is not available, but we can merge enter and update:
When I say update selection, I'm refering to the initial selection:
// update selection:
let bar = chart
.selectAll("rect")
.data(dataSet);
// enter selection:
bar.enter().append("rect")
const dataA = [
{ population: 50, size: 100 },
{ population: 100, size: 100 },
];
const dataB = [
{ money: 4, currency: "usd" },
{ money: 10, currency: "eur" },
];
function drawChart(dataSet, prop) {
let width = 400;
let height = 200;
let x = d3.scaleBand().range([0, width], 0.9);
let y = d3.scaleLinear()
.domain([dataSet[0][prop] - 39, dataSet[dataSet.length - 1][prop]])
.range([height, 0]);
let chart = d3.select("#chart").attr("width", width).attr("height", height);
let barWidth = width / dataSet.length;
let div = d3.select("body").append("div").attr("class", "tooltip");
let bar = chart
.selectAll("rect")
.data(dataSet);
bar.enter().append("rect")
.merge(bar)
.attr("transform", function (d, i) {
return "translate(" + i * barWidth + ",0)";
}).attr("y", function (d) {
return y(d[prop]);
})
.attr("height", function (d) {
return height - y(d[prop]);
})
.attr("width", barWidth);
}
function drawDataA() {
drawChart(dataA, "population");
}
function drawDataB() {
drawChart(dataB, "money");
}
d3.select("#dataA").on("click", drawDataA);
d3.select("#dataB").on("click", drawDataB);
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
</head>
<body>
<div id="app">
<svg class="chart" id="chart"></svg>
<br />
<button id="dataA">data1</button>
<button id="dataB">data2</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.1.0/d3.min.js"></script>
<script src="./index.js"></script>
</body>
</html>
And lastly, if you wish to keep d3v3 (we are on v7 already), we can rely on an implicit merging of update and enter on enter (modifying the update selection). This "magic" was removed in v4, partly because it was not explicit. To do so we need to break your method chaining so that bar contains the
const dataA = [
{ population: 50, size: 100 },
{ population: 100, size: 100 },
];
const dataB = [
{ money: 4, currency: "usd" },
{ money: 10, currency: "eur" },
];
function drawChart(dataSet, prop) {
let width = 400;
let height = 200;
let x = d3.scale.ordinal().rangeRoundBands([0, width], 0.9);
let y = d3.scale.linear()
.domain([dataSet[0][prop] - 39, dataSet[dataSet.length - 1][prop]])
.range([height, 0]);
let chart = d3.select("#chart").attr("width", width).attr("height", height);
let barWidth = width / dataSet.length;
let div = d3.select("body").append("div").attr("class", "tooltip");
// update selection:
let bar = chart
.selectAll("rect")
.data(dataSet);
// enter selection:
bar.enter().append("rect")
bar.attr("transform", function (d, i) {
return "translate(" + i * barWidth + ",0)";
}).attr("y", function (d) {
return y(d[prop]);
})
.attr("height", function (d) {
return height - y(d[prop]);
})
.attr("width", barWidth);
}
function drawDataA() {
drawChart(dataA, "population");
}
function drawDataB() {
drawChart(dataB, "money");
}
d3.select("#dataA").on("click", drawDataA);
d3.select("#dataB").on("click", drawDataB);
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
</head>
<body>
<div id="app">
<svg class="chart" id="chart"></svg>
<br />
<button id="dataA">data1</button>
<button id="dataB">data2</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script src="./index.js"></script>
</body>
</html>
Note: d3v4 made changes to method names that break code from v3. This required changes to d3.scale.linear / d3.scale.ordinal in the snippets using v4 and 7 (using merge and join respectively).
I've downloaded this visualization: https://vizhub.com/Mithunprom/6f378ad23e3e4a2f99949368f02e3290
If you access the link, you will able to see all source files including their codes.
I am not able to run it through index.html in my browser, nothing shows up inside browser.. I just simply want to get it working.
This is index.html:
<!DOCTYPE html>
<html>
<head>
<title>Making a Bar Chart</title>
<link rel="stylesheet" href="styles.css">
<script src="https://unpkg.com/d3#5.6.0/dist/d3.min.js"></script>
</head>
<body>
<svg width="960" height="500"></svg>
<script src="bundle.js"></script>
</body>
</html>
You need to add the script part and css code from the specified URL. I have also made the required changes needed to accomplish task.
Please find the working example below:
const data = [{
country: 'China',
population: 1415046
},
{
country: 'India',
population: 1354052
},
{
country: 'United States',
population: 326767
},
{
country: 'Indonesia',
population: 266795
},
{
country: 'Brazil',
population: 210868
},
{
country: 'Pakistan',
population: 200814
},
{
country: 'Nigeria',
population: 195875
},
{
country: 'Bangladesh',
population: 166368
},
{
country: 'Russia',
population: 143965
},
{
country: 'Mexico',
population: 130759
},
];
data.forEach(d => {
d.population = +d.population * 1000;
});
const titleText = 'Top 10 Most Populous Countries';
const xAxisLabelText = 'Population';
const svg = d3.select('svg');
const width = +svg.attr('width');
const height = +svg.attr('height');
const render = data => {
const xValue = d => d['population'];
const yValue = d => d.country;
const margin = {
top: 50,
right: 40,
bottom: 77,
left: 180
};
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const xScale = d3.scaleLinear()
.domain([0, d3.max(data, xValue)])
.range([0, innerWidth]);
const yScale = d3.scaleBand()
.domain(data.map(yValue))
.range([0, innerHeight])
.padding(0.1);
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
const xAxisTickFormat = number =>
d3.format('.3s')(number)
.replace('G', 'B');
const xAxis = d3.axisBottom(xScale)
.tickFormat(xAxisTickFormat)
.tickSize(-innerHeight);
g.append('g')
.call(d3.axisLeft(yScale))
.selectAll('.domain, .tick line')
.remove();
const xAxisG = g.append('g').call(xAxis)
.attr('transform', `translate(0,${innerHeight})`);
xAxisG.select('.domain').remove();
xAxisG.append('text')
.attr('class', 'axis-label')
.attr('y', 65)
.attr('x', innerWidth / 2)
.attr('fill', 'black')
.text(xAxisLabelText);
g.selectAll('rect').data(data)
.enter().append('rect')
.attr('y', d => yScale(yValue(d)))
.attr('width', d => xScale(xValue(d)))
.attr('height', yScale.bandwidth());
g.append('text')
.attr('class', 'title')
.attr('y', -10)
.text(titleText);
};
render(data);
body {
margin: 0px;
overflow: hidden;
}
rect {
fill: steelblue;
}
<!DOCTYPE html>
<html>
<head>
<title>Making a Bar Chart</title>
<link rel="stylesheet" href="styles.css">
<script src="https://unpkg.com/d3#5.6.0/dist/d3.min.js"></script>
</head>
<body>
<svg width="960" height="500"></svg>
<script src="bundle.js"></script>
</body>
</html>
I am trying to add D3 histogram to a VueJS component and I can't get the axes to appear correctly. The only thing I see rendered is the tip of the y-axis.
here is my js for the chart component:
export default {
name: "Histogram",
props: {
},
mounted() {
this.generateBars();
},
data: () => ({
title: 'Bar Chart'
}),
methods: {
generateBars() {
const sample = [
{
language: 'Rust',
value: 78.9,
color: '#000000'
},
...
{
language: 'Clojure',
value: 59.6,
color: '#507dca'
}
];
const margin = 60
const width = 1000 - 2 * margin;
const height = 600 - 2 * margin;
const svg = d3.select("svg");
const chart = svg.append('g')
.attr('transform', `translate(${margin}, ${margin})`);
const yScale = d3.scaleLinear()
.range([height, 0])
.domain([0, 100]);
chart.append('g')
.call(d3.axisLeft(yScale));
const xScale = d3.scaleBand()
.range([0, width])
.domain(this.sample.map((s) => s.language))
.padding(0.2)
chart.append('g')
.attr('transform', `translate(0, ${height})`)
.call(d3.axisBottom(xScale));
}
}
};
My template for the component is simply the chart itself:
<template>
<div id="container" class="svg-container" align="center">
<h3>{{ title }}</h3>
<svg />
</div>
</template>
Here is what I see:
After further examination, I think the footer is covering the bottom of the chart. Not sure why it does not move down to accommodate the additional content.
Looks like you forgot to set width and height for svg (via html or js).
<svg width="1000" height="600"></svg>
And remove "this" in line:
.domain(this.sample.map((s) => s.language))
I'm trying to teach myself how to make interactive visualizations in d3.js, currently working through Elijah Meeks' D3.js In Action. I'm trying to make his pie chart example interactive using three buttons. I'm doing something wrong with my tweening - I'm trying to save the currently displayed pie so that the transition goes between it and the newly chosen pie. However, my current pie keeps resetting to the initial pie. I think it's probably something simple, but I just can't figure out what I'm doing wrong.
Can someone tell me what to change to make my transitions work? To demonstrate the problem:
Run the code below,
Click on the 'Stat 2' button,
Click on the 'Stat 2' button again - you will see the pie resets to 'Stat 1', then smoothly transition to 'Stat 2'.
.as-console-wrapper { max-height: 20% !important;}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<div id="viz">
<button id="0"> Stat 1 </button>
<button id="1"> Stat 2 </button>
<button id="2"> Stat 3 </button>
<br>
<svg style="width:400px;height:300px;border:1px lightgray solid;" />
</div>
</body>
<script>
var obj = [{
name: "a",
stat1: 10,
stat2: 20,
stat3: 30,
}, {
name: "b",
stat1: 30,
stat2: 20,
stat3: 10,
}, {
name: "c",
stat1: 15,
stat2: 25,
stat3: 50,
}];
function piechart(data) {
var currentPie = 0; //Initialize to stat1
var fillScale = d3.scaleOrdinal(d3.schemeCategory10);
var pieChart = d3.pie().sort(null);
var newArc = d3.arc().innerRadius(50).outerRadius(100);
// Create each pie chart
pieChart.value(d => d.stat1);
var stat1Pie = pieChart(data);
pieChart.value(d => d.stat2);
var stat2Pie = pieChart(data);
pieChart.value(d => d.stat3);
var stat3Pie = pieChart(data);
// Embed slices on each name
data.forEach((d, i) => {
var slices = [stat1Pie[i], stat2Pie[i], stat3Pie[i]];
d.slices = slices;
});
d3.select("svg")
.append("g")
.attr("transform", "translate(200, 150)")
.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", d => newArc(d.slices[currentPie]))
.attr("fill", (d, i) => fillScale(i))
.attr("stroke", "black")
.attr("stroke-width", "2px");
function transPie(d) {
var newPie = this.id;
console.log("Transition from pie " + currentPie + " to pie " + newPie);
d3.selectAll("path")
.transition()
.delay(500)
.duration(1500)
.attrTween("d", tweenPies)
function tweenPies(d, i) {
console.log(i + ":start tween function \n current pie = " + currentPie + "\n new pie = " + newPie);
var currentAngleStart = d.slices[currentPie].startAngle;
var newAngleStart = d.slices[newPie].startAngle;
var currentAngleEnd = d.slices[currentPie].endAngle;
var newAngleEnd = d.slices[newPie].endAngle;
return t => {
var interpolateStartAngle = d3.interpolate(currentAngleStart, newAngleStart);
var interpolateEndAngle = d3.interpolate(currentAngleEnd, newAngleEnd);
d.startAngle = interpolateStartAngle(t);
d.endAngle = interpolateEndAngle(t);
return newArc(d);
};
};
};
d3.selectAll("button").on("click", transPie);
};
piechart(obj);
</script>
</html>
You never set the state of currentPie to the new state after a selection. I've added a .on('end', handler to the transition to set this state:
.on('end', function(){
currentPie = newPie;
});
Running code:
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<div id="viz">
<button id="0"> Stat 1 </button>
<button id="1"> Stat 2 </button>
<button id="2"> Stat 3 </button>
<br />
<svg style="width:1000px;height:500px;border:1px lightgray solid;"></svg>
</div>
<script>
var obj = [{name: "a",stat1: 10,stat2: 20,stat3: 30,},
{name: "b",stat1: 30,stat2: 20,stat3: 10,},
{name: "c",stat1: 15,stat2: 25,stat3: 50,}];
function piechart(data){
var currentPie = 0; //Initialize to stat1
var fillScale = d3.scaleOrdinal(d3.schemeCategory10);
var pieChart = d3.pie().sort(null);
var newArc = d3.arc().innerRadius(50).outerRadius(100);
// Create each pie chart
pieChart.value(d => d.stat1);
var stat1Pie = pieChart(data);
pieChart.value(d => d.stat2);
var stat2Pie = pieChart(data);
pieChart.value(d => d.stat3);
var stat3Pie = pieChart(data);
// Embed slices on each name
data.forEach( (d,i) => {
var slices = [stat1Pie[i], stat2Pie[i], stat3Pie[i]];
d.slices = slices;
});
d3.select("svg")
.append("g")
.attr("transform", "translate(250, 250)")
.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", d => newArc(d.slices[currentPie]))
.attr("fill", (d,i) => fillScale(i))
.attr("stroke", "black")
.attr("stroke-width", "2px");
function transPie(d) {
var newPie = +this.id;
console.log("Transition from pie " +currentPie+ " to pie " + newPie);
d3.selectAll("path")
.transition()
.delay(500)
.duration(1500)
.attrTween("d", tweenPies)
.on('end', function(){
currentPie = newPie;
})
function tweenPies(d, i) {
console.log(i + ":start tween function \n current pie = " + currentPie + "\n new pie = "+newPie);
var currentAngleStart = d.slices[currentPie].startAngle;
var newAngleStart = d.slices[newPie].startAngle;
var currentAngleEnd = d.slices[currentPie].endAngle;
var newAngleEnd = d.slices[newPie].endAngle;
return t => {
var interpolateStartAngle = d3.interpolate(currentAngleStart, newAngleStart);
var interpolateEndAngle = d3.interpolate(currentAngleEnd, newAngleEnd);
d.startAngle = interpolateStartAngle(t);
d.endAngle = interpolateEndAngle(t);
return newArc(d);
};
};
};
d3.selectAll("button").on("click", transPie);
};
piechart(obj);
</script>
</body>
</html>