FlexChart 101

Getting Started

Steps for getting started with the FlexChart 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 FlexChart 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/appVM.js"></script> </head> <body> <!-- this is the chart --> <div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country' }"> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"></div> </div> </body> </html>
// create and apply application view model function viewModel1() { // generate some random data var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','), data = []; for (var i = 0; i < countries.length; i++) { data.push({ country: countries[i], downloads: Math.round(Math.random() * 20000), sales: Math.random() * 10000, expenses: Math.random() * 5000 }); } // add data array to scope this.data = data; }; (function () { ko.applyBindings(new viewModel1()); })();
/* set default chart style */ .wj-flexchart { height: 400px; background-color: white; box-shadow: 4px 4px 10px 0px rgba(50, 50, 50, 0.75); padding: 8px; margin-bottom: 12px; }

Result (live):

Chart Types

The FlexChart control has three properties that allow you to customize the chart type:

  1. chartType: Selects the default chart type to be used for all series. Individual series may override this.
  2. stacking: Determines whether series are plotted independently, stacked, or stacked so their sum is 100%.
  3. rotated: Flips the X and Y axes so X becomes vertical and Y horizontal.

The example below allows you to see what happens when you change these properties:

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country', chartType: chartProps.chartType, stacking: chartProps.stacking, rotated: chartProps.rotated}"> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"></div> </div> <div data-bind="wjMenu: { value: chartProps.chartType, header: 'Chart Type' }"> <span data-bind="wjMenuItem: { value: 'Column' }">Column</span> <span data-bind="wjMenuItem: { value: 'Bar' }">Bar</span> <span data-bind="wjMenuItem: { value: 'Scatter' }">Scatter</span> <span data-bind="wjMenuItem: { value: 'Line' }">Line</span> <span data-bind="wjMenuItem: { value: 'LineSymbols' }">LineSymbols</span> <span data-bind="wjMenuItem: { value: 'Area' }">Area</span> <span data-bind="wjMenuItem: { value: 'Spline' }">Spline</span> <span data-bind="wjMenuItem: { value: 'SplineSymbols' }">SplineSymbols</span> <span data-bind="wjMenuItem: { value: 'SplineArea' }">SplineArea</span> </div> <div data-bind="wjMenu: { value: chartProps.stacking, header: 'Stacking' }"> <span data-bind="wjMenuItem: { value: 'None' }">None</span> <span data-bind="wjMenuItem: { value: 'Stacked' }">Stacked</span> <span data-bind="wjMenuItem: { value: 'Stacked100pc' }">Stacked 100%</span> </div> <div data-bind="wjMenu: { value: chartProps.rotated, header: 'Rotated' }"> <span data-bind="wjMenuItem: { value: false }">False</span> <span data-bind="wjMenuItem: { value: true }">True</span> </div>
// add chart properties to view model this.chartProps = { chartType: ko.observable('Column'), stacking: ko.observable('None'), rotated: ko.observable(false), ............ };

Result (live):

Column Bar Scatter Line LineSymbols Area Spline SplineSymbols SplineArea
None Stacked Stacked 100%
False True

Mixed Chart Types

You can use different chart types for each chart series by setting the chartType property on the series itself. This overrides the chart's default chart type.

In the example below, the chart's chartType property is set to Column, but the Downloads series overrides that to use the LineAndSymbol chart type:

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country', chartType: 'Column' }"> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads', chartType: 'LineSymbols' }"></div> </div>

Result (live):

Legend and Titles

Use the legend properties to customize the appearance of the chart legend, and the header, footer, and axis title properties to add titles to your charts.

