Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional valueField config setting when wanting to use another … #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bestFitLine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ Add `extend: true` to your `bestFitLine` config, and the plugin will extend the
trend line to fit the whole scope of data, even if your actual graph starts and
ends at some mid-point in data.

### Using another valueField from dataProvidor

Add `valueField: "theField"` to your `bestFitLine` config to change the field
used to calculate the trend line.
Default is the current graph's valueField.

Can be useful when using "openField" setting in graph (bar charts that doesn't
start at 0).


## Examples

Expand Down
11 changes: 9 additions & 2 deletions bestFitLine/bestFitLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,25 @@ AmCharts.bestFitLineProcess = function( chart, validateData ) {
lastCat,
firstDataCat,
lastDataCat;

if ( graph.bestFitLine !== undefined ) {
// found a graph
// generate values
var x = [],
y = [];

// set valueField setting
if ( graph.bestFitLine.valueField === undefined ) {
graph.bestFitLine.valueField = graph.valueField;
}

for ( var z = 0; z < chart.dataProvider.length; z++ ) {

// calculate category
var cat = getCategoryIndex( chart.dataProvider[ z ][ chart.categoryField ], z, chart );

// get value
var value = chart.dataProvider[ z ][ graph.valueField ] ;
var value = chart.dataProvider[ z ][ graph.bestFitLine.valueField ] ;

if ( value != null ) {
value = +value;
Expand Down Expand Up @@ -123,7 +130,7 @@ AmCharts.bestFitLineProcess = function( chart, validateData ) {

// create a graph for the best fit line
var trendGraph = graph.bestFitLine;
trendGraph.valueField = graph.valueField + "FitLine";
trendGraph.valueField = graph.bestFitLine.valueField + "FitLine";
if ( trendGraph.trendInited !== true )
chart.graphs.push( trendGraph );
trendGraph.trendInited = true;
Expand Down