Code examples explaining how to set up map charts.
Refer to What is a Map Chart? for a map chart presentation.
Create and Configure a Map Chart
public static void CreateMapChart(AnalysisApplication application)
{
// Add a map chart to the page
MapChart mapChart = application.Document.ActivePageReference.Visuals.AddNew<MapChart>();
mapChart.Title = "Basic Map Chart";
// Connect the map chart to data
DataManager dataManager = application.Document.Data;
mapChart.Data.DataTableReference = dataManager.Tables.DefaultTableReference;
// Set filtering for the visualization
mapChart.Data.UseActiveFiltering = true;
// Set marking for the visualization
mapChart.Data.MarkingReference = dataManager.Markings.DefaultMarkingReference;
// Load the map background
string mapPath = @"C:\My Maps\Country Boundaries 1.shp";
PolygonLayer polygonLayer = mapChart.AddFromFile(mapPath) as PolygonLayer;
polygonLayer.IdentifierColumnName = "CNTRY_NAME";
// Configure the marker layer
MarkerLayer markerLayer = mapChart.AddMarkerLayer();
markerLayer.PositioningMethod = PositioningMethod.Identifier;
markerLayer.DataColumnName = "Country";
markerLayer.XAxis.Scale.Visible = false;
markerLayer.YAxis.Scale.Visible = false;
markerLayer.XAxis.Scale.ShowGridlines = false;
markerLayer.YAxis.Scale.ShowGridlines = false;
//Set size of markers by sales
markerLayer.SizeAxis.Expression = "Sum(Sales)";
//Set shape to circles
markerLayer.MarkerShapeAxis.DefaultShape = new MarkerShape(MarkerType.Circle);
// automatically zoom the map to the data in the markerlayer
mapChart.AutoZoom = true;
}
Create a Map Chart with Pies
public static void CreateMapChartWithPies(AnalysisApplication application)
{
// Add a map chart to the page
MapChart mapChart = application.Document.ActivePageReference.Visuals.AddNew<MapChart>();
mapChart.Title = "Basic Map Chart";
// Connect the map chart to data
DataManager dataManager = application.Document.Data;
mapChart.Data.DataTableReference = dataManager.Tables.DefaultTableReference;
// Set filtering for the visualization
mapChart.Data.UseActiveFiltering = true;
// Set marking for the visualization
mapChart.Data.MarkingReference = dataManager.Markings.DefaultMarkingReference;
// Load the map background
string mapPath = @"C:\My Maps\Country Boundaries 2.shp";
PolygonLayer polygonLayer = mapChart.AddFromFile(mapPath) as PolygonLayer;
polygonLayer.IdentifierColumnName = "CNTRY_NAME";
// Configure the pie layer
PieLayer pieLayer = mapChart.AddPieLayer();
pieLayer.PositioningMethod = PositioningMethod.Identifier;
pieLayer.DataColumnName = "Country";
// Set sector size by sum of sales
pieLayer.SectorSizeAxis.Expression = "Sum(Sales)";
// Define Split by City
pieLayer.ColorAxis.Expression = "<City>";
// automatically zoom the map to the data in the markerlayer
mapChart.AutoZoom = true;
// Configure pie labels
pieLayer.VisualAttributes.LabelPosition = LabelPosition.Outside;
}
Setting Up a Colored Map Using a Geometry Table Column
The following example sets up a map and colors it using the REGION column from the map table:
public void SetupColoredMapUsingColumnFromMapTable(MapChart mapChart, DataManager dataManager)
{
// Open the map
PolygonLayer polygonLayer = mapChart.AddFromFile("country.shp") as PolygonLayer;
// Change the data table for the plot to use the map table
mapChart.Data.DataTableReference = polygonLayer.DataTableReference;
// Color regions by a column in the map table
polygonLayer.ColorAxis.Expression = "Sum([REGION])";
}
Setting Up a Colored Map Using a Column from the Plot Table
Sometimes data relating to the map content, in addition to the data in the map.shp file, is retrieved from a file loaded in Spotfire. The following example creates a map and colors based on the Area column in the plot data table:
/// Sets up a colored map using a column from the plot table.
/// </summary>
public void SetupColoredMapUsingColumnFromPlotTable(MapChart mapChart, DataManager dataManager)
{
// Open the map
PolygonLayer polygonLayer = mapChart.AddFromFile("country.shp") as PolygonLayer;
// Retrieve the tables that are in use
DataTable mapTable = polygonLayer.DataTableReference;
DataTable plotTable = mapChart.Data.DataTableReference;
// Set up a relation that connect the tables
dataManager.Relations.Add(mapTable, plotTable, "[country].[NAME] = [RootView].[Country]");
// Color regions by a column in the plot table
polygonLayer.ColorAxis.Expression = "Sum([Area])";
}
Setting Up a Map with Markers at Positions from Columns in the Map Table
The following example shows how to explicitly set visual attributes of the map and how to add markers on top of a map:
public void SetupMapWithMarkersAtPositionsFromMapTable(MapChart mapChart, DataManager dataManager)
{
// Open the map
PolygonLayer polygonLayer = mapChart.AddFromFile("country.shp") as PolygonLayer;
// Change the data table for the plot to use the map table
mapChart.Data.DataTableReference = polygonLayer.DataTableReference;
// Create a marker layer
MarkerLayer markerLayer = mapChart.AddMarkerLayer();
// Set the positioning method to be longitude/latitude
markerLayer.PositioningMethod = PositioningMethod.LongitudeLatitude;
// Set x and y columns
markerLayer.XAxis.Expression = "[XCenter]";
markerLayer.YAxis.Expression = "[YCenter]";
// Color regions by a column in the map table
markerLayer.ColorAxis.Expression = "Sum([REGION])";
// Turn on labels
markerLayer.LabelColumn.Expression = "UniqueConcatenate([NAME])";
markerLayer.LabelVisibility = LabelVisibility.All;
// Change color of the map
polygonLayer.Color = Color.FromArgb(208, 168, 132);
polygonLayer.BorderColor = Color.White;
polygonLayer.BorderWidth = 1.0f;
}
Setting Up a Map Chart With Interactive Shapes
public static void CreateMapChartWithShapes(AnalysisApplication application)
{
// Add a map chart to the page
MapChart mapChart = application.Document.ActivePageReference.Visuals.AddNew<MapChart>();
mapChart.Title = "Map Chart With Shapes";
// Connect the map chart to data
DataManager dataManager = application.Document.Data;
mapChart.Data.DataTableReference = dataManager.Tables.DefaultTableReference;
// Set filtering for the visualization
mapChart.Data.UseActiveFiltering = true;
// Set marking for the visualization
mapChart.Data.MarkingReference = dataManager.Markings.DefaultMarkingReference;
// Load the map background
string mapPath = GetAsResourcePath("Country Boundaries 2.shp");
PolygonLayer polygonLayer = mapChart.AddFromFile(mapPath) as PolygonLayer;
// Retrieve the tables that are in use
DataTable mapTable = polygonLayer.FeatureTableReference;
DataTable plotTable = mapChart.Data.DataTableReference;
// Set up a relation that connect the tables
dataManager.Relations.Add(mapTable, plotTable, "[Country Boundaries 2].[CNTRY_NAME] = [worldsales].[Country]");
// Color the shapes
polygonLayer.ColorAxis.Expression = "Sum(Sales)";
// Add a tooltip for Avg(Sales)
polygonLayer.Details.Items.AddExpression("Avg(Sales)");
}