Data circles

Updated 7 years ago by Anna Powell-Smith

How to use this template

Sample Flourish template: data circles

This sample template demonstrates how to use data tables.

Use the Flourish SDK to try it out.

The main code file for the template is src/index.js.

Sample data

The sample data was generated with the following code:

const words = require("fs").readFileSync("/usr/share/dict/words").toString("utf-8").split("\n");
for (let i=0; i < 25; i++) {
    const x = Math.random(),
          y = Math.random(),
          size = Math.random() * 10000,
          word = words[Math.floor(Math.random()*words.length)];

    console.log(`${x},${y},${size},${word}`);
}

This section documents API usage specific to this template, so for an introduction we suggest you refer to the generic API documentation instead.

template: _113

version: _1

Template data

There are three different formats in which you can supply data to this template. The most convenient for you to use likely depends on the source of your data, as described below.

1. Array of arrays, and a bindings object

You can supply arrays of arrays to opts.data, which might look like:

{
    data: {
        circles: [
            [ "CirclesColumn1Value1", "CirclesColumn2Value1",
            [ "CirclesColumn1Value2", "CirclesColumn2Value2",
            [ "CirclesColumn1Value3", "CirclesColumn2Value3",
            ...
        ]
    }
}

where each array of arrays represents the rows in a data sheet.

To tell the API how the values from each column should be associated with the keys that the template is expecting, you must also supply an object attached to opts.bindings. (The meanings of the keys in the bindings object are documented below.) The minimal bindings you can supply for this template are as shown in this example:

{
    template: "_113",
    version: "_1",
    bindings: {
        circles: {
            x: 0, // index of a column in your data
            y: 1, // index of a column in your data
            size: 2, // index of a column in your data
            word: 3, // index of a column in your data
        }
    },
    data: {
        circles: [
            [ "CirclesColumn1Value1", "CirclesColumn2Value1",
            [ "CirclesColumn1Value2", "CirclesColumn2Value2",
            [ "CirclesColumn1Value3", "CirclesColumn2Value3",
            ...
        ]
    }
}

All possible bindings that you can supply are shown in this example:

{
    template: "_113",
    version: "_1",
    bindings: {
        circles: {
            x: 0, // index of a column in your data
            y: 1, // index of a column in your data
            size: 2, // index of a column in your data
            word: 3, // index of a column in your data
        }
    },
    data: {
        circles: [
            [ "CirclesColumn1Value1", "CirclesColumn2Value1",
            [ "CirclesColumn1Value2", "CirclesColumn2Value2",
            [ "CirclesColumn1Value3", "CirclesColumn2Value3",
            ...
        ]
    }
}

2. Array of objects with arbitrary keys, and a bindings object

This format is most likely useful when you have data from an external source, such as CSV data loaded from d3-dsv. You should supply this attached to the opts.data, which might look like:

{
        circles: [
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            ...
        ]
    }

... but with the keys being the column headers from your source data instead. You must also supply an object attached to opts.bindings. The minimal bindings you can supply for this template are as shown in this example:

{
    template: "_113",
    version: "_1",
    bindings: {
        circles: {
            x: "CirclesHeader1",
            y: "CirclesHeader2",
            size: "CirclesHeader3",
            word: "CirclesHeader4",
        }
    },
    data: {
        circles: [
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            ...
        ]
    }
}

All possible bindings that you can supply are shown in this example:

{
    template: "_113",
    version: "_1",
    bindings: {
        circles: {
            x: "CirclesHeader1",
            y: "CirclesHeader2",
            size: "CirclesHeader3",
            word: "CirclesHeader4",
        }
    },
    data: {
        circles: [
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            { "CirclesHeader1": ..., "CirclesHeader2": ..., ... },
            ...
        ]
    }
}

(As before, the keys containing "Header" would be replaced by column names from your data source.)

3. Array of objects with template-defined keys

There is an alternative format you can use, which is likely to be easier to use if your data is not from a spreadsheet source. With this alternative format you supply your data to the template as an array of objects, attached to opts.data, where the keys must be those used by the template, as documented below. In this case there is no need to supply a bindings object, since the key names are already those expected by the template. The required properties in the data object are as follows (scroll down for a description of what each property is):

{
    template: "_113",
    version: "_1",
    data: {
    circles: [
        {
            x: ...,
            y: ...,
            size: ...,
            word: ...
        },
        ...
    ]
},
    ...
}

And the full list of all possible properties is as follows:

{
    template: "_113",
    version: "_1",
    data: {
    circles: [
        {
            x: ...,
            y: ...,
            size: ...,
            word: ...
        },
        ...
    ]
},
    ...
}

Meanings of the template data keys:

  • circles.x: x
  • circles.y: y
  • circles.size: size
  • circles.word: word

Template settings

Options for opts.state.

color color

Color. The color of the circles

opacity number

Opacity. The opacity of the circles

Max: 1