Skip to main content

Bar

汇总

Bar 类可创建条形图。 条形图使用比例条柱长度来表示值以对分类数据进行汇总和比较。

了解有关 ArcGIS Pro 中的条形图的详细信息

讨论

创建此对象时,必须使用参数名指定类构造函数的可选参数;这些可选参数不能通过参数位置指定。 有关如何使用关键字指定参数的示例,请参阅代码示例部分。

语法

Bar(x, {y}, {aggregation}, {splitCategory}, {multiSeriesDisplay}, {miniChartsPerRow}, {showPreviewChart}, {showMovingAverage}, {movingAveragePeriod}, {timeBinningProperties}, {rotated}, {title}, {description}, {xTitle}, {yTitle}, {dataSource}, {displaySize}, {theme})

名称 说明 数据类型

x

x 轴变量的字段名称。 该字段必须为类别或日期字段。 如果指定空字符串,图表将 与图层符号匹配

String

y

(可选)

数值字段的字段名称。 提供一个字段名或一个字段名称列表。

String

aggregation

(可选)

用于指定将应用于聚合到每个条柱中的值的统计计算。

  • <None>—Each value will be plotted on the chart regardless of overlap or recurring values.

  • COUNT—The total number of records will be identified and applied.

  • SUM—The sum of all values will be calculated and applied.

  • MEAN—The mean of all values will be calculated and applied.

  • MEDIAN—The median of all values will be identified and applied.

  • MIN—The minimum value will be identified and applied.

  • MAX—The maximum value will be identified and applied.

String

splitCategory

(可选)

第二个类别字段,将为字段中每一个唯一值添加单独的 系列 或条柱。

String

multiSeriesDisplay

(可选)

指定具有多个系列的条形图的显示类型。

  • sideBySide—The series will be displayed side by side.

  • stacked—The series will be displayed stacked.

  • stacked100—The series will be displayed 100 percent stacked.

  • grid—The series will be displayed as a grid of mini charts.

String

miniChartsPerRow

(可选)

每行显示的迷你图表的数量。

Integer

showPreviewChart

(可选)

指定是否在格网图表中显示预览图表。

  • True—The preview chart will be displayed.

  • False—The preview chart will not be displayed.

(默认值为 False)

Boolean

showMovingAverage

(可选)

指定是否将针对时间条形图显示移动平均线。

  • True—The moving average line will be displayed.

  • False—The moving average line will be hidden.

(默认值为 False)

Boolean

movingAveragePeriod

(可选)

移动平均时间段大小。

Integer

timeBinningProperties

(可选)

应用于时间条形图的时间图格属性。 需要 TimeBinningProperties 对象。

Object

rotated

(可选)

指定将以垂直方式还是水平方式显示条柱。

  • True—Bars will be displayed horizontally.

  • False—Bars will be displayed vertically.

(默认值为 False)

Boolean

title

(可选)

图表的标题。 标题文本显示在图表视图的顶部,并在 内容 窗格中用作图表的标注。

String

description

(可选)

图表的描述。 该描述文本会显示于图表视图底部。

String

xTitle

(可选)

图表 x 轴的标题。

String

yTitle

(可选)

图表 y 轴的标题。

String

dataSource

(可选)

图表的数据源。 当通过 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebooks 中时,将读取数据源并将其呈现在图表上。 其中包括以下有效数据源:

  • 数据集的路径(包括本地数据集、UNC 路径和服务 URL)

  • Apache Arrow 表对象

  • LayerTable 对象

Object

displaySize

(可选)

当使用 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebooks 中时,图表的大小。 该值必须指定为两个项目的列表,其中第一个项目是图表的宽度,第二个项目是图表的高度。

List

theme

(可选)

指定将应用于图表的主题。

  • Light—The Light theme will be applied.

  • Medium—The Medium theme will be applied.

  • Dark—The Dark theme will be applied.

String

属性

名称 说明 数据类型

aggregation

(读取和写入)

用于指定将应用于聚合到每个条柱中的值的统计计算。

  • <None>—每个值都将在图表中绘制,无论值是否重叠或循环。

  • COUNT—将识别并应用总记录数。

  • SUM—将计算并应用所有值的总和。

  • MEAN—将计算并应用所有值的平均值。

  • MEDIAN—将识别并应用所有值的中值。

  • MIN—将识别并应用最小值。

  • MAX—将识别并应用最大值。

