Skip to content
import Graphic from "@arcgis/core/Graphic.js";
Inheritance:
GraphicAccessor
Subclasses:
AttributeBinsGraphic, DirectionsFeature
Since
ArcGIS Maps SDK for JavaScript 4.0

A Graphic is a vector representation of real world geographic phenomena. It can contain geometry, a symbol, and attributes. A Graphic is displayed in the GraphicsLayer.

To learn how to work with graphics, see the Intro to graphics tutorial.

let polyline = {
type: "polyline", // autocasts as new Polyline()
paths: [
[-111.30, 52.68],
[-98, 49.5],
[-93.94, 29.89]
]
};
let polylineSymbol = {
type: "simple-line", // autocasts as SimpleLineSymbol()
color: [226, 119, 40],
width: 4
};
let polylineAtt = {
Name: "Keystone Pipeline",
Owner: "TransCanada"
};
let polylineGraphic = new Graphic({
geometry: polyline,
symbol: polylineSymbol,
attributes: polylineAtt
});
view.graphics.add(polylineGraphic);
See also

Constructors

Constructor

Constructor
Parameters
ParameterTypeDescriptionRequired
properties
See the properties table for a list of all the properties that may be passed into the constructor.

Properties

Any properties can be set, retrieved or listened to. See the Watch for changes topic.

aggregateGeometries

Property
Type
Record<string, GeometryUnion> | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.23

The aggregateGeometries contains spatial aggregation geometries when statistics query is executed with envelope-aggregate, centroid-aggregate and/or convex-hull-aggregate statistics type. Each property on aggregateGeometries is populated with statistics field containing the aggregate geometry matching the aggregate statistics type.

See also
Example
// average of age fields by regions
const ageStatsByRegion = new StatisticDefinition({
onStatisticField: field,
outStatisticFieldName: "avgAge",
statisticType: "avg"
});
// extent encompassing all features by region
const aggregatedExtent = new StatisticDefinition({
statisticType: "envelope-aggregate",
outStatisticFieldName: "aggregateExtent",
});
// group the statistics by Region field
// get avg age by Regions and extent of each region
const query = layer.createQuery();
query.groupByFieldsForStatistics = ["Region"];
query.outStatistics = [consumeStatsByRegion, aggregatedExtent];
layer.queryFeatures(query).then((results) => {
results.features.forEach((feature) => {
if (feature.attributes.Region === "Midwest") {
view.goTo(feature.aggregateGeometries.aggregateExtent);
}
});
});

attributes

Property
Type
any

Name-value pairs of fields and field values associated with the graphic.

Example
let graphic = new Graphic();
graphic.attributes = {
"name": "Spruce",
"family": "Pinaceae",
"count": 126
};

declaredClass

readonlyinherited Property
Type
string
Inherited from: Accessor
Since
ArcGIS Maps SDK for JavaScript 4.7

The name of the class. The declared class name is formatted as esri.folder.className.

geometry

autocast Property
Type
GeometryUnion | null | undefined

The geometry that defines the graphic's location.

Z-values defined in a geographic or metric coordinate system are expressed in meters. However, in local scenes that use a projected coordinate system, vertical units are assumed to be the same as the horizontal units specified by the service.

Example
// First create a point geometry
let point = {
type: "point", // autocasts as new Point()
longitude: -71.2643,
latitude: 42.0909
};
// Create a symbol for drawing the point
let markerSymbol = {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
color: [226, 119, 40]
};
// Create a graphic and add the geometry and symbol to it
let pointGraphic = new Graphic({
geometry: point,
symbol: markerSymbol
});

isAggregate

readonly Property
Type
boolean
Since
ArcGIS Maps SDK for JavaScript 4.18

Indicates whether the graphic refers to an aggregate, or cluster graphic. Aggregate graphics represent clusters of features. If a graphic is an aggregate, you can use its Object ID to Query.aggregateIds the features comprising the cluster.

See also
Default value
false

layer

Property
Type
GraphicLayer | null | undefined

If applicable, references the layer in which the graphic is stored.

origin

Property
Type
GraphicOrigin | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.29

Returns information about the origin of a graphic if applicable. Origin details may be returned by methods such as MapView.hitTest() or by calling queryFeatures() on a layer or layer view. Depending on the origin type, this information may include the source layer or other origin-related metadata.

Example
// get screen point from view's click event
view.on("click", (event) => {
// Search for all features only on included layers at the clicked location
view.hitTest(event, {include: vectorTileLayer}).then((response) => {
// if graphics are returned from vector tile layer, do something with results
if (response.results.length){
response.results.forEach((result, i) => {
const layerId = result.graphic?.origin?.layerId;
const styleLayer = vectorTileLayer.getStyleLayer(layerId);
// update layer's style
});
}
})
});

popupTemplate

autocast Property
Type
PopupTemplate | null | undefined

