Skip to content
页内导航

IZTRO

一套轻量级获取紫微斗数排盘信息的 Javascript 开源库。

前言

欢迎使用 iztro 开发文档!本页将向你介绍如何集成、如何获取数据、以及如何快速得到紫微斗数里一张星盘上的所有数据。如果你只是基础使用者,阅读完本篇文档将足够你日常使用。 如果你已经掌握了本页内容,可以到其他页面进行更深入的探索。如果你对紫微斗数感兴趣,但是有没有相关基础,可以点击 基础知识扫盲 进行扫盲学习。

你将获取到以下信息:

  • 如何将 iztro 安装和集成到你的代码里
  • 如何获取到一张星盘
  • 如何基于星盘开始分析宫位
  • 如何基于宫位开始分析星曜

产品

名称链接语言作者
iztroiztroGitHubGiteeTypescriptSylarLong
react-iztroreact-iztroGitHubGiteeReactSylarLong
iztro-hookiztro-hookGitHubGiteeReactSylarLong
py-iztropy-iztroGitHubGiteePython昊色居士
py-iztrodart_iztroGitHubGiteeDartEdwinXiang

安装

使用包管理安装

你可以使用任意一种你熟悉的包管理工具进行安装

sh
npm install iztro -S

安装顺利的话,会在你的package.json依赖列表中找到iztro

json
"dependencies": {
  "iztro": "^1.1.0"
}

版本号可能会有所不同

纯JS库使用 ^2.0.4

v2.0.4 版本以后,编译了 umd 的纯Javascript库。可以下载 release 资源文件中的 🗜️iztro-min-js.tar.gz 压缩包,里面包含了一个 iztro 压缩混淆过的js文件和对应的sourcemap文件。

当然,我们更推荐你直接使用 CDN 加速链接,你可以在下面列表中选择一个,在没有指定版本号的时候,会自动指向最新版本的代码库

你也可以使用如下规则来指定版本:

开始使用

引入代码

你可以根据下列方式将iztro引入你的代码

ts
import { astro } from "iztro";

获取星盘数据

在获取紫微斗数星盘的时候,可以根据农历或者阳历日期来获取,iztro提供了这两种获取方式,你可以根据你的需求使用,但我们更推荐你使用阳历的方式来使用。 放心,阳历和农历在程序内部获取到的数据是统一的。

使用 阳历 有如下便利性:

  • 可以很方便的在出生证上查到
  • 可以使用日历组件进行日期选择
  • 现在很多人都无法记住农历日期
  • 可以避免因为忽略了闰月而带来的一系列问题
ts
import { astro } from "iztro";

// 通过阳历获取星盘信息
const astrolabe = astro.bySolar("2000-8-16", 2, "");

// 通过农历获取星盘信息
const astrolabe = astro.byLunar("2000-7-17", 2, "");

你会发现以上bySolarbyLunar的返回值是一样的, 这是因为byLunar方法在内部处理的时候,也是将日期转化为阳历以后调用bySolar方法。 以下是执行结果,因为结果比较长,所以将之折叠起来,如果你想要查看你调用结果是否和这个一样,可以展开查看:

