npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datagovsg-plottable-charts

v3.4.10

Published

Reusable Plottable chart components

Downloads

10

Readme

datagovsg-plottable-charts

Motivation

Generating a chart is easy, making it looks beautiful requires much more effort. Numerous charting libraries have been written to solves the basic problem of converting data to chart objects. Regardless of the library you choose, out-of-the-box defaults hardly produces the look you want.

Charts on Data.gov.sg are rendered using the Plottable library. Based on D3, it is highly flexible and gives you many low level controls to fine-tune every single detail. However, this power comes at the price of additional configurations. We want to abstract away these configurations by creating wrappers that pre-apply all the styles we want on our component. That's why we created this library.

What it does for you

Tooltips

popovers

Pie chart labels

outerlabels

Automatically downsample tick marks

downsampleticks

And many more...

How to use

Dependencies
  • D3
  • Plottable
  • JQuery (optional, only if you require tooltip)
Use with bundler (Webpack, Rollup etc)
npm install --save datagovsg-plottable-charts
<!-- html -->
<link rel="stylesheet" href="lib/plottable.css">
<link rel="stylesheet" href="lib/datagovsg-charts.css">
<!-- ... -->
<script src="lib/d3.min.js"></script>
<script src="lib/plottable.min.js"></script>
/* js */
import {SimplePie} from 'datagovsg-plottable-charts'

// Instantiate the chart component
const pie = new SimplePie(props)

// Mount component
pie.mount(document.getElementById('ctn'))

// Update chart
pie.update(newProps)
Using plugins
import {
  highlightOnHover,
  setupOuterLabel
} from 'datagovsg-plottable-charts/dist/plugins'

highlightOnHover(pie)
setupOuterLabel(pie)
Use without a bundler
<!-- html -->
<script src="lib/datagovsg-charts.min.js"></script>
/* js */
const {SimplePie, plugins} = window.DatagovsgCharts
const {highlightOnHover, setupOuterLabel} = plugins

const pie = new SimplePie(props)
highlightOnHover(pie)
setupOuterLabel(pie)

Full Component List

Pre-styled
Unstyled

Full Plugin List

Useful helpers

PivotTable

Example usage
import {DatagovsgLine} from 'datagovsg-plottable-charts'
import PivotTable, {
  filterItems,
  filterGroups,
  groupItems,
  aggregate
} from 'datagovsg-plottable-charts/dist/PivotTable'

const pivotTable = new PivotTable(data)

pivotTable.push(
  filterItems('income', {type: 'exclude', values: ['-', 'na']}),
  groupItems('gender'),
  filterGroups('gender', {type: 'exclude', values: ['Total']})
  aggregate('year', 'income')
)

const processedData = pivotTable.transform()

const series = processedData.map(g => ({
  label: g._group.gender,
  series: g._summaries[0].series
}))

const chart = new DatagovsgLine({data: series})
chart.mount(document.getElementById('chart'))
How it works

Original data

| year | gender | income | | ---- | ------ | ------ | | 2006 | Total | 2042 | | 2016 | Total | 3250 | | 2017 | Total | - | | 2006 | Male | 2213 | | 2016 | Male | 3500 | | 2006 | Female | 1875 | | 2016 | Female | 2979 |

Transform into custom data structure

pivotTable.transform()
// returns
[
  {
    _group: {},
    _items: [
      {year: 2006, gender: 'Total', income: 2042},
      {year: 2016, gender: 'Total', income: 3250},
      {year: 2016, gender: 'Total', income: '-'},
      {year: 2006, gender: 'Male', income: 2213},
      {year: 2016, gender: 'Male', income: 3500},
      {year: 2006, gender: 'Female', income: 1875},
      {year: 2016, gender: 'Female', income: 2979}
    ],
    _summaries: []
  }
]

filterItems( )

pivotTable.push(
  filterItems('income', {type: 'exclude', values: ['-', 'na']})
)
pivotTable.transform()
// returns
[
  {
    _group: {},
    _items: [
      {year: 2006, gender: 'Total', income: 2042},
      {year: 2016, gender: 'Total', income: 3250},
      {year: 2006, gender: 'Male', income: 2213},
      {year: 2016, gender: 'Male', income: 3500},
      {year: 2006, gender: 'Female', income: 1875},
      {year: 2016, gender: 'Female', income: 2979}
    ],
    _summaries: []
  }
]

groupItems( )

pivotTable.push(
  filterItems('income', {type: 'exclude', values: ['Total']}),
  groupItems('gender')
)
pivotTable.transform()
// returns
[
  {
    _group: {gender: 'Total'},
    _items: [
      {year: 2006, gender: 'Total', income: 2042},
      {year: 2016, gender: 'Total', income: 3250},
    ],
    _summaries: []
  },
  {
    _group: {gender: 'Male'},
    _items: [
      {year: 2006, gender: 'Male', income: 2213},
      {year: 2016, gender: 'Male', income: 3500},
    ],
    _summaries: []
  },
  {
    _group: {gender: 'Female'},
    _items: [
      {year: 2006, gender: 'Female', income: 1875},
      {year: 2016, gender: 'Female', income: 2979}
    ],
    _summaries: []
  }
]

filterGroups( )

pivotTable.push(
  filterItems('income', {type: 'exclude', values: ['-', 'na']}),
  groupItems('gender'),
  filterGroups('gender', {type: 'exclude', values: ['Total']})
)
pivotTable.transform()
// returns
[
  {
    _group: {gender: 'Male'},
    _items: [
      {year: 2006, gender: 'Male', income: 2213},
      {year: 2016, gender: 'Male', income: 3500},
    ],
    _summaries: []
  },
  {
    _group: {gender: 'Female'},
    _items: [
      {year: 2006, gender: 'Female', income: 1875},
      {year: 2016, gender: 'Female', income: 2979}
    ],
    _summaries: []
  }
]

aggregate( )

pivotTable.push(
  filterItems('income', {type: 'exclude', values: ['-', 'na']}),
  groupItems('gender'),
  filterGroups('gender', {type: 'exclude', values: ['Total']})
  aggregate('year', 'income')
)
pivotTable.transform()
// returns
[
  {
    _group: {gender: 'Male'},
    _items: [
      {year: 2006, gender: 'Male', income: 2213},
      {year: 2016, gender: 'Male', income: 3500},
    ],
    _summaries: [
      {
        labelField: 'year',
        valueField: 'income',
        series: [
          {label: 2006, value: 2213},
          {label: 2016, value: 3500}
        ]
      }
    ]
  },
  {
    _group: {gender: 'Female'},
    _items: [
      {year: 2006, gender: 'Female', income: 1875},
      {year: 2016, gender: 'Female', income: 2979}
    ],
    _summaries: [
      {
        labelField: 'year',
        valueField: 'income',
        series: [
          {label: 2006, value: 1875},
          {label: 2016, value: 2979}
        ]
      }
    ]
  }
]
Using without bundler
<!-- html -->
<script src="lib/pivot-table.min.js"></script>
/* js */
const PivotTable = window.PivotTable
const {filterItems, groupItems, filterGroups, aggregate} = PivotTable

Debugging guide

  1. Clone the datagovsg/datagovsg-plottable-charts repo
  2. cd to the cloned repo
  3. Run npm install
  4. Change main field in the package.json to "main": "src/index.js"
  5. Delete module field in the package.json
  6. Set up a symlink sudo npm link
  7. cd to your working directory
  8. Run npm link datagovsg-plottable-charts