JavaScript Events

Overview

AnyChart dispatches a lot of chart events that can be handled within JavScript Code, the most convenient way to use this feature - use AnyChart JavaScript Integration Library.

When event is dispatched AnyChart passes a lot of data about the chart, so you can define what point was clicked and do some actions using this information.

In this tutorial we will list all available events, explain when they are dispatched and what data is avaialble in them. There are also several samples showing how to create event handler and how to use data coming from AnyChart.

addEventListener method of JavaScript Library is used to set event handler:

Method Parameters Description
addEventListener(event, handler)
Returns: null
event: String - event type
handler: Function - function called when event occurs
Add listener to the event
You may call this method before write()

The following event parameter values (events) are available:

Value Event Description
pointClick Occurs when user clicks data point.
pointDoubleClick Occurs when user doubleclicks on data point.
pointMouseOver Occurs when user moves mouse over the point.
pointMouseOut Occurs when mouse cursor leaves point.
pointMouseUp Occurs when mouse button is released.
pointMouseDown Occurs when mouse button is pushed.
pointSelect Occurs when user selects data point (point should be selectable)
pointDeselect Occurs when user deselects data point (point should be selectable)
create Occurs when chart swf is loaded.
draw Occurs when chart is displayed.
render Occurs when chart is rendered, but not displayed.
renderView Occurs when dashboard view is rendered, but not displayed.
drawView Occurs when chart in view is displayed.
refresh Occurs when chart changes after JS Data Manipulation methods.
refreshView Occurs when view changes after JS Data Manipulation methods.

Point Events

Point events can be used to get data about point when user clicks,selects or hovers certain point on the chart.

pointClick, pointMouseOver and pointSelect recieve all information about the point, series, category and data plot, full list of available fields can be found in the end this article.

To add point event listener you should add the following code:

// Create new chart
var chart = new AnyChart();
// Add event handler for point event
chart.addEventListener('pointClick', onPointClick);

Where onPointClick is a function that will handle an event, this function must have one parameter of Object type, which returns event data:

// Point Click Event Handler
function onPointClick(e) {
// Read point name
name=e.data.Name;
// Read point value
value=e.data.YValue;
// Read custom point attribute
attribute = e.data.Attributes['test'];
}

In the sample below you will find all you need to know to create own point events handlers.

to top

Sample - Handling Point Events

In this sample we will add one chart to the page that will receive data from XML file and add handler to all point events, in event handlers we will read data point name, value and one custom attribute - these values will be diplayed on the page.

Launch the sample with Point Events handling: Open the Sample

Download the sample with Point Events handling: Download the Sample

In the sample page we create a table and place <div> tags in cells that will be used to hold chart and point data recieved, like that:

Chart container:

<td rowspan="4" height="420">
<!-- in this div chart will be placed -->
<div id="chartContainer"></div>
</td>

Point info container:

<td width="100%">
<b>Point Click</b>
<!-- in this div data from chart will be placed -->
<div id="pointClickLog"></div>
</td>

Then we will add code to body onload event that will load charts when page is loaded:

<body onLoad="init()">

init function will create new chart, set handlers and write chart to the page:

function init() {
// Create new chart
var chart = new AnyChart();
// Add event handlers for all point events
chart.addEventListener('pointClick', onPointClick);
chart.addEventListener('pointDoubleClick', onPointDoubleClick);
chart.addEventListener('pointSelect', onPointSelect);
chart.addEventListener('pointMouseOver', onPointMouseOver);
chart.addEventListener('pointMouseOut', onPointMouseOut);
// Set data XML File
chart.setXMLFile('./data.xml');
//Output chart to "chartContainer" div
chart.write('chartContainer');
}

And here is a sample handler function:

// Point Click Event Handler
function onPointClick(e) {
// Read point name
name=e.data.Name;
// Read point value
value=e.data.YValue;
// Read custom point attribute
attribute = e.data.Attributes['test'];

// Write the info to the page
info = 'Point Name: ' + name + ', Value: ' + value + '<br>' + 'Test Attribute: ' + attribute;

document.getElementById('pointClickLog').innerHTML = info;
}

Here it is, you can see in the sample that info is read and placed on the page, you can implement any custom logic based on chart data output, study all available fields - you may find them useful.

to top

Sample - Handling Map Regions Events

In this sample we will add one map to the page that will receive data from XML file and add handlers to all point events, in event handlers we will read region name, value and custom attributes - these values will be displayed on the page.

The idea of the sample is the same as in Point Events sample above, read it if you have questions about this sample.

