Quantcast
Channel: Tutorials – amCharts Version 3 Documentation
Viewing all 111 articles
Browse latest View live

Updating map image coordinates via API

$
0
0

This demo shows how you can dynamically update coordinates of the map image. We’re going to have a loop to create a basic animation of moving a simple circle from Vienna to Rio.

If we want to move an image to other coordinates, there’s no need to update the whole map data. We can follow these easy steps:

  1. Get image object. getObjectById() method seems perfect for that.
  2. Update its latitude and longitude parameters.
  3. Call its validate() method.

The post Updating map image coordinates via API appeared first on amCharts.


Using amCharts WordPress plugin

$
0
0

WordPress being the single most used web publishing platform, is perfect for when you want your website up and running in minutes.

However, when it comes to dynamic content, such as JavaScript, you’ll find that there’s practically no options in the plain vanilla WP install.

This is precisely the reason why we went ahead and created our own, official WordPress plugin. It allows adding dynamic charts and maps to your posts and pages a breeze.

Continue with this tutorial for step-by-step instructions.

Installing the plugin

To install the plugin, simply go to your Plugins section in WP admin, click “Add New” and search for “amCharts”, then click on “Install Now” next to the “amCharts: Charts and maps”.

Installing amCharts WordPress plugin

WP will install the plugin. Make sure you click “Activate” when the button pops up instead of “Install Now”.

You’re all set.

Configuring the plugin

The plugin comes with 100% usable default configuration. If you’d like to tinker with the config, you’ll find it under Settings > Charts & Maps.

Most of the users might skip this, and dive right in.

Use config page to:

  • Set the location from where to load amCharts files. (it loads those files directly from amCharts CDN servers by default)
  • Edit default code templates for each chart type
  • Other options

Creating a chart

There are several ways to create a chart or map. You can either use a default template or start from scratch.

Starting from scratch

To create a new chart or a map we click “Add New” under “Charts & Maps” in WP admin.

You’ll get a familiar post edit interface with a few additional fields.

The “Title” field is the same field you have for all of the WP posts, pages and other content types. Make it something meaningful, so that when you have a lot of charts, you exactly know what it does.

Naming your chart with something like “Chart 1” is not as descriptive as “Pie chart for expenditures by department for 2016”.

Resources box holds all the JavaScript and CSS files you will need for this chart. All amCharts instances require the “amcharts.js” file as well as the chart type file, such as “pie.js”, “serial.js”, etc.

Some types require multiple files. I.e. Stock Chart requires “amcharts.js”, “serial.js” and “amstock.js”.

Maps require both “ammap.js” and a map file, such as “worldLow.js”.

Resource box needs to show either a relative or absolute path, with the latter being recommended.

WP resources box

If you don’t want to enter those links, you can use the “Select a resource” dropdown to quickly add the link to a resource.

HTML box is where your HTML part goes. Most often it’s a <div> tag that defines the container in which to store the chart or map.

Important part is it’s `id` property. It’s an identification which will be used by the chart to determine where it should be stored in.

You can provide static id, like “mychart”. However, we recommend using a special code “$CHART$”. When generating the chart, WordPress will replace this with a unique id, which will make it safe to have multiple charts on the same page.

Also, you probably want to set it’s style to give it some width and height:

WP HTML box

JavaScript box is where all the magic happens. It contains the actual JavaScript code for the chart or map.

Most of the amCharts-enabled charts or maps use the AmCharts.makeChart() function call.

For example the following code creates a fully functional Pie chart:

var $CHART$ = AmCharts.makeChart( "$CHART$", {
  "type": "pie",
  "dataProvider": [ {
    "country": "Lithuania",
    "litres": 501.9
  }, {
    "country": "Czech Republic",
    "litres": 301.9
  }, {
    "country": "Ireland",
    "litres": 201.1
  } ],
  "valueField": "litres",
  "titleField": "country"
} );

Notice the “$CHART$” is here again.

You are encouraged to use this code whenever you want some uniqueness. When displaying the chart, the plugin will replace it with something unique like “amchart1” for each instance on the same page.

