# FlexChart 101 (Vue 2)

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

## Getting Started

Steps for getting started with FlexChart in Vue applications:

1. Add references to Vue, Wijmo, and Wijmo's Vue interop module.
2. Create a Vue object and give it a host element.
3. Add FlexChart controls to Vue's host element and bind them to the data.
4. Add some CSS to customize the chart's appearance.

<!DOCTYPE html> <html> <head> <!-- Vue --> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.0-rc.5/vue.js"></script> <!-- Wijmo --> <link href="http://cdn.wijmo.com/5.latest/styles/wijmo.min.css" rel="stylesheet"/> <script src="http://cdn.wijmo.com/5.latest/controls/wijmo.min.js"></script> <script src="http://cdn.wijmo.com/5.latest/controls/wijmo.chart.min.js"></script> <!-- Wijmo/Vue interop --> <script src="http://cdn.wijmo.com/5.latest/interop/vue/wijmo.vue2.min.js"></script> <!-- app scripts and styles --> <link href="styles/app.css" rel="stylesheet" /> <script src="scripts/app.js"></script> </head> <body> <div id="app"> <wj-flex-chart :items-source="data" binding-x="country"> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads"></wj-flex-chart-series> </wj-flex-chart> </div> </body> </html>
// Vue application var app = new Vue({ el: '#app', data: { data: getData(), }, }); // generate some random data function getData() { 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 }); } return data; }

#### Result (live):

## Chart Types

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

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

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

<wj-flex-chart :items-source="data" :chart-type="chartProps.chartType" :stacking="chartProps.stacking" :rotated="chartProps.rotated" binding-x="country"> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads"></wj-flex-chart-series> </wj-flex-chart> <dl class="dl-horizontal"> <dt>Chart Type</dt> <dd> <wj-combo-box :items-source="chartTypes" :text="chartProps.chartType" :text-changed="chartTypeChanged"> </wj-combo-box> </dd> <dt>Stacking</dt> <dd> <wj-combo-box :items-source="stackingOptions" :text="chartProps.stacking" :text-changed="stackingChanged"> </wj-combo-box> </dd> <dt>Rotated</dt> <dd> <input type="checkbox" v-model="chartProps.rotated" /> </dd> </dl>
// Vue application var app = new Vue({ el: '#app', data: { data: getData(), chartProps: { chartType: 'Column', stacking: 'None', rotated: false }, chartTypes: 'Column,Bar,Scatter,Line,LineSymbols,Area,Spline,SplineSymbols,SplineArea'.split(','), stackingOptions: 'None,Stacked,Stacked100pc'.split(',') }, methods: { // Wijmo event handlers chartTypeChanged: function(s, e) { this.chartProps.chartType = s.text; }, stackingChanged: function(s, e) { this.chartProps.stacking = s.text; }, } });

#### Result (live):

Chart Type
Stacking
Rotated

## 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:

<wj-flex-chart :items-source="data" chart-type="Column" binding-x="country"> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads" chart-type="LineSymbols"></wj-flex-chart-series> </wj-flex-chart>
// no code required

#### 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. Note that these are SVG elements, so you have to use CSS attributes such as "fill" instead of "color".

<wj-flex-chart :items-source="data" binding-x="country" :header="chartProps.header" :footer="chartProps.footer"> <wj-flex-chart-legend :position="chartProps.legendPosition"></wj-flex-chart-legend> <wj-flex-chart-axis wj-property="axisX" :title="chartProps.titleX"></wj-flex-chart-axis> <wj-flex-chart-axis wj-property="axisY" :title="chartProps.titleY"></wj-flex-chart-axis> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads"></wj-flex-chart-series> </wj-flex-chart> <dl class="dl-horizontal"> <dt>Header</dt> <dd><input v-model="chartProps.header" class="form-control"/></dd> <dt>Footer</dt> <dd><input v-model="chartProps.footer" class="form-control"/></dd> <dt>X-Axis Title</dt> <dd><input v-model="chartProps.titleX" class="form-control"/></dd> <dt>Y-Axis Title</dt> <dd><input v-model="chartProps.titleY" class="form-control"/></dd> <dt>Legend Position</dt> <dd> <wj-combo-box :items-source="legendPositions"> :text="chartProps.legendPosition" :text-changed="legendPositionChanged"> </wj-combo-box> </dd> </dl>
// Vue application var app = new Vue({ el: '#app', data: { data: getData(), chartProps: { chartType: 'Column', stacking: 'None', rotated: false, header: 'Sample Chart', footer: 'copyright (c) ComponentOne', titleX: 'country', titleY: 'amount', legendPosition: 'Right' }, chartTypes: 'Column,Bar,Scatter,Line,LineSymbols,Area,Spline,SplineSymbols,SplineArea'.split(','), stackingOptions: 'None,Stacked,Stacked100pc'.split(','), legendPositions: 'None,Left,Top,Right,Bottom'.split(',') } });

