.container.sidemenu
.row
.large-3.medium-4.columns.column-menu
.side-bar
%ul.side-nav
%li GETTING STARTED
%li
%a( href="#setup" ) 1. Setup
%li
%a( href="#generate" ) 2. Generate chart
%li
%a( href="#customize" ) 3. Customize chart
%li
%a( href="#api" ) 4. Use APIs
%li
%a( href="#style" ) 5. Customize style
%li
%a( href="#more" ) 6. And more..
.large-9.medium-8.columns.column-content
%section
%h2 Getting Started
%p In this guide, we are going to show you how to get started with C3.
%hr
%section
%h3
%a( href="#setup" ) 1. Setup
%p Download the latest version here:
%ul
%li https://github.com/c3js/c3/releases/latest
%p Installing by Bower/Component is also available with the name c3.
%p Then, load the scripts and css:
.sourcecode.highlight
%pre
%code.html.xml
:preserve
<link href="/path/to/c3.css" rel="stylesheet">
<script src="/path/to/d3.v5.min.js" charset="utf-8"></script>
<script src="/path/to/c3.min.js"></script>
%p C3 depends on D3, so please load D3 too.
%hr
%section
%h3
%a( href="#generate" ) 2. Generate Chart
%p C3 generates a chart by calling generate() with the argument object, and an element including the chart will insert into the element specified as a selector in that argument as bindto.
%p Prepare the element to bind the chart:
.sourcecode.highlight
%pre
%code.html.xml
:preserve
<div id="chart"></div>
%p And, call generate() with arguments:
.sourcecode.highlight
%pre
%code.javascript
:preserve
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
]
}
});
%p C3 supports the asynchronous module definition (AMD) API. If you use RequireJS, you can load like this:
.sourcecode.highlight
%pre
%code.javascript
:preserve
require.config({
baseUrl: '/js',
paths: {
d3: "http://d3js.org/d3.v5.min"
}
});
require(["d3", "c3"], function(d3, c3) {
c3.generate({
...
});
});
%p Then, you will see the chart:
#chart2_1
%br
%p Data can be loaded as columned data / rowed data / csv in URL.
%p There are serveral options to customize the chart and you can see those here:
%ul
%li Examples
%hr
%section
%h3
%a( href="#customize" ) 3. Customize Chart
%p The chart can be customize by giving some options when generating. We will introduce some of them here.
%h4 1. Additional Axis
%p Introduce additional axis for data2. Add data.axes and axis.y2.show as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
axes: {
data2: 'y2' // ADD
}
},
axis: {
y2: {
show: true // ADD
}
}
});
%p Then, the chart will be like this:
#chart3_1
%br
%h4 2. Show Axis Label
%p Show labels for each axis. Add axis.y.label and axis.y2.label as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
axes: {
data2: 'y2'
}
},
axis: {
y: {
label: { // ADD
text: 'Y Label',
position: 'outer-middle'
}
},
y2: {
show: true,
label: { // ADD
text: 'Y2 Label',
position: 'outer-middle'
}
}
}
});
%p Then, the chart will be like this:
#chart3_2
%br
%h4 3. Change Chart Type
%p Show data2 as Bar chart. Add data.types as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
axes: {
data2: 'y2'
},
types: {
data2: 'bar' // ADD
}
},
axis: {
y: {
label: {
text: 'Y Label',
position: 'outer-middle'
}
},
y2: {
show: true,
label: {
text: 'Y2 Label',
position: 'outer-middle'
}
}
}
});
%p Then, the chart will be like this:
#chart3_3
%br
%h4 4. Format values
%p Format the values of each data. Add axis.y.tick.format as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
var chart = c3.generate({
bindto: '#chart',
data: {
columns: [
['data1', 30, 200, 100, 400, 150, 250],
['data2', 50, 20, 10, 40, 15, 25]
],
axes: {
data2: 'y2'
},
types: {
data2: 'bar'
}
},
axis: {
y: {
label: {
text: 'Y Label',
position: 'outer-middle'
},
tick: {
format: d3.format("$,") // ADD
}
},
y2: {
show: true,
label: {
text: 'Y2 Label',
position: 'outer-middle'
}
}
}
});
%p Then, the chart will be like this:
#chart3_4
%br
%p More information about the options, please see Examples. (We'll add the reference soon)
%hr
%section
%h3
%a( href="#api" ) 4. Use APIs
%p By using APIs, you can update the chart after it's been rendered. We will introduce some of APIs here. APIs can be called through the object returned from generate().
%h4 1. Load Data
%p By using load() API, you can load data and update the chart dynamically as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
// var chart = c3.generate({ ... });
chart.load({
columns: [
['data1', 300, 100, 250, 150, 300, 150, 500],
['data2', 100, 200, 150, 50, 100, 250]
]
});
%p If you push the button "Load" below, this code will run and the chart will be updated.
%button.small( onclick="example4_1();" ) Load
#chart4_1
%br
%h4 2. Unload Data
%p By using unload() API, you can unload the data dynamically as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
// var chart = c3.generate({ ... });
chart.unload({
ids: ['data2', 'data3']
});
%p If you push the button "Unload" below, this code will run and the chart will be updated.
%button.small( onclick="example4_2();" ) Unload
#chart4_2
%br
%p Please use unload param in load() API if load and unload need to run simultaneously. Please see this example.
%h4 3. Show/Hide Data
%p By using show() and hide() API, you can show/hide the data dynamically as follows:
.sourcecode.highlight
%pre
%code.javascript
:preserve
// var chart = c3.generate({ ... });
chart.hide(['data2', 'data3']);
chart.show(['data2', 'data3']);
%p If you push the button "Show" and "Hide" below, this code will run and the chart will be updated.
%button.small( onclick="example4_3_2();" ) Hide
%button.small( onclick="example4_3_1();" ) Show
#chart4_3
%br
%p The documentation about APIs is poor now, so please check the issues on github. There might be some hints about what you want to do. (We will add the document soon)
%hr
%section
%h3
%a( href="#style" ) 5. Customize Style
%p C3 give some classes for each element when generating. So, you can change the style of the elements by using those classes.
%h4 1. Line style
%p The lines have c3-line-[id] class, so this class can be used to define the style in css as follows:
.sourcecode.highlight
%pre
%code.css
:preserve
#chart .c3-line-data2 {
stroke-width: 5px;
}
#chart5_1
%br
%p Please check the class for each element if you want to change the style. Web Inspector would be useful. (We will add the document for class definition soon)
%hr
%section
%h3
%a( href="#more" ) 6. And More..
%p Please check the examples and the issues on github for more information. Sorry for the poor documentation. We're working on now and please give me some time. Thank you.
= partial :footer
= partial :script
= partial :script_scroll
= javascript_include_tag 'gettingstarted.js'