Home

Awesome

Ember-C3

npm version Ember Observer Score Join the chat at https://gitter.im/Glavin001/ember-c3

Ember component library for C3, a D3-based reusable chart library.

See the demo here

Compatibility

Installation

ember install ember-c3

Usage

Component usage and properties are below. The code for these example charts and more is in the dummy app source code.


Combination
image
Timeseries
ember-c3-timeseries-4
GaugePieDonut
ember-c3-gauge-2ember-c3-pie-1ember-c3-donut-1

Basic

Where this.model is your C3 data and chart options:

<C3Chart @data={{this.model}} />

Advanced

See http://c3js.org/examples.html for examples of how to use C3.

Component Arguments

The arguments match the corresponding C3 options found in the C3 Documentation. As documented, most C3 settings (i.e. bar, axis, size, etc) can be included in the data object.

Arguments break out the settings to simplify chart configuration. Note: The chart type must be assigned in the chart data object.

Properties marked with an asterisk (*) will update the chart when the property changes. See examples in the dummy app.

PropertyDescriptionExample
c3chartAssigns the generated C3 chart object to the passed property. Any C3 API method can be used with this propertychart.hide("data1")
data*C3 data object. data is mutable after the chart is created
axis*C3 axis object. See C3 examples for combining with data object. Chart axis are mutable after the chart is created
barUsed to assign bar chart properties
pieUsed to assign pie chart properties
donutUsed to assign donut chart properties
gaugeUsed to assign gauge chart properties
lineUsed to assign line chart properties
areaUsed to assign area chart properties
pointUsed to assign data point properties
gridUsed to show, hide and modify the graph grid. See docs
legendShow, hide and modify the legend position. See docs
tooltipShow, hide and modify the tooltip. See docs
subchartShow, hide and modify C3 sub charts. See docs
zoomSet C3 zoom features. See docs
sizeControl chart size. See docssize: {width: 640 }
paddingSet padding around graph. See docspadding: { top: 20}
titleSet chart titletitle: { text: "This is my chart" }
interactionEnable or disable interactionsinteraction: { enabled: false }
color*Used to assign color properties. Chart colors are mutable after chart creation
dtitleDynamically change the chart title. See details below
transitionEquivalent to transition.duration. Default duration is 350ms. Transition times less than 300ms may not render properly. Use chart.load() and .unload() if shorter times are required
unloadDataBeforeChangeWhen set to true existing data will be unloaded before new data is loaded. Useful for pie and donut chart data changes. You can manually load/unload data using chart.load() and chart..unload()

dtitle

The dtitle property is used to dynamically change a chart's title. C3 doesn't natively support this without forcing a chart redraw which can cause side effects.

The title can be set by targeting the .c3-title class but that doesn't provide abstraction from C3's internals.

dtitle gives you some control over side effects using a parameter to control how the graph is refreshed. An object with the new title and a refresh parameter is used to indicate whether all properties should be refreshed or only the chart title.

Setting refresh to false will only refresh the title and ignore changes to the data, colors and axis properties. A short example is below. See the drill down example in the dummy app to see how dttile is used and potential side effects.

The chart's initial title is set using the title parameter.

<C3Chart
  @data={{this.graphData}}
  @title={{this.title}}
  @dtitle={{this.dtitle}}
/>

<button {{on 'click' this.changeTitle}}>Change Title</button>
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { tracked } from '@glimmer/tracking';

export default class ApplicationController extends Controller {
  @tracked title = { text: 'Coffee Brewing' };
  @tracked dtitle;

  get graphData() {
    return {
      columns: [
        ['Cold Brewed', 12],
        ['Drip', 67],
        ['French Press', 14],
        ['Iced', 38],
        ['Percolated', 64]
      ],
      type: 'pie'
    };
  }

  @action
  changeTitle() {
    this.dtitle = { text: 'Making coffee!', refresh: false };
  }
}

C3 Methods

If you assign a controller property to the c3chart property, you can use most of C3's api methods. Not all the methods have been tested.

{{! templates/application.hbs }}
<C3Chart @data={{this.baseData}} @c3chart={{this.chart}} />

<button {{on 'click' this.loadUS}}>US Cars</button>
<button {{on 'click' this.loadGerman}}>German Cars</button>
<button {{on 'click' this.resetData}}>Reset</button>
// controllers/application.js
import { action } from '@ember/object';
import Controller from '@ember/controller';

export default class ApplicationController extends Controller {
  chart = null;

  baseData = {
    columns: [
      ['US', 64],
      ['German', 36]
    ],
    type: 'donut'
  };

  modelsGerman = [
    ['Mercedes', 12],
    ['Volkswagon', 54],
    ['BMW', 34]
  ];

  modelsUS = [
    ['Ford', 35],
    ['Chevy', 26],
    ['Tesla', 2],
    ['Buick', 10],
    ['Dodge', 27]
  ];

  @action
  resetData() {
    this.chart.load(this.baseData);
    this.chart.unload([
      'Mercedes',
      'Volkswagon',
      'BMW',
      'Ford',
      'Chevy',
      'Tesla',
      'Buick',
      'Dodge'
    ]);
  }

  @action
  loadUS() {
    this.chart.load({ columns: this.modelsUS });
    this.chart.unload('US', 'German');
  }

  @action
  loadGerman() {
    this.chart.load({ columns: this.modelsGerman });
    this.chart.unload('US', 'German');
  }
}

C3 Events

C3 emits two types of events - chart and data events. Chart events are assigned callback functions using component arguments.

Data events must be assigned to an action in the data object.

Chart events supported by ember-c3.

EventsDescriptionExample
oninitTriggered when chart is initialized@oninit={{this.init}}
onrenderedTriggered when chart is rendered or redrawn@onrendered={{this.render}}
onmouseoverTriggered when mouse enters the chart@onmouseover={{this.mouseover}}
onmouseoutTriggered when mouse leaves the chart@onmouseout={{this.mouseout}}
onresizeTriggered when screen is resized@onresize={{this.resize}}
onresizedTriggered when resizing is completed@onresized={{this.resized}}

Callback functions will receive the C3 chart object which can be used to modify the chart or as data source for other operations. The @oninit event does not receive the chart object because the chart has not been created yet. See the chart events example can be used in the dummy app.

C3 data events such as onclick, onmouseover and onmouseout are assgined callback functions in the chart configuration or data settings. Data events supply the data names and values based on mouse location.

A data event example is shown below. Note that data callbacks require bind. See the dummy app for more examples.

{{! templates/application.hbs }}
<C3Chart @data={{this.data}} @oninit={{this.setup}} />
// controllers/application.js
import { action } from '@ember/object';
import Controller from '@ember/controller';
import { bind } from '@ember/runloop';

export default class ApplicationController extends Controller {
  get data() {
    // iris data from R
    return {
      columns: [
        ['data1', 30],
        ['data2', 120],
        ['data3', 10],
        ['data4', 45],
        ['data5', 90]
      ],
      type: 'pie',
      // override component onclick event handler
      // bind is required for data events
      onclick: bind(this, this.onClick)
    };
  }

  // oninit chart event
  @action
  setup() {
    console.log('chart inited');
  }

  // data event - triggered when pie slice is clicked
  onClick(data, elem) {
    alert(`Data ${data.name} has a value of ${data.value}`);
  }
}

Accessing D3

You can use the D3 library in your application by importing it where needed

import d3 from 'd3';

See the D3 example in the dummy app.

Helpful Links

See the Contributing guide for details.

License

This project is licensed under the MIT License.