Charts
Requirements
Cyclops uses Chartist.js for displaying simple, responsive charts. To display a chart, include the Chartist script file.
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
Then use either the cyclops jQuery function or the knockout binding to create your chart. The knockout binding uses the jQuery function under the covers and thus shares all the same options, chart API documentation.
// jQuery $('.myChart').chart({type: 'Line', data: data})
<!-- knockout binding --> <div class="myChart" data-bind="chart:{ type: 'Line', data: data }"></div>
Interactive
In addition to displaying data in a chart, the loading, failed, and empty states should be accounted for. The interactive example below demonstrates an artifically long loading time, with options to render a failed or empty template.
Line Chart
var lineData = { labels: ["January", "February", "March", "April", "May", "June", "July"], series: [ { name: "Widget", value: [75, 76, 77, 78, 81, 79, 76] }, {...} ] }; ko.applyBindings({ data: lineData }, $("#lineChart")[0]);
<div id="lineChart" data-bind="chart:{ type: 'Line', data: data }"></div>
Bar Chart
var barData = { labels: ["January", "February", "March", "April", "May", "June", "July"], series: [{ name: "My First dataset", value: [65, 59, 80, 81, 56, 55, 40] }, { name: "My Second dataset", value: [28, 48, 40, 19, 86, 27, 90] }] }; ko.applyBindings({ data: barData }, $("#barChart")[0]);
<div id="lineChart" data-bind="chart:{ type: 'Bar', data: data }"></div>
Pie & Donut Chart
var pieData = { series: [ { value: 300, name: "set 1" }, {...} ] } ko.applyBindings({ data: pieData }, $("#pieChart")[0]); ko.applyBindings({ data: pieData }, $("#donutChart")[0]);
<div id="pieChart" data-bind="chart:{ type: 'Pie', data: data }"></div> <div id="donutChart" data-bind="chart:{ type: 'Donut', data: data }"></div>
Aspect Ratio
In order for a chart element to scale properly and be responsive, apply an aspect ratio (e.g. 4:3, 3:2, 16:9 etc.) rather than specifying a fixed width and height. Specify the ratio directly on the container without the need to calculate any fixed dimensions. In order to create a chart that is using the aspect ratio of a golden section you can add the .ct-golden-section
to the container where chart is initialized.
class name | ratio |
---|---|
.ct-square | 1 |
.ct-minor-second | 15:16 |
.ct-major-second | 8:9 |
.ct-minor-third | 5:6 |
.ct-major-third | 4:5 |
.ct-perfect-fourth | 3:4 |
.ct-perfect-fifth | 2:3 |
.ct-minor-sixth | 5:8 |
.ct-golden-section | 1:1.618 |
.ct-major-sixth | 3:5 |
.ct-minor-seventh | 9:16 |
.ct-major-seventh | 8:15 |
.ct-octave | 1:2 |
.ct-major-tenth | 2:5 |
.ct-major-eleventh | 3:8 |
.ct-major-twelfth | 1:3 |
.ct-double-octave | 1:4 |
Color Palette
Chart series default color palette is made up of 10 unique colors. If your chart happens to contain more than 10 series, the color series will loop (e.g. the 1st and 11th series will be the same color).