Skip to content

Instantly share code, notes, and snippets.

@jamesleesaunders
Last active February 20, 2023 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesleesaunders/36ccc5e130948c098209 to your computer and use it in GitHub Desktop.
Save jamesleesaunders/36ccc5e130948c098209 to your computer and use it in GitHub Desktop.
d3-ez : Polar Area Chart Example

d3-ez : Polar Area Chart Example

Generated using d3-ez D3 Reusable Chart Library

The Polar Area chart is similar to a usual pie chart, except sectors are equal angles and differ rather in how far each sector extends from the center of the circle. The polar area diagram is used to plot cyclic phenomena (e.g., count of deaths by month).

FUNCTION: Comparison; Trend over time

Credit: Data Viz Project

Credit: Peter Cook for giving permission to use code from his Radial Bar Chart

<!DOCTYPE html>
<html>
<head>
<title>d3-ez : Polar Area Chart Example</title>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://raw.githack.com/jamesleesaunders/d3-ez/master/dist/d3-ez.js"></script>
<link rel="stylesheet" type="text/css" href="http://rawgit.com/jamesleesaunders/d3.ez/master/dist/d3-ez.css" />
</head>
<body>
<div id="chartholder"></div>
<br/>
<div>Value: <span id="message"></span></div>
<script type="text/javascript">
// Generate some data
function randomDataset() {
var _randomNum = function() {
return Math.floor(Math.random() * 10);
};
var data = {
"key": "Languages",
"values": [{
"key": "JavaScript",
"value": _randomNum()
}, {
"key": "C++",
"value": _randomNum()
}, {
"key": "Java",
"value": _randomNum()
}, {
"key": "Ruby",
"value": _randomNum()
}, {
"key": "Python",
"value": _randomNum()
}, {
"key": "PHP",
"value": _randomNum()
}, {
"key": "Perl",
"value": _randomNum()
}, {
"key": "Basic",
"value": _randomNum()
}, {
"key": "Assembly",
"value": _randomNum()
}]
};
return data;
}
var chart = d3.ez.chart.polarAreaChart().colors(d3.ez.palette.diverging(2));
var legend = d3.ez.component.legend().title('Top 10');
var title = d3.ez.component.title().mainText("Programming Languages").subText("Top 10 Languages Used By Developers");
// Create chart base
var myChart = d3.ez.base()
.width(750)
.height(400)
.chart(chart)
.legend(legend)
.title(title)
.on("customValueMouseOver", function(d, i) {
d3.select("#message").text(d.value);
});
// Add to page
function update() {
var data = randomDataset();
d3.select("#chartholder")
.datum(data)
.call(myChart);
}
update();
setInterval(update, 2000);
</script>
</body>
</html>
@Ahsan9981
Copy link

How to use this implementation in Angular ?

Copy link

ghost commented Feb 20, 2023

Hello,

I tried this example in latest version v7 of d3.js. But using this version it shows an error.
errorFile

Could you please confirm does the Polar plot works with the latest version v7 of d3.js. If yes could you please share the sample code of the same because this sample code is only works for version v5 of d3.js and for above versions it shows an error.

  • Below is the code -
    `
<title>d3-ez : Polar Area Chart Example</title> <script src="https://d3js.org/d3.v7.min.js"></script> <script src="https://raw.githack.com/jamesleesaunders/d3-ez/master/dist/d3-ez.js"></script>
<script type="text/javascript"> var data = { "key": "Languages", "values": [{ "key": "JavaScript", "value": 50 }, { "key": "C++", "value": 10 }, { "key": "Java", "value": 30 }, { "key": "Ruby", "value": 70 }, { "key": "Python", "value": 80 }, { "key": "PHP", "value": 20 }, { "key": "Perl", "value": 56 }, { "key": "Basic", "value": 90 }, { "key": "Assembly", "value": 100 }] }; var chart = d3.ez.chart.polarAreaChart().colors(d3.ez.palette.diverging(2)); // Create chart base var myChart = d3.ez.base() .width(750) .height(400) .chart(chart) .on("customValueMouseOver", function (d, i) { d3.select("#message").text(d.value); }); // Add to page function update() { d3.select("#chartholder") .datum(data) .call(myChart); } update(); </script> `

@jamesleesaunders
Copy link
Author

Hi @swati-kadam28 thanks for your interest. I have not had a chance to refactor d3-ez to support D3v7 yet (I have mainly been focussing on its sister project d3-x3d a 3D charting library.
If I get a chance I will see how quickly I can update d3-ez to support it and get back to you.
Alternatively I would welcome any contributions to the project by way of Pull Request.

Copy link

ghost commented Feb 20, 2023

Hi @jamesleesaunders thanks for sharing the information.
It was very helpful. Please let me know whenever you get chance to add support for d3-ez.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment