Skip to content

Commit

Permalink
fix: scrollable legend pager does not work when the target index is i…
Browse files Browse the repository at this point in the history
…llegal. Fix #7568
  • Loading branch information
100pah committed Jan 28, 2019
1 parent 1b3010a commit 6f29ed6
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 70 deletions.
20 changes: 15 additions & 5 deletions src/component/legend/ScrollableLegendView.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ var ScrollableLegendView = LegendView.extend({
var orientIdx = legendModel.getOrient().index;
var wh = WH[orientIdx];
var xy = XY[orientIdx];

var targetItemIndex = this._findTargetItemIndex(scrollDataIndex);
var children = contentGroup.children();
var targetItem = children[targetItemIndex];
Expand Down Expand Up @@ -415,17 +416,26 @@ var ScrollableLegendView = LegendView.extend({
_findTargetItemIndex: function (targetDataIndex) {
var index;
var contentGroup = this.getContentGroup();
var defaultIndex;

if (this._showController) {
contentGroup.eachChild(function (child, idx) {
if (child.__legendDataIndex === targetDataIndex) {
var legendDataIdx = child.__legendDataIndex;
// FIXME
// If the given targetDataIndex (from model) is illegal,
// we use defualtIndex. But the index on the legend model and
// action payload is still illegal. That case will not be
// changed until some scenario requires.
if (defaultIndex == null && legendDataIdx != null) {
defaultIndex = idx;
}
if (legendDataIdx === targetDataIndex) {
index = idx;
}
});
}
else {
index = 0;
}
return index;

return index != null ? index : defaultIndex;
}

});
Expand Down
47 changes: 45 additions & 2 deletions test/legend.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ <h1>Scroll vertically, left: 10, top: 'center', position: 'start', height: 50% (
<div id="icon-circle"></div>
<div id="only-single-item"></div>
<div id="vary-size"></div>
<div id="index-to-no-name"></div>

<script>

Expand Down Expand Up @@ -1067,8 +1068,6 @@ <h1>Scroll vertically, left: 10, top: 'center', position: 'start', height: 50% (
});
}

var seriesInfo = makeSeries(50, 3);

var option = {
legend: [{
type: 'scroll',
Expand Down Expand Up @@ -1098,5 +1097,49 @@ <h1>Scroll vertically, left: 10, top: 'center', position: 'start', height: 50% (










<script>
require([
'echarts',
], function (echarts) {

var seriesInfo = makeSeries(50, 3);
var series = seriesInfo.series.slice();
series.shift();

var option = {
legend: [{
type: 'scroll',
data: seriesInfo.legendData
}],
tooltip: {
},
xAxis: {
data: seriesInfo.xAxisData
},
yAxis: {
},
series: series
};

testHelper.create(echarts, 'index-to-no-name', {
option: option,
title: [
'Pager should be OK when legend 0 (legendData index 0) is not be displayed.',
'should be at **page 1** at the beginning'
],
draggable: true
});
})
</script>



</body>
</html>
128 changes: 65 additions & 63 deletions test/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,74 +77,76 @@
});
}

// It is not a good solution.
// Do not need it any more:

// Mount bundle version print.
if (typeof require !== 'undefined') {
var originalRequire = require;
window.require = function (deps, cb) {
var newCb = function () {
if (deps && deps instanceof Array) {
printBundleVersion(deps, [].slice.call(arguments));
}
cb && cb.apply(this, arguments);
};
return originalRequire.call(this, deps, newCb);
};
}
// if (typeof require !== 'undefined') {
// var originalRequire = require;
// window.require = function (deps, cb) {
// var newCb = function () {
// if (deps && deps instanceof Array) {
// printBundleVersion(deps, [].slice.call(arguments));
// }
// cb && cb.apply(this, arguments);
// };
// return originalRequire.call(this, deps, newCb);
// };
// }

function printBundleVersion(bundleIds, bundles) {
var content = [];
for (var i = 0; i < bundleIds.length; i++) {
var bundle = bundles[i];
var bundleVersion = bundle && bundle.bundleVersion;
if (bundleVersion) {
var date = new Date(+bundleVersion);
// Check whether timestamp.
if (!isNaN(+date)) {
bundleVersion = '<span style="color:yellow">'
+ pad(date.getHours(), 2) + ':'
+ pad(date.getMinutes(), 2) + ':'
+ pad(date.getSeconds(), 2) + '.' + pad(date.getMilliseconds(), 3)
+ '</span>';
}
else {
bundleVersion = encodeHTML(bundleVersion);
}
content.push(encodeHTML(bundleIds[i]) + '.js: ' + bundleVersion);
}
}
// function printBundleVersion(bundleIds, bundles) {
// var content = [];
// for (var i = 0; i < bundleIds.length; i++) {
// var bundle = bundles[i];
// var bundleVersion = bundle && bundle.bundleVersion;
// if (bundleVersion) {
// var date = new Date(+bundleVersion);
// // Check whether timestamp.
// if (!isNaN(+date)) {
// bundleVersion = '<span style="color:yellow">'
// + pad(date.getHours(), 2) + ':'
// + pad(date.getMinutes(), 2) + ':'
// + pad(date.getSeconds(), 2) + '.' + pad(date.getMilliseconds(), 3)
// + '</span>';
// }
// else {
// bundleVersion = encodeHTML(bundleVersion);
// }
// content.push(encodeHTML(bundleIds[i]) + '.js: ' + bundleVersion);
// }
// }

var domId = 'ec-test-bundle-version';
var dom = document.getElementById(domId);
if (!dom) {
dom = document.createElement('div');
dom.setAttribute('id', domId);
dom.style.cssText = [
'background: rgb(52,56,64)',
'color: rgb(215,215,215)',
'position: fixed',
'right: 0',
'top: 0',
'font-size: 10px',
'padding: 1px 3px 1px 3px',
'border-bottom-left-radius: 3px'
].join(';');
document.body.appendChild(dom);
}
dom.innerHTML += content.join('');
}
// var domId = 'ec-test-bundle-version';
// var dom = document.getElementById(domId);
// if (!dom) {
// dom = document.createElement('div');
// dom.setAttribute('id', domId);
// dom.style.cssText = [
// 'background: rgb(52,56,64)',
// 'color: rgb(215,215,215)',
// 'position: fixed',
// 'right: 0',
// 'top: 0',
// 'font-size: 10px',
// 'padding: 1px 3px 1px 3px',
// 'border-bottom-left-radius: 3px'
// ].join(';');
// document.body.appendChild(dom);
// }
// dom.innerHTML += content.join('');
// }

function pad(num, len) {
return ('000000' + num).substr(-len, len);
}
// function pad(num, len) {
// return ('000000' + num).substr(-len, len);
// }

function encodeHTML(source) {
return String(source)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
// function encodeHTML(source) {
// return String(source)
// .replace(/&/g, '&amp;')
// .replace(/</g, '&lt;')
// .replace(/>/g, '&gt;')
// .replace(/"/g, '&quot;')
// .replace(/'/g, '&#39;');
// }

})();

0 comments on commit 6f29ed6

Please sign in to comment.