You can style the legend and titles using CSS. The CSS tab below shows the rules used to customize the appearance of the legend and titles. Notice that these are SVG elements, so you have to use CSS attributes such as "fill" instead of "color."

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country', header: chartProps.header, footer: chartProps.footer }"> <div data-bind="wjFlexChartLegend : { position: chartProps.legendPosition }"></div> <div data-bind="wjFlexChartAxis: { wjProperty: 'axisX', title: chartProps.titleX }"></div> <div data-bind="wjFlexChartAxis: { wjProperty: 'axisY', title: chartProps.titleY }"></div> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"></div> </div> <dl class="dl-horizontal"> <dt>Header</dt><dd><input data-bind="value: chartProps.header, valueUpdate: 'input'" class="form-control"/></dd> <dt>Footer</dt><dd><input data-bind="value: chartProps.footer, valueUpdate: 'input'" class="form-control"/></dd> <dt>X-Axis Title</dt><dd><input data-bind="value: chartProps.titleX, valueUpdate: 'input'" class="form-control"/></dd> <dt>Y-Axis Title</dt><dd><input data-bind="value: chartProps.titleY, valueUpdate: 'input'" class="form-control"/></dd> <dt></dt> <dd> <div data-bind="wjMenu: { value: chartProps.legendPosition, header: 'Legend' }"> <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> </dd> </dl>
this.chartProps = { chartType: ko.observable('Column'), stacking: ko.observable('None'), legendPosition: ko.observable('Right'), rotated: ko.observable(false), header: ko.observable('Sample Chart'), footer: ko.observable('copyright (c) ComponentOne'), titleX: ko.observable('country'), titleY: ko.observable('amount'), };
.wj-flexchart .wj-title { font-weight: bold; } .wj-flexchart .wj-header .wj-title { font-size: 18pt; fill: #80044d; } .wj-flexchart .wj-footer .wj-title { fill: #80044d; } .wj-flexchart .wj-axis-x .wj-title, .wj-flexchart .wj-axis-y .wj-title { font-style: italic; }

Result (live):

Header
Footer
X-Axis Title
Y-Axis Title
None Left Top Right Bottom

Tooltips

The FlexChart has built-in support for tooltips. By default, the control displays tooltips when the user touches or hovers the mouse on a data point.

The tooltip content is generated using a template that may contain the following parameters:

By default, the tooltip template is set to <b>{seriesName}</b><br/>{x} {y}, and you can see how that works in the charts above. In this example, we set the tooltip template to <b>{seriesName}</b> <img src='resources/{x}.png'/><br/>{y}, which replaces the country name with the country's flag.

You can disable the chart tooltips by setting the template to an empty string.

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country', tooltipContent: '<img src="resources/{x}.png"/> <b>{seriesName}</b><br/>{y}' }"> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"></div> </div>

Result (live):

Styling Series

The FlexChart automatically picks colors for each series based on a default palette, which you can override by setting the palette property. But you can also override the default settings by setting the style property of any series to an object that specifies SVG styling attributes, including fill, stroke, strokeThickness, and so on.

The Series.style property is an exception to the general rule that all styling in Wijmo is done through CSS. The exception reflects the fact that many charts have dynamic series, which would be impossible to style in advance. For example, a stock chart may show series selected by the user while running the application.

The chart in this example uses the style and symbolStyle properties to select style attributes for each series:

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country' }"> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales', style: {fill:'green', stroke:'darkgreen', 'stroke-width': '1'} }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses', style: {fill:'red', stroke:'darkred', 'stroke-width': '1'} }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' , chartType: 'LineSymbols', style: { stroke:'orange', 'stroke-width': '5'}, symbolStyle: {fill:'gold', stroke:'gold' } }"></div> </div>

Result (live):

Customizing Axes

Use axis properties to customize the chart's axes, including ranges (minimum and maximum), label format, tickmark spacing, and gridlines.

The Axis class has boolean properties that allow you to turn features on or off (axisLine, labels, majorTickMarks, and majorGrid.) You can style the appearance of the features that are turned on using CSS.

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country' }"> <div data-bind="wjFlexChartAxis: { wjProperty: 'axisX', axisLine: true, majorGrid: true }"></div> <div data-bind="wjFlexChartAxis: { wjProperty: 'axisY', format: 'c0', max: 10000, majorUnit: 2000, axisLine: true, majorGrid: true }"></div> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> </div>

Result (live):

Theming

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

To customize the appearance of the chart, inspect the elements you want to style and create some CSS rules that apply to those elements.

For example, if you right-click one of the labels on the X axis in IE or Chrome, you will see that it is an element with the "wj-label" class, that it is contained in an element with the "wj-axis-x" class, which is contained in the the top-level control element, which has the "wj-flexchart" class. The first CSS rule in this example uses this information to customize the X labels. The rule selector adds the additional requirement that the parent element must be have the "wj-flexchart" and the "custom-flex-chart" classes. Without this, the rule would apply to all charts on the page.

