Wednesday, July 13, 2016

A Guide to Web Font Loading Strategies, A CSS Selector Game, HTML5test Updated

Plus a 30 minute guide to building instant-loading offline-first webapps...
Read this e-mail on the Web

FrontEnd Focus

formerly HTML5 Weekly

YouTube
In just 30 minutes, Jake Archibald presents a practical guide to the technologies behind taking an online-only site and turning it into a fully network-resilient offline-first progressive webapp.


Zach Leatherman
An objective set of pros, cons and helpful tips spanning the various ways of loading Web fonts on your site.


Eugene Chiong
A Chrome DevTools extension that lets you simulate interactions with a site as a completely, partially or color-blind person, as well as run an accessibility audit. More insights here.


ForwardJS  Sponsored
Eight days of sessions and workshops on React, D3, Angular 2, Accessibility, Progressive Web Apps and Functional JS.

ForwardJS

Heather Migliorisi
A code and example-dense look at how the rising popularity of Scalable Vector Graphics (SVG) could impact users of assistive technology, and what can be done to ensure a great experience.


Luke Pacholski
A quick online game/tutorial to help you learn CSS selectors (or at least refresh your knowledge).


Tommy Hodgins
A discussion of the history and uses of element queries and how this led to the creation of EQCSS, a plugin that supports element queries, Scoped CSS, and more.


Patrick Catanzariti
Patrick Catanzariti looks at how the Internet of Things may affect front-end web development in the future.


W3C
The level one draft for the CSS Grid Layout spec has been updated, outlining the differences between Flexbox and Grid: “Unlike Flexible Box Layout, which is single-axis–oriented, Grid Layout is optimized for 2-dimensional layouts”


Jobs

In brief

Curated by Peter Cooper and published by Cooper Press.

Unsubscribe : Change email address : Read this issue on the Web

Published by Cooper Press Ltd. Fairfield Enterprise Centre, Louth, LN11 0LS, UK


by via HTML5 Weekly

Learn to Create D3.js Data Visualizations by Example

D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS.

There are only three JavaScript libraries that I would suggest every web developer should learn: jQuery, Underscore and D3. These are libraries that allow you to think about code in new ways: jQuery allows you to write less and do more with the DOM, Underscore (or lodash) gives you functional tools for changing the way you write programs and D3 gives you a rich tool-set for data manipulation and graphics programming. If you're unfamiliar with D3 please take a moment to look at the impressive gallery of examples to see what can be done with it.

This aint' your daddy's charting library.

William Playfair invented the bar, line and area charts in 1786 and the pie chart in 1801. Today, these are still the primary ways that most data sets are presented. Now, these charts are excellent but D3 gives you the tools and the flexibility to make unique data visualizations for the web, your creativity is the only limiting factor.

D3 is an extremely flexible low-level visualization library with a jQuery-like API for mapping data to HTML and SVG documents. It contains a wealth of helpful mathematical functions for data transformation and physics calculations, although most of it's power comes from manipulating geometries and paths in SVG.

This article aims to give you a high level overview of D3's capabilities, in each example you'll be able to see the input data, transformation and the output document. Rather than explaining what every function does I'll show you the code and you should be able to get a rough understanding of how things work. I'll only dig into details for the most important concepts, Scales and Selections.

A Bar Chart

A basic bar chart

See the codepen

I promised you more than William Playfair's charts but making the humble bar chart with HTML is one of the easiest ways to understand how D3 transforms data into a document. Here's what that looks like:

d3.select('#chart')
  .selectAll("div")
  .data([4, 8, 15, 16, 23, 42])
  .enter()
  .append("div")
  .style("height", (d)=> d + "px")

The selectAll function returns a D3 "selection": an array of elements that get created when we enter and append a div for each data point.

This code maps the input data [4, 8, 15, 16, 23, 42] to this output HTML.

<div id="chart">
  <div style="height: 4px;"></div>
  <div style="height: 8px;"></div>
  <div style="height: 15px;"></div>
  <div style="height: 16px;"></div>
  <div style="height: 23px;"></div>
  <div style="height: 42px;"></div>
</div>

All of the style properties that don't change can go in the CSS.

#chart div {
  display: inline-block;
  background: #4285F4;
  width: 20px;
  margin-right: 3px;
}

GitHub's Contribution Chart

With a few lines of extra code we can convert the bar chart above to a contribution chart similar to Github's.

A GitHub-style contribution chart

See the codepen

Rather than setting a height based on the data's value we can set a background-color instead.

const colorMap = d3.interpolateRgb(
  d3.rgb('#d6e685'),
  d3.rgb('#1e6823')
)

d3.select('#chart')
  .selectAll("div")
  .data([.2, .4, 0, 0, .13, .92])
  .enter()
  .append("div")
  .style("background-color", (d)=> {
    return d == 0 ? '#eee' : colorMap(d)
  })

The colorMap function takes an input value between 0 and 1 and returns a color along the gradient of colors between the two we provide. Interpolation is a key tool in graphics programming and animation, we'll see more examples of it later.

An SVG Primer

Much of D3's power comes from the fact that it works with SVG, which contains tags for drawing 2D graphics like circles, polygons, paths and text.

A collection of shapes and text

<svg width="200" height="200">
  <circle fill="#3E5693" cx="50" cy="120" r="20" />
  <text x="100" y="100">Hello SVG!</text>
  <path d="M100,10L150,70L50,70Z" fill="#BEDBC3" stroke="#539E91" stroke-width="3">
