Skip to main content

lesscode_charts是数据图标转换工具

Project description

lesscode_charts

lesscode_charts 是数据图表转换工具库,用于将后端原始数据(列表、字典等)转换为前端图表组件所需的标准 JSON 格式。

安装

pip install lesscode_charts
pip install -U lesscode_charts  # 升级到最新版本

版本说明

  • v1:基础版本,chart_type 使用语义化短名称(如 histogram
  • v2:重构版本,chart_type 按坐标系类型统一命名(如 SingeCartesianCoordinatesNoCoordinateSystemSingle),并新增公共工具函数

v1 图表模块

柱状图 / 折ramChart)

format_data — 构造系列数据项

入参说明:

参数 类型 默认值 说明
data dict / list 必填 原始数据,支持 {"2020":100} / {"2020":{"count":100}} / [{"name":"2020","value":100}] 三种格式
data_key str "" 当 data 值为 dict 时,取值所用的 key
name_key str "" 当 data 为 list 时,x 轴名称所用的 key
unit str "" 数据单位
name str "" 系列名称
default any "" 数据缺失时的默认值
value_func callable None 对每个值做二次处理的函数
chart_type str "bar" 图形类型,bar 柱图 / line 折线图
**kwargs - - 其他扩展字段,会合并到返回 dict 中

返回值: dict,供 liistogramdata 参数使用。