One important difference between chart and map events samples is that maps sample uses allow_multiple_select feature to allow collecting data about several regions selected.

The Map Region events sample can be used as a draft for creating ZIP, Fips, State, County ot Country Selector control for your web-systems.

Launch the sample with Map Region Events handling: Open the Sample

Download the sample with Map Region Events handling: Download the Sample

to top

Chart Events

You can handle chart creation/draw events to check when chart is loaded and displayed, moreover in render events you can read all chart data and process it how you like, for example output data in other way on the page.

render and renderView events receive all chart data: chart titles, data sereis array, data points array and all precalculated values, you will find full list an the end of this article.

Note that when you update chart or dashboard view using these JS Data Manipulation methods - no render or renderView occurs, only refresh or refreshView.

To add chart event listener you should add the following code:

// Create new chart
var chart = new AnyChart();
// Add Render event listener
chart.addEventListener('render', onChartRender);

Where onChartRender is a function that will handle an event, this function must have one parameter of Object type:

// Render Event handler
function onChartRender(e) {

// Store all chart data in variable for later use
chartData = chart.getInformation();
// Read certain data fields
chartTitle = chartData.Title;
// Number of series - length of Series array
numberOfSeries = chartData.Series.length;
numberOfPoints = chartData.PointCount;
}

In the sample below you will find all you need to know to create own chart events handlers.

to top

Sample - Handling Chart Events

In this sample we will add one chart to the page that will receive data from XML file and add handler to chart render event, in event handler we will read all chart data and store it in a variable, then this variable will be used to show all chart data on the page.

Launch the sample with Chart Draw/Render Event handling: Open the Sample

Download the with Chart Draw/Render Event handling: Download the Sample

First we will create layout for chart and chart info:

<table border="1" cellspacing="0" cellpadding="0">
<tr valign="Top">
<td>
<!-- div where chart will be placed-->
<div id="chartDiv"></div>
</td>
<td>
<!-- Button to launch show all chart data -->
<input style="display:none;" id="button"
value="Show all chart data" type="Submit" onclick="showChartData()">

<!-- div for chart data output -->
<div id="chartInfo"></div>
</td>
</tr>
</table>

Second, we will add code to body onload event that will load charts when page is loaded:

<body onLoad="init()">

Then we will create init function that will place a chart on the page and set chart render event handler:

function init()
{
// Create new chart
chart = new AnyChart();
// Add Render event listener
chart.addEventListener('render', onChartRender);
// Set data file
chart.setXMLFile('./data.xml');
// Write chart to "chartDiv" div
chart.write('chartDiv');
}

Event listener look like that:

// Render Event handler
function onChartRender(e) {
// Store all chart data in variable for later use
chartData = chart.getInformation();
// Read chart info
info = 'Chart "'+ chartData.Title + '" is rendered, click the button to see all data.<br> Chart has ' +
chartData.Series.length + ' series, with ' + chartData.PointCount +
' points in them.' ;
// Write info to div
document.getElementById('chartInfo').innerHTML = info;
// Enable button that will output all chart data
document.getElementById('button').style.display = "block";
}

And, finally, here is a code of showChartData function, that will put chart dat on a page. This function uses a variable that stores all chart info (chartData was assigned in render event handler), it iterates throigh Series array, and Points array in each of the series:

// Function that shows all chart data
function showChartData()
{
info = '';
// Iterate through series array
for (var i = 0; i<chartData.Series.length;i++)
{
  // Output series name
  info = info + "<b>"+ chartData.Series[i].Name + "</b><br>";
  // Iterate through points array in series
  for (var j = 0; j<chartData.Series[i].Points.length;j++)
  {
    //Output points value
    info = info+ "&nbsp;&nbsp;" + chartData.Series[i].Points[j].Name + " = " +
    chartData.Series[i].Points[j].YValue + "<br>";
  }
}
document.getElementById('chartInfo').innerHTML = info;
}

Here it is, you can see in the sample that info is read and placed on the page, you can implement any custom logic based on chart data output, study all available fields - you may find them useful.

to top

List of Values Available in Point Events

pointClick, pointMouseOver, pointSelect

Point data fields