astro.bySolar()astro.byLunar() 方法执行结果
ts
{
  // 阳历日期
  solarDate: '2000-8-16',
   // 农历日期
  lunarDate: '二〇〇〇年七月十七',
  // 四柱
  chineseDate: '庚辰 甲申 丙午 庚寅',
  // 时辰
  time: '寅时',
  // 时辰对应的时间段
  timeRange: '03:00~05:00',
  // 星座
  sign: '狮子座',
  // 生肖
  zodiac: '',
  // 命宫地支
  earthlyBranchOfSoulPalace: '',
  // 身宫地支
  earthlyBranchOfBodyPalace: '',
  // 命主
  soul: '破军',
  // 身主
  body: '文昌',
  // 五行局
  fiveElementsClass: '木三局',
  // 十二宫数据
  palaces: [
    {
      // 宫名
      name: '财帛',
      // 是否身宫
      isBodyPalace: false,
      // 是否来因宫
      isOriginalPalace: false,
      // 宫位天干
      heavenlyStem: '',
      // 宫位地支
      earthlyBranch: '',
      // 主星(含天马禄存)
      majorStars: [
        { name: '武曲', type: 'major', scope: 'origin', brightness: '' },
        { name: '天相', type: 'major', scope: 'origin', brightness: '' },
        { name: '天马', type: 'tianma', scope: 'origin', brightness: '' },
      ],
      // 辅星(含六吉六煞)
      minorStars: [],
      // 杂耀
      adjectiveStars: [
        { name: '月解', type: 'helper', scope: 'origin' },
        { name: '三台', type: 'adjective', scope: 'origin' },
        { name: '天寿', type: 'adjective', scope: 'origin' },
        { name: '天巫', type: 'adjective', scope: 'origin' },
        { name: '天厨', type: 'adjective', scope: 'origin' },
        { name: '阴煞', type: 'adjective', scope: 'origin' },
        { name: '天哭', type: 'adjective', scope: 'origin' },
      ],
      // 长生12神
      changsheng12: '',
      // 博士12神
      boshi12: '蜚廉',
      // 流年将前12神
      jiangqian12: '岁驿',
      // 流年岁前12神
      suiqian12: '吊客',
      // 大限
      stage: { range: [44, 53], heavenlyStem: '' },
      // 小限
      ages: [9, 21, 33, 45, 57, 69, 81],
    },
    {
      name: '子女',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [
        { name: '太阳', type: 'major', scope: 'origin', brightness: '' },
        { name: '天梁', type: 'major', scope: 'origin', brightness: '' },
      ],
      minorStars: [],
      adjectiveStars: [{ name: '天刑', type: 'adjective', scope: 'origin' }],
      changsheng12: '',
      boshi12: '奏书',
      jiangqian12: '息神',
      suiqian12: '病符',
      stage: { range: [34, 43], heavenlyStem: '' },
      ages: [8, 20, 32, 44, 56, 68, 80],
    },
    {
      name: '夫妻',
      isBodyPalace: false,
      isOriginalPalace: true,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [{ name: '七杀', type: 'major', scope: 'origin', brightness: '' }],
      minorStars: [
        { name: '右弼', type: 'soft', scope: 'origin', brightness: '' },
        { name: '火星', type: 'tough', scope: 'origin', brightness: '' },
      ],
      adjectiveStars: [
        { name: '封诰', type: 'adjective', scope: 'origin' },
        { name: '华盖', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '',
      boshi12: '将军',
      jiangqian12: '华盖',
      suiqian12: '岁建',
      stage: { range: [24, 33], heavenlyStem: '' },
      ages: [7, 19, 31, 43, 55, 67, 79],
    },
    {
      name: '兄弟',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [{ name: '天机', type: 'major', scope: 'origin', brightness: '' }],
      minorStars: [],
      adjectiveStars: [
        { name: '天喜', type: 'flower', scope: 'origin' },
        { name: '天空', type: 'adjective', scope: 'origin' },
        { name: '孤辰', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '',
      boshi12: '小耗',
      jiangqian12: '劫煞',
      suiqian12: '晦气',
      stage: { range: [14, 23], heavenlyStem: '' },
      ages: [6, 18, 30, 42, 54, 66, 78],
    },
    {
      name: '命宫',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [{ name: '紫微', type: 'major', scope: 'origin', brightness: '' }],
      minorStars: [{ name: '文曲', type: 'soft', scope: 'origin', brightness: '' }],
      adjectiveStars: [
        { name: '年解', type: 'helper', scope: 'origin' },
        { name: '凤阁', type: 'adjective', scope: 'origin' },
        { name: '天福', type: 'adjective', scope: 'origin' },
        { name: '截路', type: 'adjective', scope: 'origin' },
        { name: '蜚廉', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '',
      boshi12: '青龙',
      jiangqian12: '灾煞',
      suiqian12: '丧门',
      stage: { range: [4, 13], heavenlyStem: '' },
      ages: [5, 17, 29, 41, 53, 65, 77],
    },
    {
      name: '父母',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [],
      minorStars: [
        { name: '天钺', type: 'soft', scope: 'origin', brightness: '' },
        { name: '陀罗', type: 'tough', scope: 'origin', brightness: '' },
      ],
      adjectiveStars: [
        { name: '天姚', type: 'flower', scope: 'origin' },
        { name: '空亡', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '帝旺',
      boshi12: '力士',
      jiangqian12: '天煞',
      suiqian12: '贯索',
      stage: { range: [114, 123], heavenlyStem: '' },
      ages: [4, 16, 28, 40, 52, 64, 76],
    },
    {
      name: '福德',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [
        { name: '破军', type: 'major', scope: 'origin', brightness: '' },
        { name: '禄存', type: 'lucun', scope: 'origin', brightness: '' },
      ],
      minorStars: [{ name: '文昌', type: 'soft', scope: 'origin', brightness: '' }],
      adjectiveStars: [
        { name: '龙池', type: 'adjective', scope: 'origin' },
        { name: '台辅', type: 'adjective', scope: 'origin' },
        { name: '旬空', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '临官',
      boshi12: '博士',
      jiangqian12: '指背',
      suiqian12: '官符',
      stage: { range: [104, 113], heavenlyStem: '' },
      ages: [3, 15, 27, 39, 51, 63, 75],
    },
    {
      name: '田宅',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [],
      minorStars: [
        { name: '地空', type: 'tough', scope: 'origin', brightness: '' },
        { name: '擎羊', type: 'tough', scope: 'origin', brightness: '' },
      ],
      adjectiveStars: [
        { name: '咸池', type: 'flower', scope: 'origin' },
        { name: '天贵', type: 'adjective', scope: 'origin' },
        { name: '月德', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '冠带',
      boshi12: '官府',
      jiangqian12: '咸池',
      suiqian12: '小耗',
      stage: { range: [94, 103], heavenlyStem: '' },
      ages: [2, 14, 26, 38, 50, 62, 74],
    },
    {
      name: '官禄',
      isBodyPalace: true,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [
        { name: '廉贞', type: 'major', scope: 'origin', brightness: '' },
        { name: '天府', type: 'major', scope: 'origin', brightness: '' },
      ],
      minorStars: [{ name: '左辅', type: 'soft', scope: 'origin', brightness: '' }],
      adjectiveStars: [
        { name: '天才', type: 'adjective', scope: 'origin' },
        { name: '天虚', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '沐浴',
      boshi12: '伏兵',
      jiangqian12: '月煞',
      suiqian12: '大耗',
      stage: { range: [84, 93], heavenlyStem: '' },
      ages: [1, 13, 25, 37, 49, 61, 73],
    },
    {
      name: '仆役',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [{ name: '太阴', type: 'major', scope: 'origin', brightness: '' }],
      minorStars: [],
      adjectiveStars: [
        { name: '红鸾', type: 'flower', scope: 'origin' },
        { name: '恩光', type: 'adjective', scope: 'origin' },
        { name: '天官', type: 'adjective', scope: 'origin' },
        { name: '天月', type: 'adjective', scope: 'origin' },
        { name: '天伤', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '长生',
      boshi12: '大耗',
      jiangqian12: '亡神',
      suiqian12: '龙德',
      stage: { range: [74, 83], heavenlyStem: '' },
      ages: [12, 24, 36, 48, 60, 72, 84],
    },
    {
      name: '迁移',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [{ name: '贪狼', type: 'major', scope: 'origin', brightness: '' }],
      minorStars: [{ name: '铃星', type: 'tough', scope: 'origin', brightness: '' }],
      adjectiveStars: [{ name: ' 八座', type: 'adjective', scope: 'origin' }],
      changsheng12: '',
      boshi12: '病符',
      jiangqian12: '将星',
      suiqian12: '白虎',
      stage: { range: [64, 73], heavenlyStem: '' },
      ages: [11, 23, 35, 47, 59, 71, 83],
    },
    {
      name: '疾厄',
      isBodyPalace: false,
      isOriginalPalace: false,
      heavenlyStem: '',
      earthlyBranch: '',
      majorStars: [
        { name: '天同', type: 'major', scope: 'origin', brightness: '' },
        { name: '巨门', type: 'major', scope: 'origin', brightness: '' },
      ],
      minorStars: [
        { name: '天魁', type: 'soft', scope: 'origin', brightness: '' },
        { name: '地劫', type: 'tough', scope: 'origin', brightness: '' },
      ],
      adjectiveStars: [
        { name: '天德', type: 'adjective', scope: 'origin' },
        { name: '寡宿', type: 'adjective', scope: 'origin' },
        { name: '破碎', type: 'adjective', scope: 'origin' },
        { name: '天使', type: 'adjective', scope: 'origin' },
      ],
      changsheng12: '',
      boshi12: '喜神',
      jiangqian12: '攀鞍',
      suiqian12: '天德',
      stage: { range: [54, 63], heavenlyStem: '' },
      ages: [10, 22, 34, 46, 58, 70, 82],
    },
  ],
}

方法定义

  • 通过阳历日期获取星盘信息

    astro.bySolar(solarDateStr, timeIndex, gender, fixLeap, language)

    • 参数

      参数类型是否必填默认值说明
      solarDateStrstringtrue-阳历日期【YYYY-M-D】
      timeIndexnumbertrue-出生时辰序号【0~12】,对应从早子时(0)一直到晚子时(12)的序号
      genderstringtrue-性别【男/女】
      fixLeapbooleanfalsetrue是否调整闰月,为true闰月的前半个月算上个月,后半个月算下个月
      languageLanguagefalsezh-CN返回数据将被国际化为指定语言。目前支持 zh-CN,zh-TW,en-US,ko-KRja-JP
    • 返回值

      FunctionalAstrolabe

  • 通过农历日期获取星盘信息

    astro.byLunar(lunarDateStr, timeIndex, gender, isLeapMonth, fixLeap, language)

    • 参数

      参数类型是否必填默认值说明
      lunarDateStrstringtrue-农历日期【YYYY-M-D】,例如2000年七月十七则传入2000-7-17
      timeIndexnumbertrue-出生时辰序号【0~12】,对应从早子时(0)一直到晚子时(12)的序号
      genderstringtrue-性别【男/女】
      isLeapMonthbooleanfalsefalse是否闰月,当实际月份没有闰月时该参数不生效
      fixLeapbooleanfalsetrue是否调整闰月,为true闰月的前半个月算上个月,后半个月算下个月
      languageLanguagefalsezh-CN返回数据将被国际化为指定语言。目前支持 zh-CN,zh-TW,en-US,ko-KRja-JP
    • 返回值

      FunctionalAstrolabe

获取运限

紫微斗数的运限分为 大限流年流月流日流时流分流秒,由于 流分流秒 使用场景不多,所以我们暂时不提供。 大限流年流月流日流时 已经能满足绝大部分需求和使用场景了,使用 iztro 能够很轻松的获取到这些数据。

ts
import { astro } from "iztro";

// 通过阳历获取星盘信息
const astrolabe = astro.bySolar("2000-8-16", 2, "");

// 获取运限数据
astrolabe.horoscope(new Date());

调用 astrolabe.horoscope() 方法以后你会获得如下数据

horoscope() 方法返回数据
ts
{
  solarDate: "2023-8-28"
  lunarDate: "二〇二三年七月十三"
  decadal: {
    index: 2
    heavenlyStem: ""
    earthlyBranch: ""
    palaceNames: ["夫妻", "兄弟", "命宫", "父母", "福德", "田宅", "官禄", "仆役", "迁移", "疾厄", "财帛", "子女"]
    mutagen: ["太阳", "武曲", "太阴", "天同"]
    stars: [{name: "运马", type: "tianma", scope: "decadal"}], …]
    age: {
      index: 10
      nominalAge: 23
    }
  },
  yearly: {
    index: 1
    heavenlyStem: ""
    earthlyBranch: ""
    palaceNames: ["兄弟", "命宫", "父母", "福德", "田宅", "官禄", "仆役", "迁移", "疾厄", "财帛", "子女", "夫妻"]
    mutagen: ["破军", "巨门", "太阴", "贪狼"]
    stars: [[], [{name: "流魁", type: "soft", scope: "yearly"}, …], [], …]
  },
  monthly: {
  index: 3
    heavenlyStem: ""
    earthlyBranch: ""
    palaceNames: ["子女", "夫妻", "兄弟", "命宫", "父母", "福德", "田宅", "官禄", "仆役", "迁移", "疾厄", "财帛"]
    mutagen: ["太阳", "武曲", "太阴", "天同"]
  },
  daily: {
    index: 3
    heavenlyStem: ""
    earthlyBranch: ""
    palaceNames: ["子女", "夫妻", "兄弟", "命宫", "父母", "福德", "田宅", "官禄", "仆役", "迁移", "疾厄", "财帛"]
    mutagen: ["贪狼", "太阴", "右弼", "天机"]
  },
  hourly: {
    index: 3
    heavenlyStem: ""
    earthlyBranch: ""
    palaceNames: ["子女", "夫妻", "兄弟", "命宫", "父母", "福德", "田宅", "官禄", "仆役", "迁移", "疾厄", "财帛"]
    mutagen: ["天梁", "紫微", "左辅", "武曲"]
  }
}

Tips: 只有 大限流年 有流耀。上面的运限数据和你调用的会因为传入的时间参数不同而不同,但是结构上是一致的。

方法定义

  • 获取当前星盘的运限信息

    astrolabe.horoscope(date, timeIndex)

    • 参数

      参数类型是否必填默认值说明
      datestring | Datefalsenew Date()阳历日期字符串或日期对象,若时间字符串或日期对象中包含了小时的信息,timeIndex可以省略
      timeIndexnumberfalse0时辰序号,若不传该参数则会尝试从date里获取小时信息转化为时辰序号
    • 返回值

      Horoscope

获取流耀

上面的horoscope()方法内已经包含了大限流年的流耀,所以一般情况下无需在单独调用获取流耀的方法,但也有例外的情况需要自行获取流耀,那就需要调用下列方法自行获取。

ts
import { star } from "iztro";

// 通过天干地支获取流耀
const horoscopeStars = star.getHoroscopeStar("", "", "decadal");

调用 star.getHoroscopeStar() 方法以后你会获得如下数据

getHoroscopeStar() 方法返回数据
ts
[
  [{ name: "运马", type: "tianma", scope: "decadal" }],
  [{ name: "运曲", type: "soft", scope: "decadal" }],
  [],
  [{ name: "运喜", type: "flower", scope: "decadal" }],
  [],
  [
    { name: "运钺", type: "soft", scope: "decadal" },
    { name: "运陀", type: "tough", scope: "decadal" },
  ],
  [{ name: "运禄", type: "lucun", scope: "decadal" }],
  [{ name: "运羊", type: "tough", scope: "decadal" }],
  [],
  [
    { name: "运昌", type: "soft", scope: "decadal" },
    { name: "运鸾", type: "flower", scope: "decadal" },
  ],
  [],
  [{ name: "运魁", type: "soft", scope: "decadal" }],
];

方法定义

  • 通过 天干地支 获取流耀

    star.getHoroscopeStar(heavenlyStem, earthlyBranch, scope)

    • 参数

      参数类型是否必填默认值说明
      heavenlyStemHeavenlyStemNametrue-天干
      earthlyBranchEarthlyBranchNametrue-地支
      scope'decadal' | 'yearly'true-限定是大限还是流年的流耀,其中大限流耀会在星曜前面加上,流年流耀会在星曜前面加上年解比较特殊,只会出现在流年的流耀里
    • 返回值

      Star[][]

☕ 总结

如果您觉得本程序对您有用的话,可以给我带杯咖啡吗?👍 Paypal Me

以上数据可以生成如下星盘,其中 palaces 数据用于填充 12 宫,其他数据用于填充中宫。图片中流耀的显示和实际上有偏差,那是因为图片是古早以前的一个版本生成的,请以实际返回数据为准。

demo

📜 版权

MIT License

Copyright © 2023 Sylar Long

请合理使用本开源代码,禁止用于非法目的。