0.2.1 • Published 9 years ago

vcharts v0.2.1

Weekly downloads
3
License
Apache-2.0
Repository
github
Last release
9 years ago

vcharts

Reusable Vega charts.

Getting Started

Installation with bower

bower install vcharts

Setup a scaffold index.html with the following contents:

<html>
<head>
  <meta charset="UTF-8">
  <script src="bower_components/d3/d3.min.js"></script>
  <script src="bower_components/vega/vega.min.js"></script>
  <script src="bower_components/vcharts/vcharts.min.js"></script>
</head>
<body>
  <div id="vis"></div>
<script>
vcharts.chart('xy', {
  el: '#vis',
  series: [
    {
      name: 'series1',
      values: [
        {x: 0, y: 0},
        {x: 1, y: 1},
        {x: 2, y: 2},
        {x: 3, y: 3}
      ],
    }
  ]
});
</script>
</body>

Installation with npm

npm install vcharts vega d3

Setup a scaffold index.html with the following contents:

<html>
<head>
  <meta charset="UTF-8">
  <script src="node_modules/d3/d3.min.js"></script>
  <script src="node_modules/vega/vega.min.js"></script>
  <script src="node_modules/vcharts/vcharts.min.js"></script>
</head>
<body>
  <div id="vis"></div>
<script>
vcharts.chart('xy', {
  el: '#vis',
  series: [
    {
      name: 'series1',
      values: [
        {x: 0, y: 0},
        {x: 1, y: 1},
        {x: 2, y: 2},
        {x: 3, y: 3}
      ],
    }
  ]
});
</script>
</body>

View your visualization

Start your favorite local web server:

python -m SimpleHTTPServer 8080 .

Visit http://localhost:8080/index.html.

API

vcharts.chart()

Initialize a chart with the call

var chart = vcharts.chart(chartType, options);

where chartType is one of the supported chart types.

The options argument is an object specifying the chart's options. The following options are available to all charts:

OptionTypeDescription
elDOM element or selector stringThe container for the visualization.
rendererStringWhether to render in "svg" or "canvas" mode (default "canvas").
widthNumberWidth of the chart in pixels.
heightNumberHeight of the chart in pixels.

If width and/or height are not specified, they are computed based on the width and height of the enclosing element el.

chart.update()

Note that the width and height options are not dynamically bound if left unset, so if the width or height of el changes, or has just been set programmatically, you may need to call update either in a setTimeout or window.resize callback. The following will set the width and height correctly after a DOM update:

var div = $('#mydiv').css('width', '100px').css('height', '400px');

// Width and height will not be picked up yet since the DOM is not updated.
var chart = vcharts.chart(chartType, { el: div.get(0), /* more options */ });

// Refresh width and height here.
setTimeout(function () { chart.update(); }, 1);

The following will resize the chart when the window resizes:

var chart = vcharts.chart(chartType, { el: '#mydiv', /* more options */ });

// Update size on window resize.
window.onresize = function () {
    chart.update();
};

vcharts.chart('vega', options)

Generic Vega renderer. The following additional option is supported:

OptionTypeDescription
specObjectThe Vega spec to render.

vcharts.chart('xy', options)

Plots (x,y) coordinate pairs as points and/or lines. The following additional options are supported:

OptionTypeDescription
seriesArray of SeriesThe data series to render.
xAxisAxisAn object describing the x axis.
yAxisAxisAn object describing the y axis.
tooltipStringMustache-style string template where d is the hovered data element, e.g. '{{d.x}} -- {{d.y}}'.

Series

A series describes the data and visual mappings for a list of x,y coordinates. Series objects have the following options:

OptionTypeDescription
nameStringThe name of the series to show in the legend.
valuesArrayThe array of items in the series.
xStringX position field.
yStringY position field.
colorStringColor as any CSS-compatible color string representation (e.g. 'blue', '#ffffff').
lineBooleanConnect the series with a line (default true).
pointBooleanRender points (default false).
pointSizeNumberThe size of points in square pixels.
lineWidthNumberThe width of the line in pixels.

Axis

An axis describes how to scale and display an axis. Axis objects have the following options:

OptionTypeDescription
titleStringThe axis title.
typeStringThe mode for axis scale, either 'linear' (default) or 'time'.
domainArrayTwo-element domain for the axis range of the form min, max. Defaults to the range of the data.
gridBooleanShow gridlines (default false).
panBooleanAllow panning this axis with mouse drags (default true).
zoomBooleanAllow zooming this axis with mouse wheel or swipe (default true).
ticksArraySpecific values for tick marks on the axis.

vcharts.chart('bullet', options)

Bullet graphs based on the description by Perceptual Edge. The following additional options are supported:

OptionTypeDescription
valueNumberThe value to display as a solid bar.
titleStringTitle to display to the left.
subtitleStringSubtitle to display below the title.
markersArrayComparative markers to display with the form {value: Number}, displayed as a vertical line.
rangesArray of RangeBackground ranges to display under the chart.
axisFontSizeNumberFont size for axis labels.
labelFontSizeNumberFont size for value label.
titleFontSizeNumberFont size for title.
subtitleFontSizeNumberFont size for subtitle.