The template for displaying content in a Popup when the graphic is selected. The PopupTemplate may be used to access a graphic's attributes and display their values in the view's default popup. See the documentation for PopupTemplate for details on how to display attribute values in the popup.

As of 4.8, to get the actual popupTemplate of the graphic, see the getEffectivePopupTemplate() method that either returns this value or the popupTemplate of the graphic's layer.

See also
Example
// The following snippet shows how to set a popupTemplate
// on the returned results (features) from identify
idResult.feature.popupTemplate = {
title: "{NAME}",
content: [{
// Pass in the fields to display
type: "fields",
fieldInfos: [{
fieldName: "NAME",
label: "Name"
}, {
fieldName: "REGION",
label: "Region"
}]
}]
};

symbol

autocast Property
Type
SymbolUnion | null | undefined

The Symbol for the graphic. Choosing a symbol for a graphic depends on the View type (SceneView or MapView), and the geometry type of the graphic.

Each graphic may have its own symbol specified when the parent layer is a GraphicsLayer. For a FeatureLayer the symbol of each graphic should not be set by the developer since the graphics' rendering properties are determined by the layer's Renderer.

To change a graphic's Symbol in the GraphicsLayer and in the View.graphics at runtime, one of the following can be done:

  1. Clone the symbol and change its properties.
  2. Assign a new symbol to the graphic.
Example
view.on("click", function(event){
let graphic = new Graphic({
geometry: event.mapPoint,
symbol: {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
color: "blue",
size: 8,
outline: { // autocasts as new SimpleLineSymbol()
width: 0.5,
color: "darkblue"
}
}
});
});

visible

Property
Type
boolean

Indicates the visibility of the graphic.

Default value
true

Methods

MethodSignatureClass
fromJSON
inherited static
fromJSON(json: any): any
clone
inherited
clone(): this
getAttribute<T = any>(name: string): T
getEffectivePopupTemplate(defaultPopupTemplateEnabled?: boolean): PopupTemplate | null | undefined
getGlobalId(): string | null | undefined
getObjectId(): ObjectId | null | undefined
setAttribute(name: string, newValue: any): void
toJSON
inherited
toJSON(): any

fromJSON

inheritedstatic Method
Signature
fromJSON (json: any): any
Inherited from: JSONSupportMixin

Creates a new instance of this class and initializes it with values from a JSON object generated from an ArcGIS product. The object passed into the input json parameter often comes from a response to a query operation in the REST API or a toJSON() method from another ArcGIS product. See the Using fromJSON() topic in the Guide for details and examples of when and how to use this function.

Parameters
ParameterTypeDescriptionRequired
json
any

A JSON representation of the instance in the ArcGIS format. See the ArcGIS REST API documentation for examples of the structure of various input JSON objects.

Returns
any

Returns a new instance of this class.

clone

inherited Method
Signature
clone (): this
Inherited from: ClonableMixin

Creates a deep clone of this object. Any properties that store values by reference will be assigned copies of the referenced values on the cloned instance.

Returns
this

A deep clone of the class instance that invoked this method.

getAttribute

Method
Signature
getAttribute <T = any>(name: string): T
Type parameters
<T = any>

Returns the value of the specified attribute.

Parameters
ParameterTypeDescriptionRequired
name

The name of the attribute.

Returns
T

Returns the value of the attribute specified by name.

getEffectivePopupTemplate

Method
Signature
getEffectivePopupTemplate (defaultPopupTemplateEnabled?: boolean): PopupTemplate | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.8

Returns the popup template applicable for the graphic. It's either the value of popupTemplate or the popupTemplate from the graphic's layer.

Parameters
ParameterTypeDescriptionRequired
defaultPopupTemplateEnabled

= false - Whether support for default popup templates is enabled. When true, a default popup template may be created automatically if neither the graphic nor its layer have a popup template defined.

Returns
PopupTemplate | null | undefined

Returns the popup template of the graphic.

getGlobalId

Method
Signature
getGlobalId (): string | null | undefined
Since
ArcGIS Maps SDK for JavaScript 4.33

Returns the Global ID of the feature associated with the graphic, if it exists.

Returns
string | null | undefined

The GlobalID of the feature associated with the graphic.

getObjectId

Method
Signature
getObjectId (): ObjectId | null | undefined

Returns the Object ID of the feature associated with the graphic, if it exists. The Object ID can be either a numeric value or a string identifier.

Returns
ObjectId | null | undefined

The ObjectID of the feature associated with the graphic.

setAttribute

Method
Signature
setAttribute (name: string, newValue: any): void

Sets a new value to the specified attribute.

Parameters
ParameterTypeDescriptionRequired
name

The name of the attribute to set.

newValue
any

The new value to set on the named attribute.

Returns
void

toJSON

inherited Method
Signature
toJSON (): any
Inherited from: JSONSupportMixin

Converts an instance of this class to its ArcGIS portal JSON representation. See the Using fromJSON() guide topic for more information.

Returns
any

The ArcGIS portal JSON representation of an instance of this class.

Type definitions