Wijmo 5

FlexPie 101

这个页面展示了如何开始使用Wijmo FlexPie控件。

入门

在KnockoutJS应用中开始使用FlexPie的步骤:

  1. 添加KnockoutJS, Wijmo和Wijmo的KnockoutJS绑定添加引用。
  2. 添加一个视图模型提供 数据和逻辑。
  3. 向当前页面添加一个FlexPie控件,并且将它与数据绑定。
  4. (可选)添加一些CSS来个性化输入控件的外观。
<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):

基本功能

FlexPie控件有5个基本属性,允许你自定义它的布局和外观:

下面这个样例可以让你观察到这些属性更改后的情况。而且,单击一个饼图切块会为数据点显示一个工具提示。

<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: innerRadius, min: 0, max: 1, step: .1, format: 'n' }" /> </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: offset, min: 0, max: 1, step: .1, format: 'n' }" /> </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: startAngle, min: -360, max: 360, step: 45 }"/> </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: 'minimal' }">Minimal</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> </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.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 Minimal Modern Organic Slate

图例和标题

legend属性用来自定义图表图例的外观。headerfooter属性用来添加FlexPie控件的标题。

下面的实例允许你实时更改FlexPie的legend.position, headerfooter属性。

<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

选择

FlexPie控件允许你通过单击或者触摸一个饼图切块来选择数据点。 使用selectionMode属性来指定是否允许对数据点的选择或者禁用选择(默认)。

设置selctionMode属性为Point会导致用户在单击饼图切块时,FlexPie自动更新selection属性, 并且将"wj-state-selected"类应用到选中元素中。设置FlexPie的selectionMode属性为SeriesNone会禁用控件内的选择。

FlexPie提供三种额外的属性来自定义选择:

<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

主题

FlexPie控件的外观很大程度上是在CSS中定义。除了默认的主题, 我们包括了一些专业设计的主题,它们自定义了Wijmo控件的外观来达到一个一致的,具有吸引力的效果。

你可以使用CSS自定义FlexPie的外观。要做到这一点,从默认主题复制CSS规则到一个新的CSS文件并且修改需要的属性。

在这个样例中,我们向控件添加了一个"custom-pie-chart"CSS类并且定义了一些CSS规则来改变填充,字体集和页眉,页尾,图例的字体粗细。

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