For example if you have three charts on the same page, the first one will replace all occurrences of “$CHART$” to “amchart1”, the second to “amchart2”, etc. thus preserving the uniqueness and avoiding any potential id conflicts.

Using a pre-defined template

Let’s say, I want to create a Pie chart. It might be easier to start from a working Pie chart, then just modify it’s code.

To do that, we hover on the Charts & Maps item in WP admin menu, then select “+ Pie”.

Creating a new chart from template

You’ll notice that all fields we discussed in previous section are prefilled with code.

At this point, if you press “Preview chart or map” you’ll get a fully functional Pie chart.

Edit the code, publish the chart.

Previewing the chart

Once you’re ready to preview how your chart looks in real life, hit the “Preview chart or map” button which is located in the box on the left.

Previewing the chart

A new window/tab will open with the chart shown wrapped in your website’s real theme.

Publishing the chart

As with any other chart WordPress content, before it can be shown on a live website, you’ll need to publish it, by hitting the “Publish” button.

That’s all to creating charts. Now, let’s move on to displaying them.

Adding charts and maps to WP content

You can insert special embed codes into any WordPress page or post, called shortcodes.

An amCharts shortcode follows the syntax `[amcharts id=”…”]` where id is the user-firendly slug you entered (or let the plugin assign on) when creating the chart.

You can either enter the shortcode yourself, or use the special icon in editor.

Inserting chart

It will bring up a modal window, which will display a searchable list of charts you have created.

List of charts to insert

Just click on the one you want to be added to the page and the plugin will insert a proper shortcode for it.

Chart shortcode

Now, when you save your page and view it on the live website, you’ll see the the shortcode being replace by the real chart.

Live chart on a WordPress page

Related reading

If you are using our Live Editor, you may find the following article useful:

Embedding Live Editor charts into WordPress (and other oEmbed-compatible systems)

For help on transferring the demos found on amcharts.com, CodePen and jsFiddle to WordPress read this article:

Transferringdemos from amcharts.com, CodePen or jsFiddle into WordPress

The post Using amCharts WordPress plugin appeared first on amCharts.

Transferring demos from amcharts.com, CodePen or jsFiddle into WordPress

$
0
0

Most of the amCharts demos you find will be shown on either amcharts.com demo section, CodePen or jsFiddle websites.

This tutorial focuses on how to easily transfer them into your WordPress website.

Prerequisites

This tutorial relies on amCharts WordPress plugin.

If you haven’t done so already, we strongly advise you to check this introductory tutorial, which will guide you through installation of the plugin, as well as basics of displaying charts and maps in a WordPress website.

Demos on amcharts.com

You will find a lot of pre-made demos in our Demos section.

Each of the demos, has a “View Demo Source” button.

View demo source button

Hover it. Among other options, you will get an option to view its source.

When you click on it, you will get all the components that make up the chart: CSS, Resouces, JavaScript and HTML.

Viewing the source of the demo

The task here is to copy different parts of this collective code to appropriate boxes in WordPress.

CSS

This section starts with a comment <!-- Styles -->

Since WordPress plugin does not offer a separate box for CSS, we’ll skip this one.

Resources

As the name suggests, the contents should go into “Resources” box of in the WordPress.

Basically, it lists all the external files that need to be loaded, for this particular demo to work as expected.

IMPORTANT! WordPress plugin expects resources to be the link only, i.e.:

https://www.amcharts.com/lib/3/amcharts.js

However, we have a full tag:

<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>

What we’ll need to do, is to strip out the tags, so that only the links (URLs) remain from all resources:

https://www.amcharts.com/lib/3/amcharts.js
https://www.amcharts.com/lib/3/serial.js
https://www.amcharts.com/lib/3/plugins/export/export.min.js
https://www.amcharts.com/lib/3/plugins/export/export.css
https://www.amcharts.com/lib/3/themes/light.js

JavaScript

The section that starts with <!-- Chart code --> should go into JavaScript box on WordPress.