</svg>

The code above draws:

  • A circle at 50,120 with a radius of 20
  • The text "Hello SVG!" at 100,100
  • A triangle with a 3px border, the d attribute has the following instructions
    • Move to 100,10
    • Line to 150,70
    • Line to 50,70
    • Close path(Z)

<path> is the most powerful element in SVG.

Circles

Labeled circles showing sales by time of day

See the codepen

The data sets in the previous examples have been a simple array of numbers, D3 can work with more complex types too.

const data = [{
  label: "7am",
  sales: 20
},{
  label: "8am",
  sales: 12
}, {
  label: "9am",
  sales: 8
}, {
  label: "10am",
  sales: 27
}]

For each point of data we will append a <g>(group) element to the #chart and append <circle> and <text> elements to each with properties from our objects.

const g = d3.select('#chart')
  .selectAll("g")
  .data(data)
  .enter()
  .append('g')
g.append("circle")
  .attr('cy', 40)
  .attr('cx', (d, i)=> (i+1) * 50)
  .attr('r', (d)=> d.sales)
g.append("text")
  .attr('y', 90)
  .attr('x', (d, i)=> (i+1) * 50)
  .text((d)=> d.label)

The variable g holds a d3 "selection" containing an array of <g> nodes, operations like append() append a new element to each item in the selection.

This code maps the input data into this SVG document, can you see how it works?

Continue reading %Learn to Create D3.js Data Visualizations by Example%


by Mark Brown via SitePoint

Using the New ES6 Collections: Map, Set, WeakMap, WeakSet

This article is part of a web development series from Microsoft. Thank you for supporting the partners who make SitePoint possible.

Most major programming languages have several types of data collections. Python has lists, tuples, and dictionaries. Java has lists, sets, maps, queues. Ruby has hashes and arrays. JavaScript, up until now, had only arrays. Objects and Arrays were the workhorses of JavaScript. ES6 introduces four new data structures that will add power and expressiveness to the language: Map, Set, WeakSet, and WeakMap.

Let's examine these four new collections and the benefits they provide.

Searching for the JavaScript HashMap

HashMaps, dictionaries, and hashes are several ways that various programming languages store key/value pairs and these data structures are optimized for fast retrieval.

In ES5, JavaScript objects–which are just arbitrary collections of properties with keys and values–can simulate hashes, but there are several downsides to using objects as hashes.

Downside #1: Keys must be strings in ES5

JavaScript object property keys must be strings, which limits their ability to serve as a collection of key/value pairs of varying data types. You can of course coerce/stringify other data types into strings, but this adds extra work.

Downside #2: Objects are not inherently iterable

Objects weren’t designed to be used as collections, and as a result there’s no efficient way to determine how many properties an object has (see, for e.g., Object.keys is slow). When you loop over an object’s properties, you also get its prototype properties. You could add the iterable property to all objects but not all objects are meant to be used as collections. You could use the for...in loop and the hasOwnProperty() method, but this just a workaround. When you loop over an object’s properties, the properties won’t necessarily be retrieved in the same order they were inserted.

Downside #3: Challenges with built-in method collisions

Objects have built in methods like constructor, toString, and valueOf. If one of these was added as a property, it could cause collisions. You could use Object.create(null) to create a bare object (which doesn't inherit from object.prototype), but, again, this is just a workaround.

ES6 includes new collection data types, so there is no longer a need to use objects and live with their drawbacks.

Using ES6 MapCollections

Map is the first data structure/collection we’ll examine. Maps are collections of keys and values of any type. It’s easy to create new Maps, add/remove values, loop over keys/values and efficiently determine their size. Here are the crucial methods:

Creating a map and using common methods

Example code - 1

Code Example: http://ift.tt/29w3wzZ

Using the SetCollection

Sets are ordered lists of values which contain no duplicates. Instead of being indexed like an arrays are, sets are accessed using keys. Sets already exist in Java, Ruby, Python, and many other languages. One difference between ES6 Sets and those in other languages is that the order matters in ES6 (not so in many other languages). Here are the crucial Set methods:

Example code - 2

Code Example: http://ift.tt/29xpP4H

Weak Collections, Memory, and Garbage Collections

JavaScript Garbage Collection is a form of memory management whereby objects that are no longer referenced are automatically deleted and their resources are reclaimed.

Continue reading %Using the New ES6 Collections: Map, Set, WeakMap, WeakSet%


by Kyle Pennell via SitePoint

Aplus

Aplus creative pack, kit or whatever


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

Global Sports Commerce

Website is a showcase of a global sports family which consists of 8 companies which caters almost to every domain in sports industry


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

Unity

Unity – HTML Responsive Multi-Purpose Template with 32+ Demos 151+HTML Files,116 PSD Files and 50+ Headers with Unlimited Options


by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery

Quick Tip: Testing Symfony Apps with a Disposable Database

Testing code that interacts with a database can be a massive pain. Some developers mock database abstractions, and thus do not test the actual query. Others create a test database for the development environment, but this can also be a pain when it comes to continuous integration and maintaining the state of this database.

Symfony logo

In-memory databases are an alternative to these options. As they exist only in the memory of the application, they are truly disposable and great for testing. Thankfully, these are very easy to set up with Symfony applications that use Doctrine. Try reading our guide to functional testing with Symfony after this to learn about testing the end-to-end behaviour of an application.

Continue reading %Quick Tip: Testing Symfony Apps with a Disposable Database%


by Andrew Carter via SitePoint