Giter Site home page Giter Site logo

assignment-4-b's Introduction

Assignment-4-B

This assignment requires you to draw two time series as line graphs. Similar to the exercise during class, you will use d3's built-in svg generators to create <path> elements with the right geometry.

Creating data with the right structure

The dimensionality problem we discussed in class boils down to this: in drawing two time series for coffee and tea production between 1963 and 2013, we expect to see 2 (two) series of data, with 51 data points each.

However, .csv files can't represent multi-dimensional, nested data structures. The data you initially import is a one-dimensional array with 102 data points, with coffee and tea mixed together like thus:

  [
    {item:"Tea",year:1963,value:1021949},
    {item:"Tea",year:1964,value:1063199},
    ...
    {item:"Coffee",year:1963,value:4152127}
  ]

Your job is to use d3.nest() to create the right structure out of this data, so that it will look like this instead:

  [
    {
      key:"Tea",
      values:[
        {item:"Tea",year:1963,value:1021949},
        {item:"Tea",year:1964,value:1063199},
        ...
      ]
    },
  
    {
      key:"Coffee",
      values:[
        {item:"Coffee",year:1963,value:4152127},
        ...
      ]
    }
  ]

To emphasize the distinction, the new data contains 1 (one) top-level array of 2 (two) objects, with each object in turn containing 1 (one) array each of 51 data points. Now you are ready to create two <path> elements.

For your reference, this link contains a good tutorial for nesting data.

Hint: Data to DOM

There are many ways to create two <path> elements, each representing an item (coffee or tea) over a period of time. Given the data structure we've created using d3.nes() above, we can actually use the .selectAll() - .data() - .enter() pattern again. It works as follows:

var timeSeries = d3.selectAll('path') //yields a selection of 0 <path> elements
  .data(data) //joins to an array of two objects
  .enter()
  .append('path') //creates two new <path> elements as the enter set
  .attr('class', function(item){return item.key}) //each element will have class of either "coffee" or "tea"

Carefully consider what the data element joined to each <path> DOM element looks like, then look at the following:

timeSeries
  .attr('d', function(item){
    return lineGenerator(item.values);
  });

Practice the Tooltip Pattern

Practice the tooltip pattern that we worked with in class, then consider the following: how would user interactions work with the <path> element? The answer is not so simple, because as the user hovers over the <path>, we have no way of knowing which year he/she is querying for.

User interactions will a <path> element that represents an x-y series will be covered later.

assignment-4-b's People

Contributors

viztech avatar siqister avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.