String

color

(读取和写入)

将应用于每个图表系列的颜色。 该值应为十六进制字符串值的列表。 如果未设置此属性,则系列颜色将匹配符号系统颜色(如果适用)。否则,将应用标准调色板。

List

dataSource

(读取和写入)

图表的数据源。 当通过 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebook 中时,将读取数据源并将其呈现在图表上。 其中包括以下有效数据源:

  • 数据集的路径(包括本地数据集、UNC 路径和服务 URL)

  • Apache Arrow 表对象

  • LayerTable 对象

Object

description

(读取和写入)

图表的描述。 该描述文本会显示于图表视图底部。

String

displaySize

(读取和写入)

当使用 exportToSVG 方法导出图表或将其显示在 ArcGIS Notebook 中时,图表的大小。 该值必须指定为两个项目的列表,其中第一个项目是图表的宽度,第二个项目是图表的高度。

List

enableServerSideProcessing

(读取和写入)

指定当数据来自企业级数据库或云数据仓库时,是否将使用 服务器端处理

  • True—将启用服务器端处理。

  • False—将禁用服务器端处理。

Boolean

legend

(读取和写入)

图表图例的属性。

  • alignment - 指定图例的对齐。 对齐选项为 leftbottomtopright

  • title - 图例的标题。

  • visible - 指定是否在图表视图中显示图例。 True 显示图例, False 隐藏图例。

Object

miniChartsPerRow

(读取和写入)

每行显示的迷你图表的数量。

Integer

movingAveragePeriod

(读取和写入)

移动平均时间段大小。

Integer

multiSeriesDisplay

(读取和写入)

指定将用于具有多个系列的条形图的显示类型。

  • sideBySide—将并排显示系列。

  • stacked—将堆叠显示系列。

  • stacked100—将按 100% 堆叠显示系列。

  • grid—系列将显示为迷你图表网格。

String

nullCategoryColor

(读取和写入)

空类别条柱的十六进制颜色值。

String

nullCategoryLabel

(读取和写入)

空类别条柱的 x 轴标注。

String

rotated

(读取和写入)

指定将以垂直方式还是水平方式显示图表。

  • True—将以水平方式显示图表。

  • False—将以垂直方式显示图表。

Boolean

showMovingAverage

(读取和写入)

指定是否将针对时间条形图显示移动平均线。

  • True—将显示移动平均线。

  • False—将隐藏移动平均线。

Boolean

showNullCategory

(读取和写入)

指定是否将按附加条柱汇总空值。

  • True—将显示空类别条柱。

  • False—将隐藏空类别条柱。

Boolean

showPreviewChart

(读取和写入)

指定是否在格网图表中显示预览图表。

  • True—将显示预览图表。

  • False—不会显示预览图表。

Boolean

splitCategory

(读取和写入)

第二个类别字段,将为字段中每一个唯一值添加单独的 系列 或条柱。

String

title

(读取和写入)

图表的标题。 标题文本显示于图表视图顶部,并用作 内容 窗格中 按绘制顺序列出 选项卡 按绘制顺序列出 的标注。

String

timeBinningProperties

(读取和写入)

将应用于时间条形图的时间图格属性。 该值必须为 TimeBinningProperties 对象。

Object

type

(只读)

用于指示图表类型的字符串值。

String

theme

(读取和写入)

指定将应用于图表的主题。

  • Light—将应用浅色主题。

  • Medium—将应用中等主题。

  • Dark—将应用深色主题。

String

x

(读取和写入)

x 轴变量的字段名称。 该字段必须为类别或日期字段。 如果指定空字符串,图表将 与图层符号匹配

String

xAxis

(读取和写入)

设置 x 轴的属性。

  • field - x 轴变量的字段名称。 该字段必须为类别或日期字段。

  • logarithmic - Bar 类不支持此属性。

  • maximum - 最大轴边界。

  • minimum - 最小轴边界。

  • sort - 指定应用于此轴的排序方法。 ASC 为升序排序, DESC 为降序排序。 可通过分配有序的类别名称列表指定自定义排序顺序。

  • title - 图表中所显示的轴的标注。

  • useAdaptiveBounds - 指定格网图表是带自适应还是固定轴边界显示。 True 显示带自适应边界的轴。 False 显示带固定边界的轴。

Object

y

(读取和写入)