#### Result (live):

Header
Footer
X-Axis Title
Y-Axis Title
Legend Position

## 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:

- seriesName: The name of the series that contains the chart element.
- pointIndex: The index of the chart element within the series.
- x: The x value of the chart element.
- y: The y value of the chart element.

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.

<wj-flex-chart :items-source="data" tooltip-content="<img src='resources/{x}.png'/> <b>{seriesName}</b><br/>{y}" binding-x="country"> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads"></wj-flex-chart-series> </wj-flex-chart>
// no code required

#### 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 the symbolStyle properties to select style attributes for each series:

<wj-flex-chart :items-source="data" binding-x="country"> <wj-flex-chart-series name="Sales" binding="sales" :style="{ fill:'green', stroke:'darkgreen', 'stroke-width': '1' }"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses" :style="{ fill:'red', stroke:'darkred', 'stroke-width': '1' }"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads" chart-type="LineSymbols" :style="{ stroke:'orange', 'stroke-width': '5'}" :symbol-style="{ fill:'gold', stroke:'gold' }"></wj-flex-chart-series> </wj-flex-chart>
// no code required

#### 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.

<wj-flex-chart :items-source="data" binding-x="country"> <wj-flex-chart-axis wj-property="axisX" :axis-line="true" :major-grid="true"></wj-flex-chart-axis> <wj-flex-chart-axis wj-property="axisY" format="c0" :max="10000" :major-unit="2000" :axis-line="true" :major-grid="true"></wj-flex-chart-axis> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> </wj-flex-chart>
// no code required

#### 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 is contained in an element with the "wj-axis-x" class, which is contained in the the top-level control element that 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 have the "wj-flexchart"and the "custom-flex-chart" classes. Without this, the rule would apply to all charts on the page.

