FlexChart分析

这个页面演示如何使用Wijmo的FlexChart控件的函数系列和趋势线。 这些功能包含在wijmo.chart.analytics module模块中。

入门

在JavaScript应用中开始使用FlexChart分析的步骤:

  1. 添加对Wijmo的引用。
  2. 添加标记作为FlexChart的宿主。
  3. 通过JavaScript初始化FlexChart和它的itemSource属性。
  4. 创建一个或多个数据系列,并将它们添加到FlexChart的系列集合。(可选)
  5. 创建一个或多个趋势线或函数系列,并将它们添加到FlexChart的系列集合。
  6. 添加一些CSS来自定义图表的外观(可选)。
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> <link rel="stylesheet" type="text/css" href="css/wijmo.css" /> <link href="css/app.css" rel="stylesheet" type="text/css" /> <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.chart.analytics.js" type="text/javascript"> </script> <script src="scripts/app.js" type="text/javascript"></script> </head> <body> <!-- this is the FlexChart --> <div id="gettingStartChart"></div> </body> </html>
// create FlexChart var gettingStartedChart = new wijmo.chart.FlexChart('#gettingStartedChart'); // initialize FlexChart's properties gettingStartedChart.initialize({ itemsSource: appData, bindingX: 'x', series: [{ name: 'Origin', binding: 'y', chartType: wijmo.chart.ChartType.Scatter }] }); //create TrendLine var trendLine = new wijmo.chart.analytics.TrendLine(); trendLine.name = 'Trend Line'; trendLine.binding = 'y'; trendLine.sampleCount = 100; gettingStartedChart.series.push(trendLine);
.wj-flexchart { background-color: white; box-shadow: 4px 4px 10px 0px rgba(50, 50, 50, 0.75); height: 400px; margin-bottom: 12px; padding: 8px; }

Result (live):

趋势线

你可以通过设置TrendLine的fitType属性使用不同类型的趋势线。

下面的样例允许你查看当你更改这个属性时发生的现象:

<div id="trendLineChart"></div> <select id="fitTypeMenu"> <option value="0" selected="selected">Linear</option> <option value="1">Exponential</option> <option value="2">Logarithmic</option> <option value="3">Power</option> <option value="4">Fourier</option> <option value="5">Polynomial</option> <option value="6">MinX</option> <option value="7">MinY</option> <option value="8">MaxX</option> <option value="9">MaxY</option> <option value="10">AverageX</option> <option value="11">AverageY</option> </select>
// create FlexChart and Menus var trendLineChart = new wijmo.chart.FlexChart('#trendLineChart'), fitTypeMenu = new wijmo.input.Menu('#fitTypeMenu'), trendLine; // initialize FlexChart's properties trendLineChart.initialize({ itemsSource: appData, bindingX: 'x', series: [{ name: 'Origin', binding: 'y', chartType: wijmo.chart.ChartType.Scatter }] }); //create TrendLine trendLine = new wijmo.chart.analytics.TrendLine(); trendLine.name = 'Trend Line'; trendLine.binding = 'y'; trendLine.sampleCount = 100; trendLineChart.series.push(trendLine); // update the menus' headers updateMenuHeader(); fitTypeMenu.selectedIndexChanged.addHandler(function () { if (fitTypeMenu.selectedValue) { // update TrendLine's fitType trendLine.fitType = parseInt(fitTypeMenu.selectedValue); // update menu header updateMenuHeader(); } }); // helper function for Menu headers function updateMenuHeader() { fitTypeMenu.header = '<b>Fit Type</b>: ' + fitTypeMenu.text; }

Result (live):

移动平均值

你可以通过设置MovingAverage的type属性来使用不同的类型。

MovingAverage类有一个period属性,允许你设置计算平均值的时间段。

下面的样例让你观察你改变这些属性后会发生什么

