Skip to content
Kevin Warne edited this page May 2, 2016 · 32 revisions

Wiki › Charts

For more details about a specific d2b chart type please see one of:

Example Usage

<style>.pie-chart { width: 100%; height: 400px; }</style>

<div class = 'pie-chart'></div>

<script>
	var chart = d2b.chartPie().padding(30);

	var container = d3.select('.pie-chart')
		.datum([
			{label: 'older than 5', value: 2704659},
			{label: '5-13', value: 4499890},
			{label: '14-17', value: 2159981},
			{label: '18-24', value: 3853788},
			{label: '25-44', value: 14106543},
			{label: '45-64', value: 8819342},
			{label: 'younger than 65', value: 612463}
		])
		.call(chart);

	window.onresize = function () {container.call(chart)};
</script>

Most d2b charts share a common interface. The various interface properties and methods that are shared among them are described below.

Generator

# d2b[chart_type]()

Returns a chart generator for the specified chart_type.

# chart(selection)

Render the chart to the given context, which may be either a d3 selection or a corresponding transition.

var chart = d2b.chartPie();

d3.select('body')
    .datum([
      {label: 'Item 1', value: 22},
      {label: 'Item 2', value: 35},
      {label: 'Item 3', value: 16}
    ])
    .call(chart);

Transitions

All charts support d3-transitions. You can simply start the transition and then call the chart generator.

container.transition().call(chart);

note: Some charts have internal transitions that get created after the time that the chart is updated. These transitions are independent of this d3-transition. To change the internal transition duration see chart.duration

Transition to New Data

All charts support smooth transitions between data sets. This can be useful when modifying filters for the data being supplied (e.g. changing the year for which the data represents). To transition to a new data set the new element datum and call the chart generator.

container.datum(dataOne).call(chart);

setTimeout(function () {
	container.datum(dataTwo).transition().call(chart);
}, 2000);		

Properties

There is a set of common properties throughout most of the d2b charts.

# chart.size([size])

If size is specified, sets the size of the chart. size should be in the format {width: 960, height: 540}. If the size is set to null or either the width or height attributes are null, then those dimensions will be dynamically set to as the specified container's width or height.

If size is not specified, returns the current chart size.

# chart.duration([duration_ms])

If duration_ms is specified, sets the animation duration to the specified value in milliseconds. This duration is usually only used for internal transitions that are triggered after the time of chart creation.

If duration_ms is not specified, returns the current chart duration.

# chart.legendAt([at])

If at is specified, sets the position of the legend. at should be a string containing vertical and horizontal components (e.g. 'top left', 'center left', or 'top center').

If at is not specified, returns the current chart legendAt position descriptor.

note: If it is desired for the legend to span horizontally across the top or bottom it may be helpful to use this in conjunction with chart legend's orient property to change the legend orientation from vertical to horizontal.

# chart.padding([at])

If padding is specified, sets the chart padding. If padding is a constant all 4 sides will share this padding value. If padding is an object it should be in the following format {top: 10, left: 20, right: 20, bottom: 10}.

If padding is not specified, returns the current chart padding.

Legend

Many charts us a legend in order to better convey data and it's meaning.

# chart.legend()

This returns the current chart legend. After the legend has been retrieved it can be re-configured.

var legend = chart.legend();

legend
  .orient('horizontal')
  .size(16);

For more legend configuration options proceed to the svg legend component.

Clone this wiki locally