<div class="custom-flex-chart" data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country' }"> <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"></div> </div>
/* custom chart theme */ .custom-flex-chart.wj-flexchart .wj-axis-x .wj-label { font-family: Courier New, Courier, monospace; font-weight: bold; } .custom-flex-chart.wj-flexchart .wj-legend .wj-label { font-family: Courier New, Courier, monospace; font-weight: bold; } .custom-flex-chart.wj-flexchart .wj-legend > rect { fill: #f8f8f8; stroke: #c0c0c0; } .custom-flex-chart.wj-flexchart .wj-plot-area > rect { fill: #f8f8f8; stroke: #c0c0c0; }

Result (live):

Selection Modes

The FlexChart allows you to select series or data points by clicking or touching them. Use the selectionMode property to specify whether you want to allow selection by series, by data point, or no selection at all (selection is off by default.)

Setting the selectionMode property to Series or Point causes the FlexChart to update the Selection property when the user clicks the mouse, and to apply the "wj-state-selected" class to selected chart elements.

The Selection property returns the currently selected series. To get the currently selected data point, get the currently selected item within the selected series using the Series.collectionView.currentItem property as shown in the example.

<div data-bind="wjFlexChart: { itemsSource: data, bindingX: 'country', tooltipContent: '', chartType: chartProps.chartType, selectionMode: chartProps.selectionMode, selection: chartProps.selection, selectionChanged: selectionChangedEH }"< <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales' }"<</div< <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses' }"<</div< <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads' }"<</div< </div< <div data-bind="wjMenu: { value: chartProps.selectionMode, header: 'Selection Mode' }"< <span data-bind="wjMenuItem: { value: 'None' }"<None</span< <span data-bind="wjMenuItem: { value: 'Series' }"<Series</span< <span data-bind="wjMenuItem: { value: 'Point' }"<Point</span< </div< <div data-bind="wjMenu: { value: chartProps.chartType, header: 'Chart Type' }"< <span data-bind="wjMenuItem: { value: 'Column' }"<Column</span< <span data-bind="wjMenuItem: { value: 'Bar' }"<Bar</span< <span data-bind="wjMenuItem: { value: 'Scatter' }"<Scatter</span< <span data-bind="wjMenuItem: { value: 'Line' }"<Line</span< <span data-bind="wjMenuItem: { value: 'LineSymbols' }"<LineSymbols</span< <span data-bind="wjMenuItem: { value: 'Area' }"<Area</span< <span data-bind="wjMenuItem: { value: 'Spline' }"<Spline</span< <span data-bind="wjMenuItem: { value: 'SplineSymbols' }"<SplineSymbols</span< <span data-bind="wjMenuItem: { value: 'SplineArea' }"<SplineArea</span< </div< <div data-bind="if: chartProps.selectionMode() != 'None' && chartProps.selection()"< <h4< Current Selection</h4< <p< Series: <b<<span data-bind="text: chartProps.selection().name"<</span<</b<</p< <div data-bind="ifnot: chartProps.selectionMode() != 'Point' || chartProps.selectionPoint() == null"< <dl class="dl-horizontal" data-bind="with: chartProps.selectionPoint"< <dt<Country</dt<<dd data-bind="text: country" <</dd< <dt<Sales</dt<<dd data-bind="text: $parent.format(sales, 'n2')"<</dd< <dt<Expenses</dt<<dd data-bind="text: $parent.format(expenses, 'n2')"<</dd< <dt<Downloads</dt<<dd data-bind="text: $parent.format(downloads, 'n0')"<</dd< </dl< </div< </div<
this.chartProps = { chartType: ko.observable('Column'), stacking: ko.observable('None'), legendPosition: ko.observable('Right'), rotated: ko.observable(false), header: ko.observable('Sample Chart'), footer: ko.observable('copyright (c) ComponentOne'), titleX: ko.observable('country'), titleY: ko.observable('amount'), selectionMode: ko.observable('Series'), selection: ko.observable(null), selectionPoint: ko.observable() }; // update the selectionPoint observable on selection change this.selectionChangedEH = function (data, sender, args) { var curSel = sender.selection; self.chartProps.selectionPoint(curSel && curSel.collectionView.currentItem); }