<div id="movingAverageChart"></div> <select id="typeMenu"> <option value="0" selected="selected">Simple</option> <option value="1">Weighted</option> <option value="2">Exponential</option> <option value="3">Triangular</option> </select> <b>Period:</b> <input id="periodInput" type="text" />
// create FlexChart and Menus var movingAverageChart = new wijmo.chart.FlexChart('#movingAverageChart'), typeMenu = new wijmo.input.Menu('#typeMenu'), periodInput = new wijmo.input.InputNumber('#periodInput'), movingAverage; //set inputnumber periodInput.value = 2; periodInput.min = 2; periodInput.max = 29; periodInput.step = 1; periodInput.format = "n0"; // initialize FlexChart's properties movingAverageChart.initialize({ itemsSource: getData(40), bindingX: 'x', series: [{ name: 'Origin', binding: 'y', chartType: wijmo.chart.ChartType.Scatter }] }); //create MovingAverage movingAverage = new wijmo.chart.analytics.MovingAverage(); movingAverage.name = 'MA'; movingAverage.binding = 'y'; movingAverage.sampleCount = 100; movingAverageChart.series.push(movingAverage); // update the menus' headers updateMenuHeader(); typeMenu.selectedIndexChanged.addHandler(function () { if (typeMenu.selectedValue) { // update MovingAverage's type movingAverage.type = parseInt(typeMenu.selectedValue); // update menu header updateMenuHeader(); } }); periodInput.valueChanged.addHandler(function () { // update MovingAverage's period movingAverage.period = periodInput.value; }); // helper function for Menu headers function updateMenuHeader() { typeMenu.header = '<b>Type</b>: ' + typeMenu.text; }

Result (live):

Period:

YFunctionSeries

这个视图演示FlexChart的Y Function Series。 YfunctionSeries允许绘制一个函数,用y=y(x)表达式定义。使用func属性指定该函数。

<div id="yFuncSeriesChart"></div>
// create yFuncSeriesChart var yFuncSeriesChart = new wijmo.chart.FlexChart('#yFuncSeriesChart'); //create YFunctionSeries var yFuncSeries = new wijmo.chart.analytics.YFunctionSeries(); yFuncSeries.name = 'YFuncSeries'; yFuncSeries.min = -10; yFuncSeries.max = 10; yFuncSeries.sampleCount = 300; yFuncSeries.func = function (value) { return Math.sin(4 * value) * Math.cos(3 * value); }; yFuncSeriesChart.series.push(yFuncSeries);

Result (live):

ParametricFunctionSeries

ParametricFunctionSeries允许绘制一个函数,定义参数公式为x=x(t)和y=y(t)。使用xFuncyFunc属性来指定函数的坐标x,y。

<div id="paramFuncSeriesChart"></div>
//create paramFuncSeriesChart var paramFuncSeriesChart = new wijmo.chart.FlexChart('#paramFuncSeriesChart'), xParam = 5, yParam = 7; //create ParametricFunctionSeries var paramFuncSeries = new wijmo.chart.analytics.ParametricFunctionSeries(); paramFuncSeries.name = 'ParamFunc'; paramFuncSeries.max = 2 * Math.PI; paramFuncSeries.sampleCount = 1000; paramFuncSeries.xFunc = function (value) { return Math.cos(value * xParam); }; paramFuncSeries.yFunc = function (value) { return Math.sin(value * yParam); }; paramFuncSeriesChart.series.push(paramFuncSeries);

Result (live):

瀑布图

瀑布图显示一个变化的合计值,在系列中这个值被不断的增加或者减少。