Example

vcharts.chart('bullet', {
  el: '#vis',
  value: 0.8,
  title: 'Error',
  subtitle: '% deviation from ground truth',
  markers: [{value: 0.05}],
  ranges: [
    {min: 0, max: 0.1, background: '#eeeeee'},
    {min: 0.1, max: 0.75, background: '#aaaaaa'},
    {min: 0.75, max: 1, background: '#888888'}
  ]
});

Range

A range represents a visual range of an axis with background and foreground colors. Options available are:

OptionTypeDescription
minNumberThe minimum value of the range.
maxNumberThe maximum value of the range.
backgroundStringThe background color of the range.
foregroundStringThe color of values and markers that fall in this range (default: 'black').

vcharts.chart('bar', options)

Plots a bar chart. The following additional options are supported:

OptionTypeDescription
valuesArrayThe array of items in the series.
xStringField used to set the bar x position.
yStringField used to set bar height.
fillStringFill color for the bars.
hoverStringFill color when bar is hovered.
xAxisAxisAn object describing the x axis.
yAxisAxisAn object describing the y axis.
tooltipStringMustache-style string template where d is the hovered data element, e.g. '{{d.x}} -- {{d.y}}'.

vcharts.chart('histogram', options)

OptionTypeDescription
valuesArrayThe array of items in the series.
binStringField to use for bin values.
discreteBooleanIf true, treats values as discrete and makes bins for each unique value. If false, treats values as continuous and makes bins that span the range of the data.
maxBinsNumberThe maximum number of bins to use. Unused if discrete is true.
fillStringFill color for the bars.
hoverStringFill color when bar is hovered.
xAxisAxisAn object describing the x axis.
yAxisAxisAn object describing the y axis.
tooltipStringMustache-style string template where d is the hovered bin with the fields bin and count, e.g. 'Count: {{d.count}}'.

vcharts.chart('xymatrix', options)

OptionTypeDescription
valuesArrayThe array of items in the series.
fieldsArrayThe list of fields to use in the matrix of scatterplots.
color.fieldStringIf set, colors by this field.
color.typeStringIf color.field is set, specifies the type of scale (e.g. "ordinal" or "linear").
color.valueStringIf set, colors by this constant color.

vcharts.chart('box', options)

Statistical box plot based on work by Jeffrey Heer and Randy Zwitch.

OptionTypeDescription
valuesArrayThe array of items in the series.
fieldsArrayThe fields containing the values to summarize in the box plot.
groupStringThe field to group by for multiple boxes per field (optional).
fillStringThe fill color for the box.
orientStringThe orientation of the boxes, either horizontal or vertical.
boxSizeNumberA number from 0 to 1 to specify box size, where a value of 1 makes box widths touch each other, and lower numbers produce spacing between boxes.
capSizeNumberA number from 0 to 1 to specify end cap size, where a value of 1 makes caps widths touch each other, and lower numbers produce spacing between caps.

vcharts.chart('gantt', options)

Gantt chart.

OptionTypeDescription
valuesArrayThe array of items in the Gantt chart. Each object should have the fields label (the name of the item), level (either 1 for top-level or 2 for secondary), enter (the start value), and leave (the end value). Each item will be represented by a horizontal bar from enter to leave.
xAxisAxisAttributes for the x (time) axis.

Development Build

Clone the repository:

git clone https://github.com/XDATA-Year-3/vcharts.git
cd vcharts

Build the library:

npm run build

Run the tests:

npm run test

Release Instructions

To release a new verison x.y.z, first release to npm:

export VERSION=x.y.z
npm run test && npm run lint && npm run build # Should run without errors
git checkout -b version-$VERSION

Edit package.json to set the version to x.y.z, then:

git commit -am "Version bump to $VERSION"
git push -u origin version-$VERSION

Create PR on GitHub and merge to master, then:

git checkout master
git pull
npm run test && npm run lint && npm run build # This should again run without errors.
npm publish

Next, release to Bower, which amounts to creating a tag with the appropriate name that contains the built library, which is normally gitignored.

git checkout -b bower-$VERSION
git add -f vcharts*
git commit -am "Bower $VERSION release"
git tag $VERSION
git push origin $VERSION

Go to GitHub releases page and edit the tag to make it a real GitHub release.

To make a new version of the website:

git checkout master
npm run build
git branch -D gh-pages
git checkout -b gh-pages
git add -f vcharts*
git add -f node_modules/d3/d3.*
git add -f node_modules/vega/vega.*
git commit -m "Updating website for $VERSION"
git push -f -u origin gh-pages
git checkout master
rm -rf node_modules/
npm install
npm run build
0.2.1

9 years ago

0.2.0

9 years ago

0.1.7

9 years ago

0.1.6

9 years ago

0.1.5

9 years ago

0.1.4

9 years ago

0.1.3

9 years ago