Skip to content

Commit

Permalink
Highstock: Fixed #7773, added new property (compareToMain).
Browse files Browse the repository at this point in the history
  • Loading branch information
raf18seb authored and TorsteinHonsi committed Feb 5, 2019
1 parent 69a77a8 commit 7f0ce96
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 6 deletions.
29 changes: 25 additions & 4 deletions js/indicators/indicators.src.js
Expand Up @@ -83,10 +83,10 @@ seriesType(
*
* @extends plotOptions.line
* @since 6.0.0
* @excluding allAreas, colorAxis, compare, compareBase, joinBy, keys,
* navigatorOptions, pointInterval, pointIntervalUnit,
* pointPlacement, pointRange, pointStart, showInNavigator,
* stacking, useOhlcData
* @excluding allAreas, colorAxis, joinBy, keys, navigatorOptions,
* pointInterval, pointIntervalUnit, pointPlacement,
* pointRange, pointStart, showInNavigator, stacking,
* useOhlcData
* @product highstock
* @optionparent plotOptions.sma
*/
Expand All @@ -112,6 +112,16 @@ seriesType(
* @type {string}
*/
linkedTo: undefined,
/**
* Whether to compare indicator to the main series values
* or indicator values.
*
* @sample {highstock} stock/plotoptions/series-comparetomain/
* compareToMain
*
* @type {boolean}
*/
compareToMain: false,
/**
* Paramters used in calculation of regression series' points.
*/
Expand All @@ -134,6 +144,17 @@ seriesType(
* @lends Highcharts.Series.prototype
*/
{
processData: function () {
var series = this,
compareToMain = series.options.compareToMain,
linkedParent = series.linkedParent;

Series.prototype.processData.apply(series, arguments);

if (linkedParent && linkedParent.compareValue && compareToMain) {
series.compareValue = linkedParent.compareValue;
}
},
bindTo: {
series: true,
eventName: 'updatedData'
Expand Down
6 changes: 6 additions & 0 deletions samples/stock/plotoptions/series-comparetomain/demo.details
@@ -0,0 +1,6 @@
---
name: Highcharts Stock Demo
authors:
- Rafal Sebestjanski
js_wrap: b
...
7 changes: 7 additions & 0 deletions samples/stock/plotoptions/series-comparetomain/demo.html
@@ -0,0 +1,7 @@
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/indicators/indicators-all.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/modules/export-data.js"></script>

<div id="container" style="height: 400px; min-width: 310px"></div>
36 changes: 36 additions & 0 deletions samples/stock/plotoptions/series-comparetomain/demo.js
@@ -0,0 +1,36 @@
$.getJSON('https://www.highcharts.com/samples/data/aapl-c.json', function (data) {
// Create the chart
Highcharts.stockChart('container', {

plotOptions: {
series: {
compare: 'percent'
}
},

rangeSelector: {
selected: 1
},

title: {
text: 'AAPL Stock Price'
},

tooltip: {
pointFormat: '{series.name}: {point.y} ({point.change}%)',
valueDecimals: 2
},

series: [{
name: 'AAPL',
id: 'main',
data: data
}, {
type: 'sma',
name: 'SMA',
linkedTo: 'main',
compareToMain: true
}]

});
});
1 change: 1 addition & 0 deletions samples/unit-tests/series/compare/demo.html
@@ -1,4 +1,5 @@
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/indicators/indicators-all.js"></script>


<div id="qunit"></div>
Expand Down
42 changes: 40 additions & 2 deletions samples/unit-tests/series/compare/demo.js
@@ -1,5 +1,3 @@


QUnit.test('Compare in candlesticks', function (assert) {
var chart = Highcharts.stockChart('container', {
series: [{
Expand Down Expand Up @@ -98,4 +96,44 @@ QUnit.test('Compare with the correct compareValue', function (assert) {
series.points[0][series.options.pointValKey],
'compareValue is correct'
);
});

QUnit.test('Compare to the proper series (#7773)', function (assert) {
var chart = Highcharts.stockChart('container', {

plotOptions: {
series: {
compare: 'percent'
}
},

tooltip: {
pointFormat: '{series.name}: {point.y} ({point.change}%)'
},

series: [{
data: [13, 12, 8, 4, 2, 5, 10, 30],
id: 'main'
}, {
type: 'sma',
name: 'SMA',
linkedTo: 'main',
compareToMain: true,
params: {
period: 3
}
}]
});

assert.strictEqual(
chart.series[1].compareValue,
13,
'compareValue is correct'
);

assert.strictEqual(
chart.series[1].data[0].change,
-15.384615384615387,
'First change value is correct'
);
});

0 comments on commit 7f0ce96

Please sign in to comment.