IMPORTANT! The code should not include the enclosing <script> and </script>.

HTML

Finally, the tiny little bit of code that starts with <!-- HTML --> should go into, yes you guessed it correctly, HTML box on WordPress.

There’s one caveat, though. Most of the demos rely on separate CSS style setting its dimensions – width and height. Since WordPress plugin does not allow entering CSS, you will need to modify HTML to set those dimensions using in-line styles.

So, this:

<div id="chartdiv"></div>

Should become something like this:

<div id="chartdiv" style="width: 100%; height: 500px;"></div>

Cheat Sheet

amCharts demo to WordPress

CodePen and jsFiddle

Both CodePen and jsFiddle that are excellent websites, used to edit and publish working HTML/JavaScript code snippets.

It’s even easier to port amCharts demos, listed on those websites to WordPress plugin.

HTML

HTML pane on both CodePen and jsFiddle will contain both the resources and the HTML code.

As with the previous section, you will need to identify resources (<script> and <link> tags), pull out just the link portions and enter them into Resources box on WordPress.

The rest of goes into HTML. (with the same in-line style tweaks as in previous section)

Cheat Sheet

CodePen sections

The post Transferring demos from amcharts.com, CodePen or jsFiddle into WordPress appeared first on amCharts.

Angular Gauge with Two Axes

$
0
0

This demo shows how you can add multiple axes to the Angular Gauge chart. The inner circle is achieved using an axis with small radius coinciding with arrow’s innerRadius so it looks like a hollow base for it.

The post Angular Gauge with Two Axes appeared first on amCharts.

Display the last value of the graphs on value axis in Stock Chart

Selecting countries on a map

$
0
0

This demo shows how you can use JavaScript Maps’ clickMapObject event to create a map with togglable countries.

The event handler dynamically sets showAsSelected property, without reloading the whole of the chart.

This demo also defines a sample function to gather currently selected countries, by cycling through the list and checking their showAsSelected attributes.

The post Selecting countries on a map appeared first on amCharts.

Pre-selecting areas on a map using array of country ISO2 codes

Simulating double-click on GANTT chart segment

$
0
0

This demo shows how you can use clickGraphItem event to register both click and double-click events on GANTT chart segments.

It works by delaying execution of click event using JavaScript function setTimeout. If the second click occurs on the same segment within 200 milliseconds, we cancel previous click and proceed as it was double-click.

P.S. the demo outputs information into browser’s console. Press F12 and select “Console” tab to view it.

The post Simulating double-click on GANTT chart segment appeared first on amCharts.


Automatically create state labels and callouts with values

Order stacked columns by their value within each category

$
0
0

Technically, there is no way to order stacked columns within each category individually. Each column segment will come in the same order as they are defined in graphs array.

This demo shows how to work around that by automatically converting all graphs to non-stacked, non-clustered floating columns.

It uses AmCharts.addInitHandler() function to define code that executes before chart is initialized. We take this opportunity to examine our graphs, go through data and recalculate their open and close values based on their values, so they look stacked up, but in fact they’re not.

Please note that stackByValue setting used in this demo is completely made up for purposes of this demo and is not part of amCharts engine.

The post Order stacked columns by their value within each category appeared first on amCharts.

Optimizing multi-chart, periodically-updated dashboards

$
0
0

Problem

Having a lot of charts on the same page can put a significant strain on the browser. Especially if those charts are updated dynamically.

Typically, in dynamic dashboards, you’d update chart’s dataProvider and call its validateData() method. While the operation is hardly noticeable for one chart, on multi-chart dashboards they can pile up, especially if there are many dashboards, updating at the same time. This can even bring your dashboards to a crawl.

While you can amCharts has some thread simulation in place, to give your browser some breathing space across updates, it can still put a lot of burden on it.

Solution

The solution in this case could be “daisy-chaining” of chart data updates. What this basically means is that instead of updating all charts at once, we queue updates so at any give time, only one chart is updating. When it’s done — dataUpdated event kicks in — we jump onto the next one in the queue, and so on.