<div id="waterfallChart"></div> <div class="form-horizontal"> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div class="checkbox"> <label> <input id="relativeData" type="checkbox"> Is RelativeData? </label> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div class="checkbox"> <label> <input id="connectorLines" type="checkbox"> Show Connector Lines? </label> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div class="checkbox"> <label> <input id="showTotal" type="checkbox"> Show Total? </label> </div> </div> </div> <div class="form-group"> <div class="col-md-offset-3 col-md-9"> <div class="checkbox"> <label> <input id="showIntermediateTotal" type="checkbox"> Show Intermediate Total? </label> </div> </div> </div> </div>
//create chart var waterfallChart = new wijmo.chart.FlexChart('#waterfallChart'), relativeData = document.getElementById('relativeData'), connectorLines = document.getElementById('connectorLines'), showTotal = document.getElementById('showTotal'), showIntermediateTotal = document.getElementById('showIntermediateTotal'); // populate itemsSource var names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], data = []; for (var i = 0, len = names.length; i < len; i++) { data.push({ name: names[i], value: Math.round((0.5 - Math.random()) * 1000) }); } waterfallChart.itemsSource = data; waterfallChart.binding = 'value'; waterfallChart.bindingX = 'name'; //create Waterfall series var waterfall = new wijmo.chart.analytics.Waterfall(); waterfall.relativeData = false; waterfall.connectorLines = false; waterfall.showTotal = false; waterfall.start = 1000; waterfall.showIntermediateTotal = false; waterfall.intermediateTotalPositions = [3, 6, 9, 12]; waterfall.intermediateTotalLabels = ['Q1', 'Q2', 'Q3', 'Q4']; waterfall.name = 'Increase,Decrease,Total'; waterfall.styles = { connectorLines: { stroke: '#333', 'stroke-dasharray': '5 5' }, start: { fill: '#7dc7ed' }, falling: { fill: '#dd2714', stroke: '#a52714' }, rising: { fill: '#0f9d58', stroke: '#0f9d58' }, intermediateTotal: { fill: '#7dc7ed' }, total: { fill: '#7dc7ed' } }; waterfallChart.series.push(waterfall); waterfallChart.tooltip.content = function (ht) { if (ht) { return '<b>' + ht.x + '</b><br/>value: ' + ht.y; } } // relativeData - initialize checkbox properties relativeData.addEventListener('change', function () { waterfall.relativeData = this.checked; }); // connectorLines - initialize checkbox properties connectorLines.addEventListener('change', function () { waterfall.connectorLines = this.checked; }); // showTotal - initialize checkbox properties showTotal.addEventListener('change', function () { waterfall.showTotal = this.checked; }); // showIntermediateTotal - initialize checkbox properties showIntermediateTotal.addEventListener('change', function () { waterfall.showIntermediateTotal = this.checked; });

Result (live):

箱形图

箱形图系列通常用于比较不同组数值数据之间的分布。

