How to create a simple map and embed it into a web page
- Intro
- Create a web page
- Terms
- Choosing Map Data
- Attaching Custom Data to a map
- HTML File structure
- Final
Intro
AnyChart is a flexible component for creation charts and maps in Web, you can easily create a lot of maps and charts using it, so let's see step by step how to create a page with flash map.
Note: in this document only AnyChart.swf is mentioned, but you can optimize the page with selected chart type if you use custom type dependent swf. These swfs are described in SWFs Guide.
Create a web page
First of all we need a plain HTML page that contains a Flash object, and place it to some folder on your web site: Below we've done it, creating AnyChartTest folder in IIS wwwroot folder with the following structure:
- /AnyChartTest
- /swf
- AnyChart.swf
- states.amap
- /js
- AnyChart.js
- anychart.xml
- anychart.html
- /swf
Terms
Container page: a page where chart will be displayed; this page can be HTML, ASP, ASP.NET, CF, PHP, CGI, Perl (or any other scripting language capable to produce HTML output).
Data provider: AnyChart accepts all data needed to show the map in a single XML File. The structure of this file will be explained later.
Chart SWF: Charts and Maps displayed by AnyChart are rendered using single swf file - that is why we created an swf sub folder.
Map Data File : Map data file containing map of the certain region.
AnyChart JavaScript library: to embed a chart into a web page we'll use AnyChart JavaScript Integration Library.
Choosing Map data and its visual style
As we are creating a map we need some data for it - an XML file that will tell chart how it should display map and what map to display and Map data file containing map itself. Imagine that we want to show simple colored map of USA.
We will put a map of all states - this map is stored in states.amap file that we placed in /swf folder. amap files are AnyChart proprietary format to store map data.
To tell AnyChart to use this file and show the map we need create XML settings file, like that:
Now copy the above code, open anychart.xml from AnyChartTest folder in any plain text editor and paste code there.
XML structure may look complex, but most of the tags are self descriptive and we can describe everything done in XML in several words:
In <chart> node we set that AnyChart should render map: we've set a "Map" plot type - <chart plot_type="Map">
In <chart_settings> node we described how our chart should look like:
We've set a map title text:
In <data_plot_settings> we've set that "states.amap" file should be used as source for map (there are a lot of other maps in AnyChart package - see full list):
Also we've set that map projection should be Mercator (learn more about projections), set palette to color states in different colors and enabled labels for all map regions:
Attaching Custom Data to a map
With AnyChart you can attach any custom data to any map and then use this data for coloring, in labels, tooltips, legend and custom actions - learn more about this feature in How to attach data to a map tutorial.
HTML File structure
Only one step remains and we will see our map on the Web!
Here is the code you should paste in anychart.html (just open it in any text editor and copy-paste the code):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>AnyChart Sample Flash Map </title>
<script type="text/javascript" language="javascript" src="./js/AnyChart.js"></script>
</head>
<body>
<script type="text/javascript" language="javascript">
//<![CDATA[
var chart = new AnyChart( './swf/AnyChart.swf');
chart.width = 600;
chart.height = 450;
chart.setXMLFile('./anychart.xml');
chart.write();
//]]>
</script>
</body>
</html>
Only text in bold is the html code, that you need to embed AnyChart Flash Map into a HTML page.
Final
Now we should launch our html page in a web browser, in our case we can use both:
http://localhost/AnyChartTest/anychart.html
or
C:\Inetpub\wwwroot\AnyChartTest\anychart.html
If everything was done exactly as described above, you will see the following map:
You can open this sample from here: Open the Simple Map Sample
You can download the sample files from here: Download the Sample
If you want to try to modify sample in a folder other than live sample folder of AnyChart Documentation you should follow the installation tutorial: Installation of AnyChart to the Web Server of Local Machine