一个或多个数值字段的字段名称。 此属性必须指定单一字段名或字段名称列表。

String

yAxis

(读取和写入)

指定 y 轴的属性。

  • addGuide - 使用 Guide 对象向轴添加一条参考线。

  • field - 一个或多个数值字段的字段名称。 此属性必须指定单一字段名或字段名称列表。

  • logarithmic - Bar 类不支持此属性。

  • listGuides - 轴的 Guide 对象的列表。

  • maximum - 最大轴边界。

  • minimum - 最小轴边界。

  • removeGuide - 移除 Guide 对象参数指定的参考线。

  • sort - 指定应用于此轴的排序方法。 ASC 为升序排序, DESC 为降序排序。

  • title - 图表中所显示的轴的标注。

  • useAdaptiveBounds - 指定格网图表是带自适应还是固定轴边界显示。 True 显示带自适应边界的轴。 False 显示带固定边界的轴。

Object

方法

addToLayer(layer_or_layerfile)

addToLayer 方法可将图表对象添加到图层或表视图。

名称 说明 数据类型

layer_or_layerfile

The chart will be added to the target object. This argument can be a Layer or Table object.

Object

将图表添加到现有图层。

import arcpy

# Insert creation of chart object here
# ...

aprx = arcpy.mp.ArcGISProject("current")
map = aprx.listMaps()[0]
censusLayer = map.listLayers('Census Block Groups')[0]

# Add chart object to a layer
chart.addToLayer(censusLayer)

exportToJPEG(path, width, height)

exportToJPEG 方法用于将图表导出为 JPEG 格式。

名称 说明 数据类型

path

The path where the chart will be exported in JPEG format.

String

width

The width of the output graphic.

Integer

height

The height of the output graphic.

Integer

将具有要素服务数据源的图表导出到 .jpeg 文件中。

import arcpy

# Insert creation of chart object here
# ...

featureServiceURL = r'https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/CrimesChiTheft/FeatureServer/0'

# Set data source of chart object to a feature service URL
chart.dataSource = featureServiceURL

# Save the chart to file with dimensions width=800, height=600
chart.exportToJPEG('theftsPerBeat.jpg', 800, 600)

exportToPNG(path, width, height)

exportToPNG 方法用于将图表导出为 PNG 格式。

名称 说明 数据类型

path

The path where the chart will be exported in PNG format.

String

width

The width of the output graphic.

Integer

height

The height of the output graphic.

Integer

从图层访问现有图表,更新标题,然后将其导出为 .png 文件。

import arcpy

lyr = arcpy.mp.ArcGISProject('current').listLayers()[0]

# Retrieve the first chart belonging to the layer
chart = lyr.listCharts()[0]

# Update chart title
chart.title = 'Voter Turnout by State'

# Save the chart to PNG file with dimensions width=800, height=600
chart.exportToPNG('populationByState.png', 800, 600)

exportToSVG(path, width, height)

exportToSVG 方法可将图表导出为 SVG 格式。

名称 说明 数据类型

path

将图表以 SVG 格式导出时的路径。

String

width

输出图形的宽度。

Integer

height

输出图形的高度。

Integer

将具有工程图层数据源的图表导出到 .svg 文件中。

import arcpy

# Insert creation of chart object here
# ...

aprx = arcpy.mp.ArcGISProject('current')
censusLayer = aprx.listMaps()[0].listLayers('Census Block Groups')[0]

# Set data source of chart object to a layer in current project
chart.dataSource = censusLayer

# Save the chart to file with dimensions width=500, height=500
chart.exportToSVG('populationByState.svg', 500, 500)

updateChart()

updateChart 方法可更新图表属性,以同步对象与之前添加到图层的图表之间的更改。

可以使用 updateChart 方法将图表属性更改同步到图层中。

import arcpy

# Insert creation of chart object here
# ...

chart.addToLayer(myLayer)

# Further modification is necessary
chart.description = "Data from the U.S. Census Bureau"
chart.updateChart()

代码示例

可以使用要素服务来创建条形图,然后将该图表导出至 .svg 文件。

import arcpy

url = r"https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/CrimesChiTheft/FeatureServer/0"
chart = arcpy.charts.Bar(x="BEAT", aggregation="count", title="Chicago Thefts by Beat", dataSource=url)
chart.exportToSVG('bar.svg', width=800, height=500)