Field Type
data.YValue The y value of this point.
data.YPercentOfCategory The percentage of all the points with the same name this point represents.
data.YPercentOfSeries The percentage of the series this point represents.
data.YPercentOfTotal The percentage of all the series on the chart this point represents.
data.High The high value of this point (OHLC, Candlestick).
data.Low The low value of this point (OHLC, Candlestick).
data.Open The open value of this point (OHLC, Candlestick).
data.Close The close value of this point (OHLC, Candlestick).
data.XValue The x value of this point (Scatter Plot charts).
data.XPercentOfSeries The percentage of the series this point represents (Scatter Plot charts).
data.XPercentOfTotal The percentage of all the series on the chart this point represents.
data.BubbleSize The bubble size value of this point (Bubble chart).
data.BubbleSizePercentOfCategory The percentage of all the points with the same name this point represents (Categorized charts).
data.BubbleSizePercentOfSeries The percentage of the series this point represents.
data.BubbleSizePercentOfTotal The percentage of all the series on the chart this point represents.
data.Name The name of this point.
data.Index The index of this point in the series this point represents (zero-based).
data.RangeStart The starting value of this point (Range charts).
data.RangeEnd The ending value of this point (Range charts).
data.Range The range of this point (RangeEnd - RangeStart).
data.Attributes[] Collection of Attributes - a name of attribute is the key.

to top

Point Series Data Fields

Field Description
data.Series.Name The name of this series.
data.Series.FirstXValue The x value of the first point in this series (Scatter plot charts).
data.Series.FirstYValue The y value of the first point in this series.
data.Series.LastXValue The x value of the last point in this series (Scatter plot charts).
data.Series.LastYValue The y value of the first point in this series.
data.Series.YSum The sum of all the points y values.
data.Series.XSum The sum of all the points x values (Scatter plot charts).
data.Series.BubbleSizeSum The sum of all the points bubble sizes (Bubble chart).
data.Series.YMax The maximal y value of all the elements within this series.
data.Series.YMin The minimal y value of all the elements within this series.
data.Series.XMax The maximal x value of all the elements within this series (Scatter plot charts).
data.Series.XMin The minimal x value of all the elements within this series (Scatter plot charts).
data.Series.BubbleMaxSize The maximal bubble size value of all the points within this series (Bubble chart).
data.Series.BubbleMinSize The minimal bubble size value of all the points within this series (Bubble chart).
data.Series.YAverage The average y value of all the points within this series.
data.Series.XAverage The average x value of all the points within this series.
data.Series.BubbleSizeAverage The average bubble size value of all the points within this series (Bubble chart).
data.Series.YMedian The median y value of all the points within this series.
data.Series.XMedian The median x value of all the points within this series (Scatter plot charts).
data.Series.BubbleSizeMedian The median bubble size value of all the points within this series (Bubble chart).
data.Series.YMode The mode y value of all the points within this series.
data.Series.XMode The mode x value of all the points within this series (Scatter plot charts).
data.Series.BubbleSizeMode The mode bubble size value of all the points within this series (Bubble chart).
data.Series.PointCount The number of points in this series.
data.Series.YAxisName The title text of the Y Axis.
data.Series.XAxisName The title text of the X Axis.
data.Series.YRangeMax The maximal range in this series (Range charts).
data.Series.YRangeMin The minimal range in this series (Range charts).
data.Series.YRangeSum The sum of all ranges in this series (Range charts).
data.Series.Attributes[] Collection of Attributes - a name of attribute is the key.

to top

Point Category Data Fields

Field Description
data.Category.YPercentOfTotal The percent of all the data on the chart this category represents.
data.Category.YSum The sum of all the points within this category.
data.Category.YAverage The average of all the points within this category.
data.Category.YMedian The median of all the points within this category.
data.Category.YMode The mode of all the points within this category.
data.Category.Name The name of the category.
data.Category.YRangeMax The maximal range in this category (Range charts).
data.Category.YRangeMin The minimal range in this category (Range charts).
data.Category.YRangeSum The sum of all ranges in this category (Range charts).

to top

Point Data Plot Fields

