// 判断数据类型
function isType(type) {
  return function (val) {
    if (Object.prototype.toString.call(val) === `[object ${type}]`) {
      return true
    }
    return false
  }
}
export const isArray = isType('Array')
export const isObject = isType('Object')
export const isStr = isType('String')
export const isNum = isType('Number')
export const isFun = isType('Function')
export const isUndefined = isType('Undefined')
export const isNull = isType('Null')
export const isError = isType('Error')

//采用配置项替换默认值
export function setNewValue(settings, defaultSettings) {
  const result = JSON.parse(JSON.stringify(defaultSettings));
  if (settings && typeof settings === "object") {
    for (let key in settings) {
      if (settings[key] && result[key] && settings[key] === result[key])
        continue;
      else
      result[key] = settings[key];
    }
  }
  return result;
}

// echarts文字较多时换行展示
export function newlineEcharts(option, number, axis){
  var axisLabelConfig = option[axis]['axisLabel'] ? option[axis]['axisLabel'] : {};
  option[axis]['axisLabel']={
    ...axisLabelConfig,
    interval: 0,
    formatter: function(params){
      var newParamsName = "";
      var paramsNameNumber = params.length;
      var provideNumber = number;
      var rowNumber = Math.ceil(paramsNameNumber / provideNumber);
      if (paramsNameNumber > provideNumber) {
        for (var p = 0; p < rowNumber; p++) {
          var tempStr = "";
          var start = p * provideNumber;
          var end = start + provideNumber;
          if (p == rowNumber - 1) {
            tempStr = params.substring(start, paramsNameNumber);
          } else {
            tempStr = params.substring(start, end) + "\n";
          }
          newParamsName += tempStr;
        }
        } else {
          newParamsName = params;
        }
        return newParamsName
    }
  }
  return option;
}