Result (live):

None Series Point
Column Bar Scatter Line LineSymbols Area Spline SplineSymbols SplineArea

Current Selection

Series:

Country
Sales
Expenses
Downloads

Toggle Series

The Series class has a visibility property that allows you to determine whether a series should be shown in the chart and in the legend, only in the legend, or completely hidden.

This sample shows how you can use the visibility property to toggle the visibility of a series using two methods:

  1. Clicking on legend entries:
    The chart binding sets the chart's legendToggle property to true, which toggles the visibility property of a series and an observable bound to it when its legend entry is clicked.
  2. Using checkboxes:
    The page uses Knockout writable computed observables to bind input controls to observables bound to the visibility property of each series and to convert values between boolean and SeriesVisibility values.
<div data-bind="wjFlexChart: { itemsSource: data, legendToggle: true }"< <div data-bind="wjFlexChartSeries: { name: 'Sales', binding: 'sales', visibility: visibility1 }"<</div< <div data-bind="wjFlexChartSeries: { name: 'Expenses', binding: 'expenses', visibility: visibility2 }"<</div< <div data-bind="wjFlexChartSeries: { name: 'Downloads', binding: 'downloads', visibility: visibility3 }"<</div< </div< <!-- toggle series with checkboxes --< Sales <input type="checkbox" data-bind="checked: ko.computed(null, visibility1, visibilityToBool)"/<<br /< Expenses <input type="checkbox" data-bind="checked: ko.computed(null, visibility2, visibilityToBool)"/<<br /< Downloads <input type="checkbox" data-bind="checked: ko.computed(null, visibility3, visibilityToBool)"/<<br /<
this.visibility1 = ko.observable(); this.visibility2 = ko.observable(); this.visibility3 = ko.observable(); // SeriesVisibility-to-boolean writable computed observable. 'this' references a source SeriesVisibility observable. this.visibilityToBool = { read: function () { var vis = this(); return vis === wijmo.chart.SeriesVisibility.Visible || vis === wijmo.chart.SeriesVisibility.Plot; }, write: function (value) { this(value ? wijmo.chart.SeriesVisibility.Visible : wijmo.chart.SeriesVisibility.Legend); } }

Result (live):

Sales
Expenses
Downloads

Dynamic Charts

The FlexChart uses an ICollectionView internally, so any changes you make to the data source are automatically reflected in the chart.

In this sample, we use a timer to add items to the data source, discarding old items to keep the total count at 200. The result is a dynamic chart that scrolls as new data arrives.

<div data-bind="wjFlexChart: { itemsSource: trafficData, chartType: 'Area', stacking: 'Stacked', bindingX: 'time' }"> <div data-bind="wjFlexChartAxis: { wjProperty: 'axisX', format: 'mm:ss' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Trucks', binding: 'trucks' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Ships', binding: 'ships' }"></div> <div data-bind="wjFlexChartSeries: { name: 'Planes', binding: 'planes' }"></div> </div> <dl class="dl-horizontal"> <dt>Update Speed</dt> <dd> <div class="btn-group"> <button type="button" class="btn btn-default" data-bind="click: setInterval.bind($data, 200)">Slow</button> <button type="button" class="btn btn-default" data-bind="click: setInterval.bind($data, 100)">Medium</button> <button type="button" class="btn btn-default" data-bind="click: setInterval.bind($data, 50)">Fast</button> <button type="button" class="btn btn-default" data-bind="click: setInterval.bind($data, 0)">Stop</button> </div> </dd> </dl>
var toAddData; this.trafficData = new wijmo.collections.ObservableArray(); this.setInterval = function (interval) { if (toAddData) { clearTimeout(toAddData); toAddData = null; } self.interval = interval; if (interval) { toAddData = setTimeout(addTrafficItem); } }; this.setInterval(500); function addTrafficItem() { // add random data, limit array length ... // keep adding ... }

Result (live):

Update Speed