This alone will ensure, that your dashboard page becomes much more responsive.

This demo shows how you can add functions, queue and events required for daisy-chained chart updates.

The post Optimizing multi-chart, periodically-updated dashboards appeared first on amCharts.

Serial chart with clickable links in rollover balloons

$
0
0

Normally the links in rollover balloons are not clickable because the chart disables it. To re-enable interactivity with contents of the balloon, such as links, set balloon’s property disableMouseEvents to false.

Another thing you will need to set is fixedPosition to true. It will ensure that balloon will be displayed in the fixed place, instead of follow the mouse, which would effectively make it impossible to roll over it.

"balloon": {
  "disableMouseEvents": false,
  "fixedPosition": true
}

The post Serial chart with clickable links in rollover balloons appeared first on amCharts.

Removing the outline from Pie chart slices

$
0
0

Normally, pie chart displays a small white outline around slices. You can change the outline color using outlineColor property.

However, if you don’t want to show any outline at all, you can simply set outlineColor to an empty string:

var chart = AmCharts.makeChart("chartdiv", {
  "type": "pie",
  "outlineColor": "",
  // ...
});

The post Removing the outline from Pie chart slices appeared first on amCharts.

Showing one balloon per column stack

Displaying an automatically-generated table of data synced with category axis

$
0
0

This demo will show how you can use AmCharts.addInitHandler() to automatically generate a table out of the data below the chart.

The code in handler – that is executed before the chart loads – will cycle through chart’s graphs and data and generate a <table> out of it.

Using CSS and chart’s settings we will make the table’s columns and chart’s category axis perfectly aligned. To achieve that, we are going to set the same value for chart’s marginLeft setting and the first column of the table to the same width.

The rest of the columns in table automatically align to take up the rest of the space at equal intervals, just like category axis, making them aligned.

The post Displaying an automatically-generated table of data synced with category axis appeared first on amCharts.


Pre-zooming chart’s value axis on chart load

$
0
0

The chart’s value axis can be zoomable. To enable zooming of the value axis, simply add valueScrollbar into your chart config.

If you also want the value axis to load pre-zoomed to certain values, use chart’s rendered event handler to call value axis’ zoomToValues() function.

"listeners": [{
  "event": "rendered",
  "method": function(e) {
    e.chart.valueAxes[0].zoomToValues(30, 70);
  }
}]

The post Pre-zooming chart’s value axis on chart load appeared first on amCharts.

Simulating box plot with candlestick and floating column graphs

$
0
0

This demo shows how you can simulate a box plot chart using overlaid candlestick and three separate floating column graphs.

Column graphs represent the three horizontal dividers for middle, high and low values. To make them appear as lines we set their valueField and openField to the same field in data, as well as lineThickness so it has an outline and therefore visible.

For a horizontal version of this chart, click here.

The post Simulating box plot with candlestick and floating column graphs appeared first on amCharts.

Showing countries on a World map selectively

$
0
0

If you need only some of the countries shown on the map, you can hide the other ones by setting unlistedAreasAlpha to zero. Add the ids of the areas you want to be shown to areas array, leave out the ones that need to be hidden.

This demo shows how you can apply the technique to show only the African continent, using World map.

Alternatively, if you are looking for continental maps, make sure you check out the Premium Map Pack, which has a lot of dedicated continental and regional maps available. The add-on is available for free for all commercial license holders.

The post Showing countries on a World map selectively appeared first on amCharts.

Sync chart cursor with HTML5 audio

Lazy-loading 120 charts on the same page

$
0
0

This demo shows how you can build a “smart scroll” jQuery plugin that would start initializing the chart only when it scrolls into view.

Having many charts on the same page might essentially bog down the browser if they start initializing all at once. By having only visible charts initializing, we avoid this issue, resulting in way better UX.

You can also check out this demo, which uses alternate approach to lazy-loading, by overriding the makeChart function.

The post Lazy-loading 120 charts on the same page appeared first on amCharts.

Viewing all 111 articles
Browse latest View live