list2single_histogram — 单系列柱状图 / 折线图

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 原始数据,示例:[{"key":"2020","value":10}]
title str "" 图表标题
unit str "" 数据单位
x_name str "" x 轴名称
y_name str "" y 轴名称
x_key str "key" data 中作为 x 轴的字段名
y_key str "value" data 中作为 y 轴的字段名
chart_type str "bar" 图形类型,bar 柱图 / line 折线图
way any None 扩展属性,原样写入 series(仅 v1)
**kwar- 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "histogram"
title str 图表标题
xName str x 轴名称
yName str y 轴名称
x list x 轴数据列表
series list 系列数据,每项包含 namedataunitindexisType、way`
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

list2histogram — 单 / 多系列柱状图 / 折线图(推荐)

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 format_data 构造的系列列表
title str "" 图表标题
x_name str "" x 轴名称
y_name str "" y 轴名称
x_index int None 指定以第几个系列的 x 轴作为全局 x 轴,默认合并所有
x_func callable None 对 x 轴列表做二次处理的函数
x_order bool / None None x 轴排序,False 正序,True 倒序,None 不排序
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "histogram"
title str 图表标题
xName str x 轴名称
yName str y 轴名称
x list 数据列表
series list 系列数据,每项包含 nameunittypeisTypedataxway
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

bar_chart — 字典转多柱

入参说明:

参数 类型 默认值 说明
data dict 必填 形如 {"INB1335":{"count":1,"sum":10}}
detail_list list 必填 系列配置列表,每项含 nameunitdata_key(支持 a&b 多级取值)、func(可选,值处理函数)

返回字段说明:

字段 类型 说明
chart_type str 固定值 "histogram"
x list x 轴数据(data 的 key 列表)
series list 系列数据,funcdata_key 字段已从每项中移除,每项新增 data 字段

示例:

from lesscode_charts.v1.histogram_chart import HistogramChart

# 单系列柱状图
result = HistogramChart.list2single_histogram(
    data=[{"key": "2020", "value": 10}, {"key": "2021", "value": 20}],
    title="年度数量", unit="个", x_name="年份", y_name="数量",
    x_key="key", y_key="value", chart_type="bar"
)

# 多系列(推荐写法)
item1 = HistogramChart.format_data(
    data={"2020": 100, "2021": 200}, name="系列1", unit="个", chart_type="bar"
)
item2 = HistogramChart.format_data(
    data={"2020": 50, "2021":}, name="系列2", unit="个", chart_type="line"
)
result = HistogramChart.list2histogram(
    data=[item1, item2], title="年度对比", x_name="年份", y_name="数量"
)

# 字典转多柱
result = HistogramChart.bar_chart(
    data={"INB1335": {"count": 1, "sum": 10}},
    detail_list=[
        {"name": "计数", "unit": "个", "data_key": "count", "func": int},
        {"name": "求和", "unit": "万", "data_key": "sum", "func": lambda x: x / 10000}
    ]
)

返回示例:

{
  "chart_type": "histogram",
  "xName": "年份",
  "yName": "数量",
  "title": "年度对比",
  "x": [
    "2020",
    "2021"
  ],
  "series": [
    {
      "name": "系列1",
      "data": [
        100,
        200
      ],
      "unit": "个",
      "type": "bar",
      "isType": "0",
      "index": 0,
      "way": null
    }
  ],
  "pool": {}
}

饼图(PieChart)

dict2pie — 字典转饼图

入参说明:

参数 类型 默认值 说明
data dict 必填 分组数据,示例1:{"A":{"count":1}} 示例2:{"A":1}
unit str "" 数据单位
data_key str "count" 当 data 值为 dict 时取值所用的 key,设为 None 则直接使用 data 值
total int None 计算占比的总数,默认自动累加
decimal_place int None 占比保留小数位,None 表示取整
title str "" 图表标题
other_data_key str None 附加字段 key,值写入每项的 other 字段
**kwargs - - 额外数据,写入 pool 字段

list2pie — 列表转饼图

入参说明:

参数 类型 默认值 说明
data list 必填 分组数据,示例:[{"name":"A","count":10}]
unit str "" 数据单位
name_key str "name" 名称字段 key
data_key str "count" 数值字段 key
total int None 计算占比的总数,默认自动累加
decimal_place int None 占比保留小数位,None 表示取整
title str "" 图表标题
other_data_key str None 附加字段 key,值写入每项的 other 字段
**kwargs - - 额外数据,写入 pool 字段

返回字段说明(两个方法相同):

字段 类型 说明
chart_type str 固定值 "pie"
title str 图表标题
series list 单元素列表,包含 nameunitdata
series[0].data list 每项含 namevalueunitproportion(占比)、other
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.pie_chart import PieChart

# 字典转饼图
result = PieChart.dict2pie(
    data={"人工智能": {"count": 10}, "工业制造": {"count": 20}},
    unit="家", data_key="count", title="行业分布", decimal_place=2
)

# 列表转饼图
result = PieChart.list2pie(
    data=[{"name": "人工智能", "count": 10}, {"name": "工业制造", "count": 20}],
    unit="家", name_key="name", data_key="count", title="行业分布", decimal_place=2
)

返回示例:

{
  "chart_type": "pie",
  "title": "行业分布",
  "series": [
    {
      "name": "行业分布",
      "data": [
        {
          "name": "人工智能",
          "value": 10,
          "unit": "家",
          "proportion": 33.33,
          "other": null
        },
        {
          "name": "工业制造",
          "value": 20,
          "unit": "家",
          "proportion": 66.67,
          "other": null
        }
      ],
      "unit": "家"
    }
  ],
  "pool": {}
}

表格(TableChart)

依赖:pip install lesscode_utils

list2table_with_page — 列表转分页表格

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 列表数据
head dict / list 必填 表头配置,dict 示例:{"企业名称":"name"};list 示例:[{"title":"企业名称","key":"name"}]
title str "" 表格标题
total int 0 数据总条数
count int 0 页面展示总数
index_enable bool False 是否显示序号列
index_name str "序号" 序号列表头名称
index_key str "index" 序号列字段名
page_num int 1 当前页码(用于计算序号起始值)
page_size int 10 每页条数
column_covert dict None 字段类型转换配置,示例:{"id": str}
column_keys dict None 字段映射配置,支持多级取值(用 . 分隔),示例:{"name":"basic.name"}
custom_head_enable bool False True 时直接使用 head 作为 columns,不做解析
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "table"
title str 表格标题
columns list 表头列配置,每项含 titledataIndexkey
dataSource list 表格数据行列表
total int 数据总条数
count int 页面展示总数
auth_count int 同 total
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.table_chart import TableChart

result = TableChart.list2table_with_page(
    data=[{"name": "张三", "age": 18}],
    head={"姓名": "name", "年龄": "age"},
    title="用户列表",
    total=100, count=100,
    page_num=1, page_size=10,
    index_enable=True
)

返回示例:

{
  "chart_type": "table",
  "title": "用户列表",
  "columns": [
    {
      "title": "序号",
      "dataIndex": "index",
      "key": "index"
    },
    {
      "title": "姓名",
      "dataIndex": "name",
      "key": "name"
    },
    {
      "title": "年龄",
      "dataIndex": "age",
      "key": "age"
    }
  ],
  "dataSource": [
    {
      "index": 1,
      "name": "张三",
      "age": 18
    }
  ],
  "total": 100,
  "count": 100,
  "auth_count": 100
}

雷达图(RadarChart)

radar

入参说明:

参数 类型 默认值 说明
indicator_list List[str] 必填 指标名称列表,如 ["速度","耐力","力量"]
data_list List[dict] 必填 数据列表,每项包含名称字段和值字段
title str 必填 图表标题
unit_list List[str] 必填 各指标单位列表,如 ["km/h","min","kg"]
name_key str "name" data_list 中名称字段的 key
value_key str "value" data_list 中值字段的 key,值应为与 indicator_list 等长的列表
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "radar"
indicator list 指标名称列表
title str 图表标题
series list 单元素列表,包含 nameunitdata
series[0].data list 每项含 name(系列名)和 value(各指标值列表)
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.radar_chart import RadarChart

result = RadarChart.radar(
    indicator_list=["速度", "耐力", "力量"],
    data_list=[{"name": "运动员A", "value": [80, 90, 70]}],
    title="综合能力",
    unit_list=["km/h", "min", "kg"]
)

返回示例:

{
  "chart_type": "radar",
  "indicator": [
    "速度",
    "耐力",
    "力量"
  ],
  "series": [
    {
      "name": "综合能力",
      "data": [
        {
          "name": "运动员A",
          "value": [
            80,
            90,
            70
          ]
        }
      ],
      "unit": [
        "km/h",
        "min",
        "kg"
      ]
    }
  ],
  "title": "综合能力"
}

散点图(ScatterChart)

scatter

入参说明:

参数 类型 默认值 说明
data list 必填 系列数据列表,每项含 namedata(点列表)、unit
x_name str "" x 轴名称
y_name str "" y 轴名称
title str "" 图表标题
**kwargs - - 额外数据,写入 pool 字段

每个点格式:{"name": "点名称", "value": [经度, 纬度, 数值]}

返回字段说明:

字段 类型 说明
chart_type str 固定值 "scatter"
xName str x 轴名称
yName str y 轴名称
title str 图表标题
series list 原样透传的系列数据
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.scatter_chart import ScatterChart

result = ScatterChart.scatter(
    data=[
        {"name": "系列1", "data": [{"name": "点1", "value": [116.4, 39.9, 100]}], "unit": "个"}
    ],
    x_name="经度", y_name="纬度", title="散点分布"
)

返回示例:

{
  "chart_type": "scatter",
  "xName": "经度",
  "yName": "纬度",
  "title": "散点分布",
  "series": [
    {
      "name": "系列1",
      "data": [
        {
          "name": "点1",
          "value": [
            116.4,
            39.9,
            100
          ]
        }
      ],
      "unit": "个"
    }
  ]
}

关系图(RelationChart)

relation_chart

入参说明:

参数 类型 默认值 说明
data list 必填 系列列表,每项含 namedata(节点列表)、links(连线列表)、unit
x_name str "" x 轴名称
y_name str "" y 轴名称
title str "" 图表标题(未写入返回值,仅占位)
**kwargs - - 额外数据,写入 pool 字段

节点格式:{"name": "节点名", "value": 数值} 连线格式:{"source": "节点名", "target": "节点名", "value": 权重, "label": "关系名称"}

返回字段说明:

字段 类型 说明
chart_type str 固定值 "relation"
xName str x 轴名称
yName str y 轴名称
series list 原样透传的系列数据
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.relation_chart import RelationChart

result = RelationChart.relation_chart(
    data=[
        {
            "name": "系列1",
            "data": [{"name": "A", "value": 2}, {"name": "B", "value": 3}],
            "links": [{"source": "A", "target": "B", "value": 10, "label": "关联"}],
            "unit": "个"
        }
    ],
    x_name="", y_name="", title="关系图"
)

返回示例:

{
  "chart_type": "relation",
  "xName": "",
  "yName": "",
  "series": [
    {
      "name": "系列1",
      "data": [
        ...
      ],
      "links": [
        ...
      ],
      "unit": "个"
    }
  ]
}

桑基图(SankeyChart)

sankey

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 含父子关系的节点列表,每项需有 id 字段、parent_id 字段和 config 字段
name str "" 系列名称
title str "" 图表标题
data_key str "id" 节点唯一标识字段名
parent_key str "parent_id" 父节点字段名,根节点该字段值为 None
**kwargs - - 额外数据,写入 pool 字段

config 字段格式:{"label": "显示名称", "value": 流量值, "depth": 层级}

返回字段说明:

字段 类型 说明
chart_type str 固定值 "sankey"
title str 图表标题
series list 单元素列表,含 namedata(节点列表)、links(连线列表)
series[0].data list 节点列表,每项含 name 及 config 中的字段
series[0].links list 连线列表,每项含 sourcetarget,有流量时含 value
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.sankey_chart import SankeyChart

result = SankeyChart.sankey(
    data=[
        {"id": "A", "config": {"label": "节点A", "depth": 1}, "parent_id": None},
        {"id": "B", "config": {"label": "节点B", "value": 10, "depth": 2}, "parent_id": "A"}
    ],
    title="桑基图", data_key="id", parent_key="parent_id"
)

返回示例:

{
  "chart_type": "sankey",
  "title": "桑基图",
  "series": [
    {
      "name": "B",
      "data": [
        {
          "name": "A",
          "label": "节点A",
          "depth": 1
        },
        {
          "name": "B",
          "label": "节点B",
          "value": 10,
          "depth": 2
        }
      ],
      "links": [
        {
          "source": "A",
          "target": "B",
          "value": 10
        }
      ]
    }
  ]
}

森林 / 树图(ForestChart)

依赖:pip install lesscode_utils

forest

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 含父子关系的节点列表
key str "" 节点唯一标识字段名(如 "id"
parent_key str "" 父节点字段名(如 "parent_id"),根节点该字段值为 None
title str "" 图表标题
**kwargs - - 额外数据,写入 pool 字段

节点字段建议包含:idnamedepthvalueunitparent_id

返回字段说明:

字段 类型 说明
chart_type str 固定值 "forest"
title str 图表标题
series list 树形结构数据,由 lesscode_utils.find_child 生成
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.forest_chart import ForestChart

result = ForestChart.forest(
    data=[
        {"id": "1", "name": "根节点", "depth": 0, "value": 10, "unit": "个", "parent_id": None},
        {"id": "2", "name": "子节点", "depth": 1, "value": 5, "unit": "个", "parent_id": "1"}
    ],
    key="id", parent_key="parent_id", title="树图"
)

返回示例:

{
  "chart_type": "forest",
  "title": "树图",
  "series": [
    {
      "id": "1",
      "name": "根节点",
      "children": [
        {
          "id": "2",
          "name": "子节点"
        }
      ]
    }
  ]
}

矩形树图 / 旭日图(RectChart)

依赖:pip install lesscode_utils

render_rect_chart

入参说明:

参数 类型 默认值 说明
data list 必填 含父子关系的节点列表
key str 必填 节点唯一标识字段名
parent_key str 必填 父节点字段名,根节点该字段值为 None
unit str 必填 数据单位
name str "" 系列名称
title str "" 图表标题
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "rect"
title str 图表标题
series dict 包含 namedata(树形结构)、unit
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.rect_chart import RectChart

result = RectChart.render_rect_chart(
    data=[
        {"id": "1", "name": "根", "value": 100, "parent_id": None},
        {"id": "2", "name": "子A", "value": 60, "parent_id": "1"}
    ],
    key="id", parent_key="parent_id", unit="个", name="矩形树图", title="矩形树图"
)

返回示例:

{
  "chart_type": "rect",
  "title": "矩形树图",
  "series": {
    "name": "矩形树图",
    "data": [
      {
        "id": "1",
        "name": "根",
        "value": 100,
        "children": [
          ...
        ]
      }
    ],
    "unit": "个"
  }
}

热力图(HeatMapChart)

依赖:pip install coord-convert==0.2.1 sklearn==0.0

new_heat_map — 一站式热力图生成

入参说明:

参数 类型 默认值 说明
data_list List[dict] 必填 数据列表,每项含坐标字段
date_key str "reg_xy" 坐标字段名,值为 [经度, 纬度]
title str "" 图表标题
method str "wgs2bd" 坐标转换方式,可选:wgs2gcjwgs2bdgcj2wgsgcj2bdbd2wgsbd2gcj
min_samples int 1 DBSCAN 聚类核心点最小邻域样本数
eps float 0.0001 DBSCAN 聚类邻域半径
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "heat_map"
title str 图表标题
data list 聚类后的热力点列表,每项含 value: [经度均值, 纬度均值, 点数量]
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.heat_map_chart import HeatMapChart

result = HeatMapChart.new_heat_map(
    data_list=[
        {"id": 1, "name": "北京上奇", "reg_xy": [116.4, 39.9]},
        {"id": 2, "name": "北京分部", "reg_xy": [116.41, 39.91]}
    ],
    date_key="reg_xy", title="热力图", method="wgs2bd",
    min_samples=1, eps=0.001
)

返回示例:

{
  "chart_type": "heat_map",
  "title": "热力图",
  "data": [
    {
      "value": [
        116.405,
        39.905,
        2
      ]
    }
  ]
}

K 线图(KLineChart)

k_line

入参说明:

参数 类型 默认值 说明
data List[Any] 必填 K 线数据,每项为 [开盘价, 收盘价, 最低价, 最高价]
x List[Any] 必填 x 轴时间序列
y List[Any] 必填 y 轴刻度区间列表
unit str "" 数据单位
title str "" 图表标题
name str "" 系列名称
x_name str "" x 轴名称
y_name str "" y 轴名称
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "k_line"
xName str x 轴名称
yName str y 轴名称
title str 图表标题
x list x 轴时间序列
y list y 轴刻度区间
series list 单元素列表,含 namedataunit
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.k_line_chart import KLineChart

result = KLineChart.k_line(
    data=[[20, 34, 10, 38], [30, 25, 15, 35]],
    x=["2024-01", "2024-02"],
    y=[10, 20, 30, 40],
    unit="元", title="股价走势",
    name="某股票", x_name="月份", y_name="价格"
)

返回示例:

{
  "chart_type": "k_line",
  "xName": "月份",
  "yName": "价格",
  "title": "股价走势",
  "x": [
    "2024-01",
    "2024-02"
  ],
  "y": [
    10,
    20,
    30,
    40
  ],
  "series": [
    {
      "name": "某股票",
      "data": [
        [
          20,
          34,
          10,
          38
        ],
        [
          30,
          25,
          15,
          35
        ]
      ],
      "unit": "元"
    }
  ]
}

金字塔图(PyramidChart)

pyramid

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 数据列表,默认字段为 namevalue
unit str "" 数据单位
title str "" 图表标题
decimal_place int 2 占比保留小数位
total int 0 计算占比的总数,默认自动累加
name_key str "name" 名称字段 key(仅 v1)
value_key str "value" 数值字段 key(仅 v1)
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "pyramid"
title str 图表标题
series list 单元素列表,含 nameunitdata
series[0].data list 每项含 namevalueproportion(占比)
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.pyramid_chart import PyramidChart

result = PyramidChart.pyramid(
    data=[{"name": "上市企业", "value": 100}, {"name": "规上企业", "value": 500}],
    unit="家", title="企业层级", decimal_place=2,
    name_key="name", value_key="value"
)

返回示例:

{
  "chart_type": "pyramid",
  "title": "企业层级",
  "series": [
    {
      "name": "企业层级",
      "data": [
        {
          "name": "上市企业",
          "value": 100,
          "proportion": 16.67
        },
        {
          "name": "规上企业",
          "value": 500,
          "proportion": 83.33
        }
      ],
      "unit": "家"
    }
  ]
}

地图色块图(UnivalentGraphChart)

UnivalentGraph

入参说明:

参数 类型 默认值 说明
data_list list 必填 地图数据列表,每项含 namevalueunitcenter_latitudecenter_longitude
title str 必填 图表标题
name str "" 系列名称
unit str "" 数据单位
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "univalent_graph"
title str 图表标题
series list 单元素列表,含 namedataunit
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.univalent_graph_chart import UnivalentGraphChart

result = UnivalentGraphChart.UnivalentGraph(
    data_list=[
        {
            "name": "长沙市", "value": 1115890, "unit": "家",
            "center_latitude": 28.19, "center_longitude": 112.98
        }
    ],
    title="湖南企业分布", name="企业数", unit="家"
)

返回示例:

{
  "chart_type": "univalent_graph",
  "title": "湖南企业分布",
  "series": [
    {
      "name": "企业数",
      "data": [
        {
          "name": "长沙市",
          "value": 1115890,
          "unit": "家",
          "center_latitude": 28.19,
          "center_longitude": 112.98
        }
      ],
      "unit": "家"
    }
  ]
}

词云(WordCloudChart)

word_cloud_chart

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 词云数据列表,每项含 name(词语)和 value(权重)
title str "" 图表标题
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "word_cloud"
title str 图表标题
data list 原样透传的词云数据
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.word_cloud_chart import WordCloudChart

result = WordCloudChart.word_cloud_chart(
    data=[
        {"name": "气压信号", "value": 2},
        {"name": "织物张力", "value": 2}
    ],
    title="关键词"
)

返回示例:

{
  "chart_type": "word_cloud",
  "title": "关键词",
  "data": [
    {
      "name": "气压信号",
      "value": 2
    },
    {
      "name": "织物张力",
      "value": 2
    }
  ]
}

布局图(LayoutChart)— 仅 v1

layout_chart

入参说明:

参数 类型 默认值 说明
data list 必填 系列列表,每项含 namecategoriesdata(节点列表)、links(连线列表)、unit
title str "" 图表标题
**kwargs - - 额外数据,写入 pool 字段

节点格式:{"name": "节点名", "value": 数值, "x": x坐标, "y": y坐标, "category": 类目索引} 连线格式:{"source": "节点名", "target": "节点名", "value": 权重, "label": "关系名"}

返回字段说明:

字段 类型 说明
chart_type str 固定值 "layout"
title str 图表标题
series list 原样透传的系列数据
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.layout_chart import LayoutChart

result = LayoutChart.layout_chart(
    data=[
        {
            "name": "系列1",
            "categories": ["A", "B"],
            "data": [
                {"name": "1", "value": 1, "x": 10, "y": 10, "category": 0},
                {"name": "2", "value": 2, "x": 20, "y": 20, "category": 1}
            ],
            "links": [{"source": "1", "target": "2", "value": 10, "label": "关联"}],
            "unit": ["个"]
        }
    ],
    title="布局图"
)

返回示例:

{
  "chart_type": "layout",
  "title": "布局图",
  "series": [
    {
      "name": "系列1",
      "categories": [
        "A",
        "B"
      ],
      "data": [
        ...
      ],
      "links": [
        ...
      ],
      "unit": [
        "个"
      ]
    }
  ]
}

列表(ListChart)— 仅 v1

list_chart

入参说明:

参数 类型 默认值 说明
columns list / dict 必填 表头配置,dict 示例:{"姓名":"name"};list 示例:[{"title":"姓名","dataIndex":"name"}]
data list 必填 列表数据
total int 0 数据总条数
count int 0 页面展示总数
page_num int 1 当前页码(用于计算序号起始值)
page_size int 10 每页条数
index_enable bool False 是否显示序号列
index_name str "序号" 序号列表头名称
index_key str "index" 序号列字段名
title str "" 列表标题
**kwargs - - 额外数据,写入 pool 字段

返回字段说明:

字段 类型 说明
chart_type str 固定值 "list"
title str 列表标题
columns list 表头配置列表,每项含 titledataIndexkey
data list 表格数据行列表
total int 数据总条数
count int 页面展示总数
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v1.list_chart import ListChart

result = ListChart.list_chart(
    columns={"姓名": "name", "年龄": "age"},
    data=[{"name": "张三", "age": 18}],
    total=100, count=100,
    page_num=1, page_size=10,
    index_enable=True, title="用户列表"
)

返回示例:

{
  "chart_type": "list",
  "title": "用户列表",
  "columns": [
    {
      "title": "序号",
      "dataIndex": "index",
      "key": "index"
    },
    {
      "title": "姓名",
      "dataIndex": "name",
      "key": "name"
    },
    {
      "title": "年龄",
      "dataIndex": "age",
      "key": "age"
    }
  ],
  "data": [
    {
      "index": 1,
      "name": "张三",
      "age": 18
    }
  ],
  "total": 100,
  "count": 100
}

v2 图表模块

v2 对 chart_type 按坐标系类型进行了统一规范,接口整体与 v1 一致,以下仅说明差异点。

chart_type 对照表

图表类型 v1 chart_type v2 chart_type
柱状图 / 折线图 histogram SingeCartesianCoordinates
饼图 / 词云 / 金字塔 pie / word_cloud / pyramid NoCoordinateSystemSingle
雷达图 radar NoCoordinateSystemMore
关系图 relation NoCartesianCoordinatesRelation
桑基图 / 森林树图 sankey / forest NoCoordinateSystemTree
矩形树图 / 旭日图 rect NoCoordinateSystemRect
K 线图 k_line Candlestick
热力图 / 散点图 heat_map / scatter GeographicCoordinateSystem
地图色块图 univalent_graph NoCoordinateSystemSingleMap
表格 table Table

柱状图 / 折线图(HistogramChart)— v2

接口与 v1 完全一致,差异如下:

  • chart_type 返回 "SingeCartesianCoordinates"
  • isType 字段和 way 字段已从 series 中移除
  • format_datachart_type 默认值改为 "Bar"

饼图(PieChart)— v2

在 v1 基础上新增 is_standard_value 参数,并修复最后一项比例精度问题。

新增 / 变更入参:

参数 类型 默认值 说明
decimal_place int 2 占比保留小数位(v1 默认为 None)
is_standard_value bool False True 时对 value 应用千位分隔格式化

比例精度修复: 最后一项占比 = 100 - 前面所有项占比之和,避免浮点误差导致总和不等于 100%。

示例:

from lesscode_charts.v2.pie_chart import PieChart

result = PieChart.dict2pie(
    data={"人工智能": {"count": 10}, "工业制造": {"count": 20}},
    unit="家", data_key="count", title="行业分布",
    decimal_place=2, is_standard_value=True
)
# chart_type -> "NoCoordinateSystemSingle"

词云(WordCloudChart)— v2

数据结构从扁平 data 列表改为 series 格式,并新增 proportion 字段。

返回字段变化:

字段 v1 v2
chart_type word_cloud NoCoordinateSystemSingle
数据位置 data 列表 series[0].data 列表
proportion 每项新增,值为百分比(保留 2 位)

示例:

from lesscode_charts.v2.word_cloud_chart import WordCloudChart

result = WordCloudChart.word_cloud_chart(
    data=[
        {"name": "气压信号", "value": 2},
        {"name": "织物张力", "value": 2}
    ],
    title="关键词"
)

返回示例:

{
  "chart_type": "NoCoordinateSystemSingle",
  "title": "关键词",
  "series": [
    {
      "name": "关键词",
      "data": [
        {
          "name": "气压信号",
          "value": 2,
          "proportion": 50.0
        },
        {
          "name": "织物张力",
          "value": 2,
          "proportion": 50.0
        }
      ]
    }
  ]
}

表格(TableChart)— v2

不依赖 lesscode_utils,提供两个轻量接口,序号由内部 format_list_index_rank 自动处理。

list2table

入参说明:

参数 类型 默认值 说明
dataSource List[dict] 必填 列表数据(会原地写入序号字段)
columns list 必填 表头配置列表,每项含 titledataIndex
total int 0 数据总条数
count int 0 页面展示总数
offset int 0 序号起始偏移量,序号从 offset+1 开始
sum int 0 汇总值

list2table_v2

入参说明:

参数 类型 默认值 说明
data List[dict] 必填 列表数据(会原地写入序号字段)
columns list / dict 必填 表头配置,dict 示例:{"企业名称":"name"};list 示例:[{"title":"企业名称","dataIndex":"name"}]
total int 0 数据总条数
count int 0 页面展示总数
page_number int 0 当前页码(用于计算序号偏移,偏移 = (page_number-1)*page_size)
page_size int 0 每页条数
sum int 0 汇总值

返回字段说明(两个方法相同):

字段 类型 说明
chart_type str 固定值 "Table"
columns list 表头列配置列表
dataSource list 表格数据行,每行新增 index 序号字段(字符串类型)
total int 数据总条数
count int 页面展示总数
sum int 汇总值

示例:

from lesscode_charts.v2.table_chart import TableChart

# 基础接口
result = TableChart.list2table(
    dataSource=[{"name": "张三", "age": 18}],
    columns=[{"title": "姓名", "dataIndex": "name"}, {"title": "年龄", "dataIndex": "age"}],
    total=100, count=100, offset=0
)

# 分页接口
result = TableChart.list2table_v2(
    data=[{"name": "张三", "age": 18}],
    columns={"姓名": "name", "年龄": "age"},
    total=100, count=100,
    page_number=2, page_size=10
)
# 第2页时序号从11开始

返回示例:

{
  "chart_type": "Table",
  "columns": [
    {
      "title": "姓名",
      "dataIndex": "name"
    },
    {
      "title": "年龄",
      "dataIndex": "age"
    }
  ],
  "dataSource": [
    {
      "index": "1",
      "name": "张三",
      "age": 18
    }
  ],
  "total": 100,
  "count": 100,
  "sum": 0
}

关系图(RelationChart)— v2

接口签名从接受 list 改为接受单个 dict,数据结构扁平化。

入参说明:

参数 类型 默认值 说明
data dict 必填 关系图数据,含 namenodeslinksunit
x_name str "" x 轴名称
y_name str "" y 轴名称
title str "" 图表标题
**kwargs - - 额外数据,写入 pool 字段

nodes 格式:[{"name": "节点名", "value": 数值}] links 格式:[{"source": "节点名", "target": "节点名", "value": 权重, "label": "关系名"}]

返回字段说明:

字段 类型 说明
chart_type str 固定值 "NoCartesianCoordinatesRelation"
title str 图表标题
xName str x 轴名称
yName str y 轴名称
name str 系列名称(取自 data.name)
nodes list 节点列表
links list 连线列表
unit str 数据单位
pool dict 额外透传数据(仅当 kwargs 不为空时存在)

示例:

from lesscode_charts.v2.relation_chart import RelationChart

result = RelationChart.relation_chart(
    data={
        "name": "关系图",
        "nodes": [{"name": "A", "value": 2}, {"name": "B", "value": 3}],
        "links": [{"source": "A", "target": "B", "value": 10, "label": "关联"}],
        "unit": "个"
    },
    title="知识图谱"
)

返回示例:

{
  "chart_type": "NoCartesianCoordinatesRelation",
  "title": "知识图谱",
  "xName": "",
  "yName": "",
  "name": "关系图",
  "nodes": [
    {
      "name": "A",
      "value": 2
    },
    {
      "name": "B",
      "value": 3
    }
  ],
  "links": [
    {
      "source": "A",
      "target": "B",
      "value": 10,
      "label": "关联"
    }
  ],
  "unit": "个"
}

散点图(ScatterChart)— v2

接口大幅简化,专注于地理坐标系,去掉 xNameyName 参数。

入参说明:

参数 类型 默认值 说明
data list 必填 点数据列表,每项格式为 {"name": "点名", "value": [经度, 纬度, 数值]}
title str "" 图表标题

返回字段说明:

字段 类型 说明
chart_type str 固定值 "GeographicCoordinateSystem"
title str 图表标题
series list 单元素列表,含 data(点数据列表)

示例:

from lesscode_charts.v2.scatter_chart import ScatterChart

result = ScatterChart.scatter(
    data=[
        {"name": "北京上奇", "value": [116.4, 39.9, 100]}
    ],
    title="地图散点"
)

返回示例:

{
  "chart_type": "GeographicCoordinateSystem",
  "title": "地图散点",
  "series": [
    {
      "data": [
        {
          "name": "北京上奇",
          "value": [
            116.4,
            39.9,
            100
          ]
        }
      ]
    }
  ]
}

公共工具(common)— 仅 v2

standard_format_value — 数字标准化格式化

入参说明:

参数 类型 默认值 说明
value int / float / str / list 必填 待格式化的值,传入 list 时递归处理每个元素
delimiter bool False 是否添加千位分隔符,True 返回字符串,False 返回数值
round int 1 保留小数位数(通过 kwargs 传入)

返回值:

  • delimiter=True:返回带千位分隔符的字符串,如 "1,234,567.9"
  • delimiter=False:返回 int 或 float 数值,如 1234567.9
  • 传入 list 时:返回同结构列表

示例:

from lesscode_charts.v2.common import standard_format_value

standard_format_value(1234567.891, delimiter=True)  # -> "1,234,567.9"
standard_format_value(1234567.891, delimiter=False)  # -> 1234567.9
standard_format_value(-9876.5, delimiter=True)  # -> "-9,876.5"
standard_format_value([1000, 2000], delimiter=True)  # -> ["1,000", "2,000"]
standard_format_value(1234.5678, delimiter=False, round=2)  # -> 1234.57
standard_format_value(0, delimiter=True)  # -> "0"

可选依赖

功能模块 依赖包 安装命令
森林 / 树图、矩形树图 lesscode_utils pip install lesscode_utils
热力图坐标转换 coord_convert pip install coord-convert==0.2.1
热力图聚类算法 scikit-learn pip install sklearn==0.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lesscode_charts-0.0.71.tar.gz (47.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lesscode_charts-0.0.71-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

File details

Details for the file lesscode_charts-0.0.71.tar.gz.

File metadata

  • Download URL: lesscode_charts-0.0.71.tar.gz
  • Upload date:
  • Size: 47.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for lesscode_charts-0.0.71.tar.gz
Algorithm Hash digest
SHA256 ff692da5e1a32d2f4664950088f91fbff13122868247f0079122585deb38850b
MD5 e06becfed06721512b11794574e6f1cd
BLAKE2b-256 25a6015acda5e497cf099120e9d1e6bffd6c9244162495c5e8ed45336f6e688f

See more details on using hashes here.

File details

Details for the file lesscode_charts-0.0.71-py3-none-any.whl.

File metadata

File hashes

Hashes for lesscode_charts-0.0.71-py3-none-any.whl
Algorithm Hash digest
SHA256 e099d9ed247f824a9cce63e2c9db03c38139bc2e633161df0e00c3403b389302
MD5 2a73036fe690a9d9b7d4b172de91b945
BLAKE2b-256 f41d8b580a572ea41bdf9844721a3b7bd1152fb9ce8e7f84f88f214d7611ef0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page