Since 5.0
The GeoChart widget displays information about countries, regions or states according to the provided data with the help of Webix and Google Map API. The specified areas are highlighted with colors, labelled with texts or indicated with markers according to the chosen mode.
To initialize Webix GeoChart on a page, use the following code:
webix.ui({
view:"geochart",
id:"map",
// provide your own Google API key
// https://developers.google.com/maps/documentation/javascript/get-api-key
key:"AIzaSyAi0oVNVO-e603aUY8SILdD4v9bVBkmiTg",
data:[
{id:1, country:'Germany', area:200},
{id:2, country:'United States', area:400},
{id:3, country:'Brazil', area:1000},
{id:4, country:'Canada', area:500},
{id:5, country:'France', area:650},
{id:6, country:'RU', area:1180}
]
});
Pay attention that before working with Webix GeoChart you need to get a personal Google API key.
Related sample: GeoChart: Basic
Main Properties
The following properties define configuration of the GoogleMap widget:
To get the map object, you can make use of the getMap method.
var mapObj = $$("map").getMap();
The method can take the waitMap parameter. If the parameter is passed, the method returns a promise which will be resolved when the map is rendered.
$$("map").getMap("waitMap").then(function(mapObj){
// some code
});
GeoChart is a DataStore-based widget and you can work with it like with any other Webix data widget. You can use all common ways of data loading to load the data in supported data formats (JSON, XML, CSV, JSArray) into the chart.
Regardless of the data format, you should ensure that each data item contains the information about the related tile (country, region or state).
For example, you can set data inline with the help of the data parameter:
webix.ui({
view:"geochart",
key:"..",
data:[
{id:1, country:'Germany', area:200},
{id:2, country:'United States', area:400},
{id:3, country:'Brazil', area:700},
{id:4, country:'Canada', area:500},
{id:5, country:'France', area:200},
{id:6, country:'Russia', area:880}
];
});
The data will be automatically converted into GeoChart-specific format and shown as regions, markers or texts depending on the chosen display mode.
You can load data into GeoChart in the following formats:
<data>
<item id="1" country="Germany" area="200"/>
<item id="2" country="United States" area="400" />
<item id="3" country="Brazil" area="700"/>
<item id="4" country="Canada" area="500"/>
<item id="5" country="France" area="200"/>
<item id="6" country="Russia" area="880"/>
</data>
[
['Germany', 200],
['United States', 400],
['Brazil', 700],
['Canada', 500],
['France', 200],
['Russia', 880]
];
Germany, 200
United States, 400
Brazil, 700
Canada, 500
France, 200
Russia, 880
See the details on the data formats supported by Webix.
Related sample: GeoChart: Data Formats
The columns setting defines the properties of a data item which will be displayed in the geochart in the desired order. The number of columns and their format are defined by the active display mode. Read the detailed specification on columns data format.
You can set the columns as an array of strings:
webix.ui({
view:"geochart",
key:"..",
columns:["country", "area"],
data:[
{id:1, a:124, country:'Germany', area:200},
{id:2, a:121, country:'United States', area:400}
]
});
In this case the column data type (number or string from above) will be detected automatically depending of the value of this property in the first data item.
Or, if needed, you can set the desired attributes like column label and data type explicitly:
webix.ui({
view:"geochart",
key:"..",
columns:[
{ type:"string", label:"country"},
{ type:"number", label:"area"}
],
data:[
{id:1, a:124, country:'Germany', area:200},
{id:2, a:121, country:'United States', area:400}
]
});
The columns can contain other properties. The format of columns is defined by the google.visualization.DataTable class.
Related sample: GeoChart: Custom columns
The GeoChart widget allows working with geochart data via Webix DataStore API, which means that you can load data into the geochart in any of the available ways as well as add, update, remove the items, etc.
var id = $$("map").add({country:"Brazil", area:900});
$$("map").updateItem(id, { area:1500});
$$("map").removeItem(id);
Related sample: GeoChart: Operations with Data
There are several events you can handle while working with geochart:
The event handler takes the id of the data item as a parameter.
webix.ui({
view:"geochart",
id:"map",
key:"..",
data:json_data,
on:{
onItemClick:function(id){
webix.message("Data item clicked: "+this.getItem(id).country);
}
}
});
The event handler takes the region object as a parameter.
webix.ui({
view:"geochart",
id:"map",
data:json_data,
key:"..",
on:{
onRegionClick:function(obj){
webix.message("Region clicked: "+obj.region);
}
}
});
$$("map").attachEvent("onMapError", function(id, message){
// your code here
});
The event handler takes two parameters: the code of the error and the error message.
$$("map").attachEvent("onMapReady", function(){
// your code here
});
Related sample: GeoChart: Events
Settings of the inner GeoChart, such as displayMode, region, resolution, colorAxis, etc. can be defined in the chart object definition:
{
view:"geochart",
id:"map",
// provide your own Google API key
// https://developers.google.com/maps/documentation/javascript/get-api-key
key:"AIzaSyAi0oVNVO-e603aUY8SILdD4v9bVBkmiTg",
data:data,
chart:{
displayMode:"regions",
region:"150", // Europe
colorAxis: {colors: ["#f2f28d", "#9370db"]}
}
}
The full list of settings is available in the Google GeoCharts docs.
You can dynamically change them in one of the following ways:
$$("map").config.chart.colorAxis = {colors: ["#e7711c", "#4374e0"]};
$$("map").config.chart.displayMode = "markers",
$$("map").refresh();
$$("map").setDisplayMode("text");
$$("map").setRegion("IT");
Note that when changing the properties via config you should call the refresh method to apply changes.
Related sample: GeoChart: Modes
You can set a custom tooltip for displaying the tiles' data via the related attribute of the geochart object. It is based on Webix Tooltip and uses the same syntax.
webix.ui({
view:"geochart",
id:"map",
data:data,
key:"..",
columns:["country", "population"],
tooltip:"Population: #population#<br>Area: #area#"
});
Related sample: GeoChart: Custom Tooltip
Note that this setting can't affect the displaying of a region name in the tooltip.
Since there's no genuine selection in Google GeoCharts, you can provide logic for tile highlighting on the base of the data while Webix API will help you.
For example, you can set the selected property for data items. Its value will be 1 for the "selected" items and 0 for "unselected" ones. On each item click the active item will be updated to selected:1, while all the others will be updated to selected:0.
In addition, you should restrict the Google Chart color axis to 0 and 1 values by setting its minValue and maxValue. Then, the axis colors will be correctly distributed between the "selected" item and "unselected" ones.
webix.ui({
view:"geochart",
id:"map",
data:[
{ id:1, country:'Germany', selected:1 },
{ id:2, country:'United States', selected:0}
],
key:"..",
tooltip:"",
chart:{
legend:"none",
colorAxis: { minValue: 0, maxValue: 1, colors: ["#f2f28d", "#9370db"]}
},
on:{
onItemClick:function(id){
this.data.each(function(obj){
obj.selected = (obj.id == id)?1:0
});
this.refresh();
}
}
});
Note that items coloring depends on their data values. Here we just use the data and change it to mimic genuine selection.
Related sample: GeoChart: Selection
Back to top