Skip to content

Commit

Permalink
calendar cellSize can set auto
Browse files Browse the repository at this point in the history
  • Loading branch information
xhong0 committed Mar 1, 2017
1 parent 0b0318c commit b3674b2
Show file tree
Hide file tree
Showing 23 changed files with 699 additions and 76 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require('./lib/component/geo');
require('./lib/component/parallel');
require('./lib/component/singleAxis');
require('./lib/component/brush');
require('./lib/component/calendar');

require('./lib/component/title');

Expand Down
4 changes: 3 additions & 1 deletion src/chart/graph/simpleLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ define(function (require) {
var zrUtil = require('zrender/core/util');
var simpleLayoutHelper = require('./simpleLayoutHelper');
var simpleLayoutEdge = require('./simpleLayoutEdge');
var modelUtil = require('../../util/model');

return function (ecModel, api) {
ecModel.eachSeriesByType('graph', function (seriesModel) {
var layout = seriesModel.get('layout');
var coordSys = seriesModel.coordinateSystem;
if (coordSys && coordSys.type !== 'view') {
var data = seriesModel.getData();
var dimensions = coordSys.dimensions
var dimensions = modelUtil.getCoordSysDimNames(coordSys);

data.each(dimensions, function () {
var hasValue;
var args = arguments;
Expand Down
56 changes: 34 additions & 22 deletions src/coord/calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ define(function (require) {

type: 'calendar',

dimensions: ['time'],
dimensions: [{
name: 'time',
stackable: false,
type: 'time'
}],

getHandledRangeInfo: function () {
return this._rangeInfo;
Expand Down Expand Up @@ -112,35 +116,43 @@ define(function (require) {
},

update: function (ecModel, api) {
var calendarRect = layout.getLayoutRect(
this._model.getBoxLayoutParams(),

{
width: api.getWidth(),
height: api.getHeight()
}
);

this._rect = calendarRect;

this._firstDayOfWeek = this._model.getModel('dayLabel').get('firstDay');
this._orient = this._model.get('orient');
this._lineWidth = this._model.getModel('itemStyle.normal').getItemStyle().lineWidth || 0;


this._rangeInfo = this._getRangeInfo(this._initRangeOption());
var weeks = this._rangeInfo.weeks || 1;
var whNames = ['width', 'height'];
var cellSize = this._model.get('cellSize').slice();
var layoutParams = this._model.getBoxLayoutParams();
var cellNumbers = this._orient === 'horizontal' ? [weeks, 7] : [7, weeks];

zrUtil.each([0, 1], function (idx) {
if (cellSizeSpecified(cellSize, idx)) {
layoutParams[whNames[idx]] = cellSize[idx] * cellNumbers[idx];
}
});

var size = this._model.get('cellSize');
var whGlobal = {
width: api.getWidth(),
height: api.getHeight()
};
var calendarRect = this._rect = layout.getLayoutRect(layoutParams, whGlobal);

if (zrUtil.isArray(size)) {
this._sw = size[0];
this._sh = size[1] || this._sw;
}
else {
this._sw = size;
this._sh = this._sw;
}
zrUtil.each([0, 1], function (idx) {
if (!cellSizeSpecified(cellSize, idx)) {
cellSize[idx] = calendarRect[whNames[idx]] / cellNumbers[idx];
}
});

function cellSizeSpecified(cellSize, idx) {
return cellSize[idx] != null && cellSize[idx] !== 'auto';
}

this._orient = this._model.get('orient');
this._lineWidth = this._model.getModel('itemStyle.normal').getItemStyle().lineWidth || 0;
this._sw = cellSize[0];
this._sh = cellSize[1];
},


Expand Down
58 changes: 53 additions & 5 deletions src/coord/calendar/CalendarModel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
define(function (require) {

'use strict';

var ComponentModel = require('../../model/Component');
var zrUtil = require('zrender/core/util');
var layout = require('../../util/layout');

var CalendarModel = ComponentModel.extend({

Expand All @@ -12,11 +15,6 @@ define(function (require) {
*/
coordinateSystem: null,

layoutMode: {
type: 'box',
ignoreSize: true
},

defaultOption: {
zlevel: 0,
z: 2,
Expand Down Expand Up @@ -97,9 +95,59 @@ define(function (require) {
fontSize: 20
}
}
},

/**
* @override
*/
init: function (option, parentModel, ecModel, extraOpt) {
var inputPositionParams = layout.getLayoutParams(option);

CalendarModel.superApply(this, 'init', arguments);

var cellSize = normalizeCellSize(option);

mergeLayoutParam(option, inputPositionParams, cellSize);
},

/**
* @override
*/
mergeOption: function (option, extraOpt) {
CalendarModel.superApply(this, 'mergeOption', arguments);

var cellSize = normalizeCellSize(this.option);

mergeLayoutParam(this.option, option, cellSize);
}
});

function normalizeCellSize(option) {
var size = option.cellSize;

if (!zrUtil.isArray(size)) {
option.cellSize = [size, size];
}
else if (size.length === 1) {
size[1] = size[0];
}

return option.cellSize;
}

function mergeLayoutParam(target, raw, cellSize) {
var whNames = ['width', 'height'];
var ignoreSize = zrUtil.map([0, 1], function (idx) {
if (raw[whNames[idx]] != null) {
cellSize[idx] = 'auto';
}
return cellSize[idx] != null && cellSize[idx] !== 'auto';
});
layout.mergeLayoutParam(target, raw, {
type: 'box', ignoreSize: ignoreSize
});
}

return CalendarModel;

});
8 changes: 3 additions & 5 deletions src/layout/points.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
define(function (require) {

var zrUtil = require('zrender/core/util');
var modelUtil = require('../util/model');

return function (seriesType, ecModel) {
ecModel.eachSeriesByType(seriesType, function (seriesModel) {
var data = seriesModel.getData();
var coordSys = seriesModel.coordinateSystem;

if (coordSys) {
var dims = zrUtil.map(coordSys.dimensions, function (dimItem) {
return zrUtil.isObject(dimItem) ? dimItem.name : dimItem;
});
var dims = modelUtil.getCoordSysDimNames(coordSys);

if (dims.length === 1) {
data.each(dims[0], function (x, idx) {
Expand All @@ -29,4 +27,4 @@ define(function (require) {
}
});
};
});
});
16 changes: 10 additions & 6 deletions src/util/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,24 +352,28 @@ define(function(require) {
* @param {Object} targetOption
* @param {Object} newOption
* @param {Object|string} [opt]
* @param {boolean} [opt.ignoreSize=false] Some component must has width and height.
* @param {boolean|Array.<boolean>} [opt.ignoreSize=false] Some component must has width and height.
*/
layout.mergeLayoutParam = function (targetOption, newOption, opt) {
!zrUtil.isObject(opt) && (opt = {});
var hNames = ['width', 'left', 'right']; // Order by priority.
var vNames = ['height', 'top', 'bottom']; // Order by priority.
var hResult = merge(hNames);
var vResult = merge(vNames);

var ignoreSize = opt.ignoreSize;
!zrUtil.isArray(ignoreSize) && (ignoreSize = [ignoreSize, ignoreSize]);

var hResult = merge(hNames, ignoreSize[0]);
var vResult = merge(vNames, ignoreSize[1]);

copy(hNames, targetOption, hResult);
copy(vNames, targetOption, vResult);

function merge(names) {
function merge(names, ignoreSize) {
var newParams = {};
var newValueCount = 0;
var merged = {};
var mergedValueCount = 0;
var enoughParamNumber = opt.ignoreSize ? 1 : 2;
var enoughParamNumber = ignoreSize ? 1 : 2;

each(names, function (name) {
merged[name] = targetOption[name];
Expand Down Expand Up @@ -446,4 +450,4 @@ define(function(require) {
};

return layout;
});
});
14 changes: 13 additions & 1 deletion src/util/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ define(function(require) {
? NaN : +value; // If string (like '-'), using '+' parse to NaN
};

/**
* coordSys.dimensions can be [{name: ..., type: ...}, '...', ...]
*
* @param {Object} coordSys
* @return {Array.<string>}
*/
modelUtil.getCoordSysDimNames = function (coordSys) {
return zrUtil.map(coordSys.dimensions, function (dimItem) {
return zrUtil.isObject(dimItem) ? dimItem.name : dimItem;
});
};

/**
* Create a model proxy to be used in tooltip for edge data, markLine data, markPoint data.
* @param {module:echarts/data/List} data
Expand Down Expand Up @@ -581,4 +593,4 @@ define(function(require) {
}

return modelUtil;
});
});
Loading

0 comments on commit b3674b2

Please sign in to comment.