Wijmo

FlexPie 101

This page shows how to get started with Wijmo's FlexPie control.

Getting Started

Steps for getting started with the FlexPie control in KnockoutJS applications:

  1. Add references to KnockoutJS, Wijmo, and Wijmo's KnockoutJS bindings.
  2. Add a view model to provide data and logic.
  3. Add a Wijmo FlexPie control to the page and bind it to your data.
  4. (Optional) Add some CSS to customize the input control's appearance.
<html> <head> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> <link rel="stylesheet" type="text/css" href="css/wijmo.css" /> <link rel="stylesheet" href="styles/app.css" /> <script src="scripts/knockout.js" type="text/javascript"></script> <script src="scripts/wijmo.js" type="text/javascript"></script> <script src="scripts/wijmo.input.js" type="text/javascript"></script> <script src="scripts/wijmo.chart.js" type="text/javascript"></script> <script src="scripts/wijmo.knockout.js" type="text/javascript"></script> <script src="scripts/bindings/appBindings.js"></script> <script src="scripts/app.js"></script> <script src="scripts/viewmodels/appViewModel.js"></script> </head> <body> <!-- this is the chart --> <div data-bind="wjFlexPie: { itemsSource: itemsSource, binding: 'value', bindingName: 'name' }"> </div> </body> </html>
function SimpleVM() { var names = ['Oranges', 'Apples', 'Pears', 'Bananas', 'Pineapples'], data = []; // populate itemsSource for (var i = 0; i < names.length; i++) { data.push({ name: names[i], value: Math.round(Math.random() * 100) }); } this.itemsSource = data; } (function() { ko.applyBindings(new SimpleVM()); })();

Result (live):

Basic Features

The FlexPie control has five basic properties that allow you to customize its layout and appearance:

The example below allows you to see what happens when you change these properties. Also, clicking on a pie slice will display a tooltip for the data point.

<div data-bind="wjFlexPie: { control: chart, itemsSource: itemsSource, binding: 'value', bindingName: 'name', innerRadius: innerRadius, offset: offset, startAngle: startAngle, reversed: reversed}"> </div> <div class="form-horizontal"> <div class="form-group"> <label class="col-md-3 control-label">Inner Radius</label> <div class="col-md-9"> <input data-bind="wjInputNumber: { value: 0, min: 0, max: 1, step: .1, format: 'n', valueChanged: innerRadiusChanged }" /> </div> </div> <div class="form-group"> <label class="col-md-3 control-label">Offset</label> <div class="col-md-9"> <input data-bind="wjInputNumber: { value: 0, min: 0, max: 1, step: .1, format: 'n', valueChanged: offsetChanged }" /> </div> </div> <div class="form-group"> <label class="col-md-3 control-label">Start Angle</label> <div class="col-md-9"> <input data-bind="wjInputNumber: { value: 0, min: -360, max: 360, step: 45, valueChanged: startAngleChanged }"/> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div data-bind="wjMenu: { value: palette, header: 'Palette', itemClicked: paletteChanged }"> <span data-bind="wjMenuItem: { value: 'standard' }">Standard</span> <span data-bind="wjMenuItem: { value: 'cocoa' }">Cocoa</span> <span data-bind="wjMenuItem: { value: 'coral' }">Coral</span> <span data-bind="wjMenuItem: { value: 'dark' }">Dark</span> <span data-bind="wjMenuItem: { value: 'highcontrast' }">High Contrast</span> <span data-bind="wjMenuItem: { value: 'light' }">Light</span> <span data-bind="wjMenuItem: { value: 'midnight' }">Midnight</span> <span data-bind="wjMenuItem: { value: 'modern' }">Modern</span> <span data-bind="wjMenuItem: { value: 'organic' }">Organic</span> <span data-bind="wjMenuItem: { value: 'slate' }">Slate</span> <span data-bind="wjMenuItem: { value: 'zen' }">Zen</span> <span data-bind="wjMenuItem: { value: 'cyborg' }">Cyborg</span> <span data-bind="wjMenuItem: { value: 'superhero' }">Superhero</span> <span data-bind="wjMenuItem: { value: 'flatly' }">Flatly</span> <span data-bind="wjMenuItem: { value: 'darkly' }">Darkly</span> <span data-bind="wjMenuItem: { value: 'cerulan' }">Cerulan</span> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div class="checkbox"> <label> <input type="checkbox" data-bind="checked: reversed"> Reversed? </label> </div> </div> </div> </div>
var self = this; // get a reference to the FlexPie control this.chart = ko.observable(undefined); this.itemsSource = data; this.innerRadius = ko.observable(0); this.offset = ko.observable(0); this.startAngle = ko.observable(0); this.reversed = ko.observable(false); this.palette = ko.observable('standard'); this.innerRadiusChanged = function (data, sender) { if (sender.value < sender.min || sender.value > sender.max) { return; } self.innerRadius(sender.value); } this.offsetChanged = function (data, sender) { if (sender.value < sender.min || sender.value > sender.max) { return; } self.offset(sender.value); } this.startAngleChanged = function (data, sender) { if (sender.value < sender.min || sender.value > sender.max) { return; } self.startAngle(sender.value); } this.paletteChanged = function (data, sender, args) { // update FlexPie control's palette self.chart().palette = wijmo.chart.Palettes[sender.selectedValue]; }

Result (live):

Standard Cocoa Coral Dark High Contrast Light Midnight Modern Organic Slate Zen Cyborg Superhero Flatly Darkly Cerulan

Legend & Titles

The legend properties can be used to customize the appearance of the chart's legend. The header and footer properties can be used to add titles to the FlexPie control as well.

The following example allows you to change the FlexPie's legend.position, header, and footer properties in real-time.

<div data-bind="wjFlexPie: { itemsSource: itemsSource, binding: 'value', bindingName: 'name', header: header, footer: footer }"> <div data-bind="wjFlexChartLegend: { position: legendPosition }"></div> </div> <div class="form-horizontal"> <div class="form-group"> <label class="col-md-3 control-label">Header</label> <div class="col-md-9"> <input type="text" class="form-control" data-bind="value: header, valueUpdate: 'input'" placeholder="Specify the FlexPie's header" /> </div> </div> <div class="form-group"> <label class="col-md-3 control-label">Footer</label> <div class="col-md-9"> <input type="text" class="form-control" data-bind="value: footer, valueUpdate: 'input'" placeholder="Specify the FlexPie's footer" /> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div data-bind="wjMenu: { value: legendPosition, header: 'Legend Position' }"> <span data-bind="wjMenuItem: { value: 'None' }">None</span> <span data-bind="wjMenuItem: { value: 'Left' }">Left</span> <span data-bind="wjMenuItem: { value: 'Top' }">Top</span> <span data-bind="wjMenuItem: { value: 'Right' }">Right</span> <span data-bind="wjMenuItem: { value: 'Bottom' }">Bottom</span> </div> </div> </div> </div>
this.itemsSource = data; this.header = ko.observable('Fruit by Value'); this.footer = ko.observable('2014 GrapeCity, inc.'); this.legendPosition = ko.observable('Right');

Result (live):

None Left Top Right Bottom

Selection

The FlexPie control allows you to select data points by clicking or touching a pie slice. Use the selectionMode property to specify whether you want to allow selection by data point or no selection at all (default).

Setting the selctionMode property to Point causes the FlexPie to update the selection property when the user clicks on a pie slice, and to apply the "wj-state-selected" class to the selected element. Setting the FlexPie's selectionMode property to Series or None will disable selections within the control.

The FlexPie offers three additional properties to customize the selection:

<div data-bind="wjFlexPie: { itemsSource: itemsSource, binding: 'value', bindingName: 'name', selectionMode: 'Point', selectedItemPosition: selectedPosition, selectedItemOffset: selectedOffset, isAnimated: isAnimated }"> </div> <div class="form-horizontal"> <div class="form-group"> <label class="col-md-3 control-label">Selected Item Offset</label> <div class="col-md-9"> <input data-bind="wjInputNumber: { value: selectedOffset, min: 0, max: .5, step: .1, format: 'n' }"/> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div data-bind="wjMenu: { header: 'Selected Item Position', value: selectedPosition}"> <span data-bind="wjMenuItem: { value: 'None' }">None</span> <span data-bind="wjMenuItem: { value: 'Left' }">Left</span> <span data-bind="wjMenuItem: { value: 'Top' }">Top</span> <span data-bind="wjMenuItem: { value: 'Right' }">Right</span> <span data-bind="wjMenuItem: { value: 'Bottom' }">Bottom</span> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div class="checkbox"> <label> <input type="checkbox" data-bind="checked: isAnimated"> Is Animated? </label> </div> </div> </div> </div>
this.itemsSource = data; this.selectedPosition = ko.observable('Top'); this.selectedOffset = ko.observable(0); this.isAnimated = ko.observable(true);

Result (live):

None Left Top Right Bottom

Theming

The appearance of the FlexPie control is largely defined in CSS. In addition to the default theme, we include several professionally designed themes that customize the appearance of all Wijmo controls to achieve a consistent, attractive look.

You can customize the appearance of the FlexPie control using CSS. To do this, copy the CSS rules from the default theme to a new CSS file and modify the properties as needed.

In this example, we added the "custom-pie-chart" CSS class to the control and defined some CSS rules to change the fill, font family, and font weight of the header, footer, and legend.

<div class="custom-pie-chart" data-bind="wjFlexPie: { itemsSource: itemsSource, binding: 'value', bindingName: 'name', header: 'Header', footer: 'Footer' }"> </div>
this.itemsSource = data;
.custom-pie-chart.wj-flexchart .wj-header .wj-title, .custom-pie-chart.wj-flexchart .wj-footer .wj-title, .custom-pie-chart.wj-flexchart .wj-legend > .wj-label { fill: #666; font-family: 'Courier New', Courier, monospace; font-weight: bold; }

Result (live):