<div id="boxwhiskerChart"></div> <div class="form-horizontal"> <dl class="dl-horizontal"> <dt>Group Width</dt> <dd> <div id="boxGroupWidth"></div> </dd> </dl> <dl class="dl-horizontal"> <dt>Gap Width</dt> <dd> <div id="boxGapWidth"></div> </dd> </dl> <dl class="dl-horizontal"> <dt> <label for="boxShowMeanLine">Show Mean Line</label> </dt> <dd> <input id="boxShowMeanLine" type="checkbox" /> </dd> </dl> <dl class="dl-horizontal"> <dt> <label for="boxShowMeanMarker">Show Mean Marker</label> </dt> <dd> <input id="boxShowMeanMarker" type="checkbox" /> </dd> </dl> <dl class="dl-horizontal"> <dt> <label for="boxShowInnerPoints">Show Inner Points</label> </dt> <dd> <input id="boxShowInnerPoints" type="checkbox" /> </dd> </dl> <dl class="dl-horizontal"> <dt> <label for="boxShowOutliers">Show Outliers</label> </dt> <dd> <input id="boxShowOutliers" type="checkbox" /> </dd> </dl> <dl class="dl-horizontal"> <dt> <label for="boxRotated">Rotated</label> </dt> <dd> <input id="boxRotated" type="checkbox" /> </dd> </dl> <dl class="dl-horizontal"> <dt> <label for="boxShowLabel">Show Label</label> </dt> <dd> <input id="boxShowLabel" type="checkbox" /> </dd> </dl> <dl class="dl-horizontal"> <dt>Quartile Calculation</dt> <dd> <select id="boxQuartileCalculation"> <option value="0" selected="selected">Inclusive Median</option> <option value="1">Exclusive Median</option> </select> </dd> </dl> </div>
//create chart var boxwhiskerChart = new wijmo.chart.FlexChart('#boxwhiskerChart'), groupWidth = new wijmo.input.InputNumber('#boxGroupWidth'), gapWidth = new wijmo.input.InputNumber('#boxGapWidth'), quartileCalculation = new wijmo.input.Menu('#boxQuartileCalculation'); // populate itemsSource // generate some random data var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','), funnelData = [], boxData = []; for (var i = 0; i < countries.length; i++) { boxData.push({ country: countries[i], downloads: [getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData()], sales: [getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData()], expenses: [getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData(), getData()] }); } function getData() { return Math.round(Math.random() * 100); } //create Box & Whisker series boxwhiskerChart.beginUpdate(); boxwhiskerChart.tooltip.content = function (hti) { return '<b>' + hti.name + '</b> - <b>' + hti.x + '</b></br>' + '<b>min</b>: ' + hti.item.min + '</br>' + '<b>firstQuartile</b>: ' + hti.item.firstQuartile + '</br>' + '<b>median</b>: ' + hti.item.median + '</br>' + '<b>thirdQuartile</b>: ' + hti.item.thirdQuartile + '</br>' + '<b>max</b>: ' + hti.item.max + '</br>' + '<b>mean</b>: ' + hti.item.mean + '</br>'; } boxwhiskerChart.itemsSource = boxData; boxwhiskerChart.bindingX = 'country'; var boxwhisker1 = new wijmo.chart.analytics.BoxWhisker(); boxwhisker1.name = 'downloads'; boxwhisker1.binding = 'downloads'; boxwhiskerChart.series.push(boxwhisker1); var boxwhisker2 = new wijmo.chart.analytics.BoxWhisker(); boxwhisker2.name = 'sales'; boxwhisker2.binding = 'sales'; boxwhiskerChart.series.push(boxwhisker2); var boxwhisker3 = new wijmo.chart.analytics.BoxWhisker(); boxwhisker3.name = 'expenses'; boxwhisker3.binding = 'expenses'; boxwhiskerChart.series.push(boxwhisker3); boxwhiskerChart.endUpdate(); // groupWidth - initialize InputNumber's properties groupWidth.min = 0; groupWidth.max = 1; groupWidth.step = 0.1; groupWidth.valueChanged.addHandler(function (sender) { if (sender.value < sender.min || sender.value > sender.max) { return; } boxwhisker1.groupWidth = sender.value; boxwhisker2.groupWidth = sender.value; boxwhisker3.groupWidth = sender.value; boxwhiskerChart.refresh(true); }); groupWidth.value = 0.8; // gapWidth - initialize InputNumber's properties gapWidth.min = 0; gapWidth.max = 1; gapWidth.step = 0.1; gapWidth.valueChanged.addHandler(function (sender) { if (sender.value < sender.min || sender.value > sender.max) { return; } boxwhisker1.gapWidth = sender.value; boxwhisker2.gapWidth = sender.value; boxwhisker3.gapWidth = sender.value; boxwhiskerChart.refresh(true); }); gapWidth.value = 0.1; updateMenuHeader(quartileCalculation); quartileCalculation.selectedIndexChanged.addHandler(function () { if (quartileCalculation.selectedValue) { var val = +quartileCalculation.selectedValue; boxwhisker1.quartileCalculation = val; boxwhisker2.quartileCalculation = val; boxwhisker3.quartileCalculation = val; updateMenuHeader(quartileCalculation); boxwhiskerChart.refresh(true); } }); var showMeanLine = document.getElementById('boxShowMeanLine'); showMeanLine.addEventListener('click', function () { boxwhisker1.showMeanLine = this.checked; boxwhisker2.showMeanLine = this.checked; boxwhisker3.showMeanLine = this.checked; boxwhiskerChart.refresh(true); }); var showMeanMarker = document.getElementById('boxShowMeanMarker'); showMeanMarker.addEventListener('click', function () { boxwhisker1.showMeanMarker = this.checked; boxwhisker2.showMeanMarker = this.checked; boxwhisker3.showMeanMarker = this.checked; boxwhiskerChart.refresh(true); }); var boxShowInnerPoints = document.getElementById('boxShowInnerPoints'); boxShowInnerPoints.addEventListener('click', function () { boxwhisker1.showInnerPoints = this.checked; boxwhisker2.showInnerPoints = this.checked; boxwhisker3.showInnerPoints = this.checked; boxwhiskerChart.refresh(true); }); var boxShowOutliers = document.getElementById('boxShowOutliers'); boxShowOutliers.addEventListener('click', function () { boxwhisker1.showOutliers = this.checked; boxwhisker2.showOutliers = this.checked; boxwhisker3.showOutliers = this.checked; boxwhiskerChart.refresh(true); }); var boxRotated = document.getElementById('boxRotated'); boxRotated.addEventListener('click', function () { boxwhiskerChart.rotated = this.checked; boxwhiskerChart.refresh(true); }); var boxShowLabel = document.getElementById('boxShowLabel'); boxShowLabel.addEventListener('click', function () { boxwhiskerChart.dataLabel.content = this.checked ? '{y}' : ''; boxwhiskerChart.refresh(true); }); // helper function for Menu headers function updateMenuHeader(menu) { menu.header = menu.text; }

Result (live):

Group Width
Gap Width
Quartile Calculation