This is an automated email from the ASF dual-hosted git repository. sushuang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-echarts.git
commit e5fdb088e88bee2b4c8e070e2ccb68d00b41eee9 Author: sushuang <sushuang0...@gmail.com> AuthorDate: Wed Apr 18 22:40:14 2018 +0800 Expose api of auto tick strategy and tweak splitArea logic. --- src/component/axis/CartesianAxisView.js | 2 +- src/coord/Axis.js | 96 ++++++++++++++++++++------------- src/coord/axisTickLabelBuilder.js | 13 +++-- test/axis-interval2.html | 67 +++++++++++++++++++++++ 4 files changed, 136 insertions(+), 42 deletions(-) diff --git a/src/component/axis/CartesianAxisView.js b/src/component/axis/CartesianAxisView.js index 10f60f4..0e80f0e 100644 --- a/src/component/axis/CartesianAxisView.js +++ b/src/component/axis/CartesianAxisView.js @@ -203,7 +203,7 @@ var CartesianAxisView = AxisView.extend({ } var tickValue = ticksCoords[i - 1].tickValue; - newSplitAreaColors.set(tickValue, colorIndex); + tickValue != null && newSplitAreaColors.set(tickValue, colorIndex); this._axisGroup.add(new graphic.Rect({ anid: 'area_' + tickValue, diff --git a/src/coord/Axis.js b/src/coord/Axis.js index 88a22b8..4ae58e2 100644 --- a/src/coord/Axis.js +++ b/src/coord/Axis.js @@ -1,6 +1,10 @@ import {each, map} from 'zrender/src/core/util'; import {linearMap, getPixelPrecision} from '../util/number'; -import {createAxisTicks, createAxisLabels} from './axisTickLabelBuilder'; +import { + createAxisTicks, + createAxisLabels, + calculateCategoryInterval +} from './axisTickLabelBuilder'; var NORMALIZED_EXTENT = [0, 1]; @@ -151,7 +155,9 @@ Axis.prototype = { * `boundaryGap:true` of category axis and splitLine and splitArea. * @param {Object} [opt] * @param {number} [opt.tickModel=axis.model.getModel('axisTick')] - * @param {boolean} [opt.clamp] If `false`, clip. If `true`, clamp. + * @param {boolean} [opt.clamp] If `true`, the first and the last + * tick must be at the axis end points. Otherwise, clip ticks + * that outside the axis extent. * @return {Array.<Object>} [{ * coord: ..., * tickValue: ... @@ -237,7 +243,17 @@ Axis.prototype = { * @abstract * @return {number} Get axis rotate, by degree. */ - getRotate: null + getRotate: null, + + /** + * Only be called in category axis. + * Can be overrided, consider other axes like in 3D. + * @param {boolean} hideLabel + * @return {number} Auto interval for cateogry axis tick and label + */ + calculateCategoryInterval: function (hideLabel) { + return calculateCategoryInterval(this, hideLabel); + } }; @@ -260,40 +276,48 @@ function fixExtentWithBands(extent, nTick) { // case). function fixOnBandTicksCoords(axis, ticksCoords, tickCategoryInterval, alignWithLabel, clamp) { var ticksLen = ticksCoords.length; - if (axis.onBand && !alignWithLabel && ticksLen) { - var axisExtent = axis.getExtent(); - var last; - if (ticksLen === 1) { - ticksCoords[0].coord = axisExtent[0]; - last = ticksCoords[1] = {coord: axisExtent[0]}; - } - else { - var shift = (ticksCoords[1].coord - ticksCoords[0].coord); - each(ticksCoords, function (ticksItem) { - ticksItem.coord -= shift / 2; - var tickCategoryInterval = tickCategoryInterval || 0; - // Avoid split a single data item when odd interval. - if (tickCategoryInterval % 2 > 0) { - ticksItem.coord -= shift / ((tickCategoryInterval + 1) * 2); - } - }); - last = {coord: ticksCoords[ticksLen - 1].coord + shift}; - ticksCoords.push(last); - } - var inverse = axisExtent[0] > axisExtent[1]; - if (inverse - ? ticksCoords[0].coord > axisExtent[0] - : ticksCoords[0].coord < axisExtent[0] - ) { - clamp ? (ticksCoords[0].coord = axisExtent[0]) : ticksCoords.shift(); - } - if (inverse - ? last.coord < axisExtent[1] - : last.coord > axisExtent[1] - ) { - clamp ? (last.coord = axisExtent[1]) : ticksCoords.pop(); - } + if (!axis.onBand || alignWithLabel || !ticksLen) { + return; + } + + var axisExtent = axis.getExtent(); + var last; + if (ticksLen === 1) { + ticksCoords[0].coord = axisExtent[0]; + last = ticksCoords[1] = {coord: axisExtent[0]}; + } + else { + var shift = (ticksCoords[1].coord - ticksCoords[0].coord); + each(ticksCoords, function (ticksItem) { + ticksItem.coord -= shift / 2; + var tickCategoryInterval = tickCategoryInterval || 0; + // Avoid split a single data item when odd interval. + if (tickCategoryInterval % 2 > 0) { + ticksItem.coord -= shift / ((tickCategoryInterval + 1) * 2); + } + }); + last = {coord: ticksCoords[ticksLen - 1].coord + shift}; + ticksCoords.push(last); + } + + var inverse = axisExtent[0] > axisExtent[1]; + + if (littleThan(ticksCoords[0].coord, axisExtent[0])) { + clamp ? (ticksCoords[0].coord = axisExtent[0]) : ticksCoords.shift(); + } + if (clamp && littleThan(axisExtent[0], ticksCoords[0].coord)) { + ticksCoords.unshift({coord: axisExtent[0]}); + } + if (littleThan(axisExtent[1], last.coord)) { + clamp ? (last.coord = axisExtent[1]) : ticksCoords.pop(); + } + if (clamp && littleThan(last.coord, axisExtent[1])) { + ticksCoords.push({coord: axisExtent[1]}); + } + + function littleThan(a, b) { + return inverse ? a > b : a < b; } } diff --git a/src/coord/axisTickLabelBuilder.js b/src/coord/axisTickLabelBuilder.js index 74c1d1d..f82a735 100644 --- a/src/coord/axisTickLabelBuilder.js +++ b/src/coord/axisTickLabelBuilder.js @@ -151,13 +151,16 @@ function listCacheSet(cache, key, value) { return value; } -function makeAutoCategoryInterval(axis, useFake) { - var cacheKey = useFake ? 'fakeAutoInterval' : 'autoInterval'; +function makeAutoCategoryInterval(axis, hideLabel) { + var cacheKey = hideLabel ? 'tickAutoInterval' : 'autoInterval'; var result = inner(axis)[cacheKey]; if (result != null) { return result; } - return (inner(axis)[cacheKey] = calculateCategoryInterval(axis, useFake)); + + return ( + inner(axis)[cacheKey] = axis.calculateCategoryInterval(hideLabel) + ); } /** @@ -165,7 +168,7 @@ function makeAutoCategoryInterval(axis, useFake) { * To get precise result, at least one of `getRotate` and `isHorizontal` * should be implemented in axis. */ -function calculateCategoryInterval(axis, useFake) { +export function calculateCategoryInterval(axis, hideLabel) { var params = fetchAutoCategoryIntervalCalculationParams(axis); var labelFormatter = makeLabelFormatter(axis); var rotation = (params.axisRotate - params.labelRotate) / 180 * Math.PI; @@ -200,7 +203,7 @@ function calculateCategoryInterval(axis, useFake) { var width = 0; var height = 0; - if (!useFake) { + if (!hideLabel) { // Polar is also calculated in assumptive linear layout here. // Not precise, do not consider align and vertical align // and each distance from axis line yet. diff --git a/test/axis-interval2.html b/test/axis-interval2.html index 53980f5..d07a963 100644 --- a/test/axis-interval2.html +++ b/test/axis-interval2.html @@ -34,6 +34,7 @@ <div class="chart" id="main5"></div> <div class="chart" id="main6"></div> <div class="chart" id="main7"></div> + <div class="chart" id="main8"></div> <script> @@ -1013,6 +1014,72 @@ + <script> + + require([ + 'echarts' + ], function (echarts) { + + var xAxisData = []; + var data1 = []; + var data3 = []; + + for (var i = 0; i < 100; i++) { + xAxisData.push('c' + i); + data1.push((Math.random() * 5).toFixed(2)); + data3.push((Math.random() + 0.5).toFixed(2)); + } + + var option = { + legend: { + }, + tooltip: { + trigger: 'axis', + axisPointer: { + type: 'shadow' + } + }, + xAxis: { + type: 'category', + data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + splitArea: { + interval: 3, + show: true, + areaStyle: { + color: ['blue', 'red'] + } + } + }, + yAxis: { + axisLabel: { + show: false + }, + splitArea: { + show: false + } + }, + dataZoom: [{ + type: 'inside' + }, { + }], + series: [] + }; + + chart = myChart = testHelper.create(echarts, 'main8', { + title: [ + 'Check splitArea correct for indivisible interval.', + 'Move left handle of the dataZoom and check splitArea correct' + ], + option: option, + info: {xAxis: option.xAxis} + }); + }); + + </script> + + + + </body> </html> \ No newline at end of file -- To stop receiving notification emails like this one, please contact sushu...@apache.org. --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@echarts.apache.org For additional commands, e-mail: commits-h...@echarts.apache.org