Field Description
data.Series.Plot.YSum The sum of all the points y values.
data.Series.Plot.XSum The sum of all the points x values (Scatter plot charts).
data.Series.Plot.BubbleSizeSum The sum of all the points bubble sizes (Bubble chart).
data.Series.Plot.YMax The maximal of all the points y values.
data.Series.Plot.YMin The minimal of all the points y values.
data.Series.Plot.XMax The maximal of all the points x values (Scatter plot chart).
data.Series.Plot.XMin The minimal of all the points x values (Scatter plot chart).
data.Series.Plot.BubbleMaxSize The maximal of all the points bubble sizes (Bubble chart).
data.Series.Plot.BubbleMinSize The minimal of all the points bubble sizes (Bubble chart).
data.Series.Plot.XAverage The average x value of all the points (Scatter plot charts).
data.Series.Plot.YAverage The average y value of all the points.
data.Series.Plot.BubbleSizeAverage The average bubble size of all the points (Scatter plot charts).
data.Series.Plot.MaxYValuePointName The name of the point with a maximal of all the points y values.
data.Series.Plot.MinYValuePointName The name of the point with a minimal of all the points y values.
data.Series.Plot.MaxYValuePointSeriesName The name of the series with a maximal of all the points y values.
data.Series.Plot.MinYValuePointSeriesName The name of the series with a minimal of all the points y values.
data.Series.Plot.MaxYSumSeriesName The name of the series with a maximal sum of the points y values.
data.Series.Plot.MinYSumSeriesName The name of the series with a minimal sum of the points y values.
data.Series.Plot.YRangeMax The maximal of the ranges of the points within the chart.
data.Series.Plot.YRangeMin The minimal of the ranges of the points within the chart.
data.Series.Plot.YRangeSum The sum of the ranges of the points within the chart.
data.Series.Plot.PointCount The number of the points within the chart.
data.Series.Plot.SeriesCount The number of the series within the chart.

to top

Point Y-Axis Fields

Field Description
data.Series.YAxis.Sum The sum of all y values of all points in series that are bound to this axis.
data.Series.YAxis.BubbleSizeSum The sum of all bubble sizes of all points in series that are bound to this axis.
data.Series.YAxis.Max The maximal y value of all points in series that are bound to this axis.
data.Series.YAxis.Min The minimal y value of all points in series that are bound to this axis.
data.Series.YAxis.BubbleSizeMax The maximal bubble size of all points in series that are bound to this axis.
data.Series.YAxis.BubbleSizeMin The minimal bubble size of all points in series that are bound to this axis.
data.Series.YAxis.Average The average y value of all points in series that are bound to this axis.
data.Series.YAxis.Median The median y value of all points in series that are bound to this axis.
data.Series.YAxis.Mode The mode y value of all points in series that are bound to this axis.
data.Series.YAxis.Name The name of the axis.

to top

Point X-Axis Fields

Field Description
data.Series.XAxis.Sum The sum of all x values of all points in series that are bound to this axis.
data.Series.XAxis.BubbleSizeSum The sum of all bubble sizes of all points in series that are bound to this axis.
data.Series.XAxis.Max The maximal x value of all points in series that are bound to this axis.
data.Series.XAxis.Min The minimal x value of all points in series that are bound to this axis.
data.Series.XAxis.BubbleSizeMax The maximal bubble size of all points in series that are bound to this axis.
data.Series.XAxis.BubbleSizeMin The minimal bubble size of all points in series that are bound to this axis.
data.Series.XAxis.Average The average x value of all points in series that are bound to this axis.
data.Series.XAxis.Median The median x value of all points in series that are bound to this axis.
data.Series.XAxis.Mode The mode x value of all points in series that are bound to this axis.
data.Series.XAxis.Name The name of the axis.

to top

pointMouseOut

No data available for pointMouseOut - it can be used to undo actions done on pointMouseOver.

to top

List of Values Available in Chart Events

There are two cases in chart events: you single chart mode and dashboard mode.

If you want to get all information about chart you need to invoke something like:

data = chart.getInformation();

and then use collections listed below.

render, renderView events

Render can receive all chart data as if it is a single chart.

Dashboard mode:

Field Description
data.Views[] Array of views with charts, each view[i] has all Single Chart fields
data.Views[].chartName Dashboard Chart Name
data.Views[].viewName Dashboard View Name

Single chart mode:

Field Description
data.* All fields from Plot , for example: data.YSum
data.XAxes[].* Collection of X Axes, each axis contain all X Axis fields, for example:
data.XAxes["X Axis"].Name
data.YAxes[].* Collection of Y Axes, each axis contain all Y Axis fields, for example:
data.YAxes["Y Axis"].Name
data.Categories[].* Collection of Categories, each category contain all Category fields, for example:
data.Categories["A"].Name
data.Series[].* Array of Series, each series contain all Series Fields, for example:
data.Series[0].Name
data.Series[].Points[].* Array of Points in series, each point contain all Point Fields, for example:
data.Series[0].Points[0].YValue

to top

drawView

Draw view receives only view field that hold the name of the view.

Field Description
view The name of the view displayed, for example: e.view.

to top

create, draw

Create and Draw events do not receive any data.

 

to top