Giter VIP home page Giter VIP logo

module-georgia-turnout-chart's Introduction

POLITICO

politico-turnout-chart

Chart module for {your chart type here}.

Install

$ npm install --save git+ssh://[email protected]:The-Politico/graphic_politico-turnout-chart
Requirements

This module uses ES6 syntax. To use as a pre-compiled module, you'll need a compiler like babel.

Use

In the client, include the global-chart.js bundle, which defines a global chart object, TurnoutChart:

<script src="some/path/to/global-chart.js"></script>

To use as a module, simply import the chart object:

import TurnoutChart from 'politico-turnout-chart';

The chart object has three methods, one to create the chart, initially, another to update chart elements with new data, and one to resize the chart.

var myChart = new TurnoutChart();

// The create method needs a selection string, which will be parent
// to the chart elements, and a data array. You can also provide an
// optional properties object.

const props = {
  stroke: 'orange',
};

myChart.create('#chart', data, props);

// The update method takes new data to update chart elements.
myChart.update(newData);

// The resize method can be called at any point to update the chart's size.
myChart.resize();

To apply this chart's default styles when using SCSS, simply define the variable $TurnoutChart-container to represent the ID or class of the chart's container(s) and import the _chart-styles.scss partial.

$TurnoutChart-container: '#chart';

@import 'path/to/politico-turnout-chart/src/scss/_chart-styles';

Developing the chart

Write your chart code in chart.js and add custom styles to _chart-styles.scss.

Run gulp to compile scripts:

$ gulp

To minimize javascript before publishing:

$ gulp --production

Writing idempotent chart functions

The chart module is structured around an idempotent chart function. Your chart's render method can be called anywhere and will produce the same chart as long as it's called with the same data parameters. This lets you create multiples of your chart easily as well as update the chart with new data.

To help you write idempotent chart functions, the module adds a custom method to d3. appendSelect will either append an element (with a class) if it doesn't exist or will return the selection of the existing element.

selection.appendSelect(<selector_string>, <class_string>);
// class_string is optional

For example:

function render(selection, data) {
  // Use this...
  var svg = d3.select(selection).appendSelect('svg', 'chart');
  // ... instead of awkward constructions like this...
  var svg = d3.select(selection).select('svg.chart').size() === 0 ?
    d3.select(selection).append('svg').attr('class', 'chart') :
    d3.select(selection).select('svg.chart');
  // ...
}
// ... so that you can call this function multiple times without
// creating duplicate svg elements.
render('myChart', data);
render('anotherChart', otherData);
render('myChart', newData);

Configurable chart options

Set configurable options on your chart's props object:

// Set default options.
let props = {
  stroke: '#eee',
  fill: 'orange',
  legend: true,
  // etc.
};

// Then use these props in your chart's render method like this:
circles
  .attr('stroke', props.stroke)
  .attr('fill', props.fill);

// When you call your chart, pass custom props that will
// overwrite defaults.
myChart.create('#chart', data, { stroke: 'orange' });

module-georgia-turnout-chart

module-georgia-turnout-chart's People

Watchers

 avatar  avatar

Forkers

the-politico

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.