<wj-flex-chart class="custom-flex-chart" :items-source="data" binding-x="country"> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads"></wj-flex-chart-series> </wj-flex-chart>
/* 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 turned 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 the 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.

<wj-flex-chart :items-source="data" binding-x="country" tooltip-content="" :chart-type="chartProps.chartType" :selection-mode="chartProps.selectionMode" :selection-changed="selectionChanged"> <wj-flex-chart-series name="Sales" binding="sales"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads"></wj-flex-chart-series> </wj-flex-chart> <dl class="dl-horizontal"> <dt>Selection Mode</dt> <dd> <wj-combo-box :items-source="selectionModes" :text="chartProps.selectionMode" :text-changed="selectionModeChanged"> </wj-combo-box> </dd> <dt>Chart Type</dt> <dd> <wj-combo-box :items-source="chartTypes" :text="chartProps.chartType" :text-changed="chartTypeChanged"> </wj-combo-box> </dd> </dl> <div v-show="chartProps.selection != null"> <h4> Current Selection</h4> <p> Series: <b>{​{ chartProps.selection.name }}</b></p> <dl class="dl-horizontal" v-show="chartProps.selectionMode == 'Point' && chartProps.selection.collectionView && chartProps.selection.collectionView.currentItem != null" > <dt>Country</dt> <dd>{​{ chartProps.selection.collectionView.currentItem.country }}</dd> <dt>Sales</dt> <dd>{​{ chartProps.selection.collectionView.currentItem.sales | wj-format 'c' }}</dd> <dt>Expenses</dt> <dd>{​{ chartProps.selection.collectionView.currentItem.expenses | wj-format 'c' }}</dd> <dt>Downloads</dt> <dd>{​{ chartProps.selection.collectionView.currentItem.downloads | wj-format 'n0' }}</dd> </dl> </div>
// Vue application var app = new Vue({ el: '#app', data: { data: getData(), chartProps: { chartType: 'Column', selectionMode: 'None', selection: null }, chartTypes: 'Column,Bar,Scatter,Line,LineSymbols,Area,Spline,SplineSymbols,SplineArea'.split(','), selectionModes: 'None,Series,Point'.split(',') }, methods: { // Wijmo event handlers chartTypeChanged: function(s, e) { this.chartProps.chartType = s.text; }, selectionModeChanged: function (s, e) { this.chartProps.selectionMode = s.text; }, selectionChanged: function (s, e) { this.chartProps.selection = s.selection; } } });

#### Result (live):

Selection Mode
Chart Type

#### Current Selection

Series: {{ chartProps.selection.name }}

Country
  {{ chartProps.selection.collectionView.currentItem.country }}
Sales
  {{ chartProps.selection.collectionView.currentItem.sales | wj-format('c') }}
Expenses
  {{ chartProps.selection.collectionView.currentItem.expenses | wj-format('c') }}
Downloads
  {{ chartProps.selection.collectionView.currentItem.downloads | wj-format('n0') }}

## 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 in the following two ways:

1. Clicking the legend entries:
   The chart directive sets the chart's legendToggle property to true, which toggles the visibility property of a series when its legend entry is clicked.
2. Using checkboxes:
   The page uses Vue directives to bind input controls directly to the visibility property of each series.

<wj-flex-chart :items-source="data" :legend-toggle="true" control="toggleChart"> <wj-flex-chart-series name="Sales" binding="sales" :visibility="getSeriesVisibility('sales')"></wj-flex-chart-series> <wj-flex-chart-series name="Expenses" binding="expenses" :visibility="getSeriesVisibility('expenses')"></wj-flex-chart-series> <wj-flex-chart-series name="Downloads" binding="downloads" :visibility="getSeriesVisibility('downloads')"></wj-flex-chart-series> </wj-flex-chart> <!-- toggle series with checkboxes --> <label> Sales <input type="checkbox" value="sales" v-model="chartProps.seriesVisible"/> </label><br /> <label> Expenses <input type="checkbox" value="expenses" v-model="chartProps.seriesVisible"/> </label><br /> <label> Downloads <input type="checkbox" value="downloads" v-model="chartProps.seriesVisible"> </label>
// Vue application var app = new Vue({ el: '#app', data: { data: getData(), chartProps: { chartType: 'Column', seriesVisible: 'sales,expenses,downloads'.split(',') }, }, methods: { // get series visibility from 'seriesVisible' array getSeriesVisibility: function (name) { return this.chartProps.seriesVisible.indexOf(name) > -1 ? 'Visible' : 'Hidden'; } } });

#### 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 the new data arrives.

<wj-flex-chart :items-source="trafficData" chart-type="Area" stacking="Stacked" binding-x="time"> <wj-flex-chart-axis wj-property="axisX" format="mm:ss"></wj-flex-chart-axis> <wj-flex-chart-series name="Trucks" binding="trucks"></wj-flex-chart-series> <wj-flex-chart-series name="Ships" binding="ships"></wj-flex-chart-series> <wj-flex-chart-series name="Planes" binding="planes"></wj-flex-chart-series> </wj-flex-chart> <dl class="dl-horizontal"> <dt>Update Speed</dt> <dd> <div class="btn-group"> <button type="button" class="btn btn-default" @click="setInterval(500)">Slow</button> <button type="button" class="btn btn-default" @click="setInterval(250)">Medium</button> <button type="button" class="btn btn-default" @click="setInterval(50)">Fast</button> <button type="button" class="btn btn-default" @click="setInterval(0)">Stop</button> </div> </dd> </dl>
// Vue application var app = new Vue({ el: '#app', data: { trafficData: getTrafficData() }, methods: { setInterval: function (interval) { setTrafficInterval(interval) } } }); // generate some dynamic data var trafficInterval, trafficData = new wijmo.collections.CollectionView(); function getTrafficData() { return trafficData; } function setTrafficInterval(value) { clearInterval(trafficInterval); if (value) { trafficInterval = setInterval(addTrafficItem, value); } } function addTrafficItem() { var arr = trafficData.sourceCollection; arr.push(...); if (arr.length > 200) { arr.splice(0, 1); } }

#### Result (live):

Update Speed