After last week’s Angular+Svg+Html blog entry, here’s another version of the same US States incarceration rates data. Bar-charts in SVG this time.

Here it is running in this page:

Order by: Alphabetically Incarceration Rate
735 511 567 430 467 467 407 463 557 540 332 474 351 442 291 303 492 1619 151 403 218 488 179 634 509 368 247 486 220 298 316 307 368 225 449 661 371 240 519 412 436 639 232 260 489 272 331 393 374 387 Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada New Hampshire New Jersey New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregan Rhode Island South Carolina South Dakota Tenassee Texas Utah Vermont Virginia Washington West Virginia Wisconsin Wisconsin Wyoming


Again, it’s interactive. You can sort alphabetically, or by rate. It’s the SVG rectangles that Angular is mutating, and the DOM (in modern browsers) just makes that view correctly.

You really want to view the source, because as always, I’ve done a low-JavaScript usage of Angular. I greatly prefer that style and recommend it.

Snafus

Two ng-repeat loops

I have to do two ng-repeat loops - one for the rectangles, and one for the text written (at 90 degrees) in the rectangles:


If you’re new to Angular, note how little SVG is being used for a multi-bar chart :)

I had to change the JSON data :-(

The nature of the data, has to change from the last blog entry, so that ordering can be supported. It is an array now:

$scope.states = [
	{ name: "Louisiana", "inc" : 1619},
	{ name: "Alabama", "inc" : 735}
];

And in the previous blog entry, it was a dictionary:

$scope.states = {
	"la": {"inc" : 1619},
	"al": {"inc" : 735}
};

This is because of this issue in Angular. It is marked as “won’t fix” which I disagree with for the record. At least the workaround offered, I believe should be in the framework by default.

May 14, 2016

Updated for Angular 1.5.5



Published

February 26th, 2014
Reads: 4,336

Syndicated by DZone.com
Reads:

Tags

Comments formerly in Disqus, but exported and mounted statically ...


Thu, 27 Feb 2014Michael George

Great post ... but as a resident of Louisiana, this data makes me extremely sad and frustrated.

Wed, 01 Jul 2015Matt Hughes

The console is full of errors! How would you do this without raising any exceptions?