<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Export Chart as PNG image to Server</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>	
		<!-- jquery library is used to create AJAX calls -->
		<script type="text/javascript" language="javascript" src="./js/jquery.min.js"></script>
		<script type="text/javascript" language="javascript" src="./../js/AnyChartStock.js?v=1.9.0r9317"></script>
		<!-- chart size settings -->
		<style type="text/css">
			#chartContainer {
				width: 700px;
				height: 500px;
			}
		</style>
		
		<script type="text/javascript" language="javascript">
			// Creating new chart object. 
			var chart = new AnyChartStock("./../swf/AnyChartStock.swf?v=1.9.0r9317", "./../swf/Preloader.swf?v=1.9.0r9317");
			// Setting XML config file.
			chart.setXMLFile("config.xml");
			// Writing the flash object into the page DOM.
			chart.write("chartContainer");
			
			//--------------------------------------------------------------------------------
			//		Exporting
			//--------------------------------------------------------------------------------
			
			function getBase64PNGImage() {
				// object with settings
				var exportSettings = {
					backgroundColor: $("#bgColor").val(), // background color
					transparent: $("#transparent").is(":checked") // transparency
				};
				
				var useCustomSize = $("#useCustomSize").is(":checked"); // use custom or original size
				if (useCustomSize) {
					exportSettings.size = "Custom"; // custom size is used
					exportSettings.width = $("#imgWidth").val(); // custom image width
					exportSettings.height = $("#imgHeight").val(); // custom image height
					exportSettings.resizingMode = $("#resizingMode").val(); // resizing mode
					exportSettings.cutExtraSpace = $("#cropExtraSpace").is(":checked"); // cut extra space settings
				}else {
					exportSettings.size = "Original"; //  use original size
				}
				
				return chart.getPNGImageBase64Encoded(exportSettings);
			}
			
			function exportImage() {	
				// POST query to save.php
				$.post("save.php", { // post params
						imgType: "png", 
						timestamp: new Date().getTime(),
						imgData: getBase64PNGImage()
					},
					function(data) { // load handler
						alert ("Image was saved to the server: "+data);
						$("#res").attr("src",data); // set image src
					});
			}
			
			//--------------------------------------------------------------------------------
			//		Auxiliary Interface Functions
			//--------------------------------------------------------------------------------
            
			// Function to update inputs states depending on custom size flag state
			function checkCustomSize() {
				var disable = !$("#useCustomSize").is(":checked");
				$("#imgWidth").attr("disabled", disable);
				$("#imgHeight").attr("disabled", disable);
				$("#resizingMode").attr("disabled", disable);
				$("#cropExtraSpace").attr("disabled", disable);
			}
            
			// Function to update inputs states depending on resizing mode
			function checkResizingMode() {
				if (!$("#useCustomSize").is(":checked")) return;
            
				var mode = $("#resizingMode").val();
				$("#cropExtraSpace").attr("disabled", mode == "Stretch" || mode == "Recalculate");
			}
			
			// Function to disable/enable bgColor depending on transparency settings
			function checkTransparent() {
				$("#bgColor").attr("disabled",$("#transparent").is(":checked"));
			}
			
			//---------------------------------------------------------------------------------------------------
			//		html events binding
			//---------------------------------------------------------------------------------------------------
			
			$(function() {
				$("#useCustomSize").change(checkCustomSize);
				$("#resizingMode").change(checkResizingMode);
				$("#transparent").change(checkTransparent);
			});
			
			//---------------------------------------------------------------------------------------------------
			//		Enable html controls
			//---------------------------------------------------------------------------------------------------
			
			chart.onChartDraw = function() {
				$("#useCustomSize").removeAttr("disabled");
				$("#btnExport").removeAttr("disabled");
				$("#bgColor").removeAttr("disabled");
				$("#transparent").removeAttr("disabled");
				
				chart.onChartDraw = null;
			}
		</script>
		<!-- table settings -->
		<style type="text/css">
			table.settings {
				border-style: solid;
				border-width: 1px;
				border-color: #D0CDC9;
			}

			table.settings tr th {
				font:normal 60% Verdana;
				background-color: #DCD9D5;
				font-weight:bold;
				padding-bottom:5px;
				padding-top:5px;
				padding-left:10px;
				text-align:left;
			}
			
			table.settings tr td {
				background-color: #F8F4F0;
				font:normal 70% Verdana;
				padding-bottom:2px;
				padding-top:2px;
				padding-left:10px;
				text-align:left;	
			}
		</style>
	</head>
	<body>
		<table>
			<tr valign="top">
				<td>
					<table class="settings">
						<tr>
							<th>Source chart:</th>
						</tr>
						<tr>
							<td id="chartContainer"><!-- Chart Container --></td>
						</tr>
					</table>
				</td>
				<td>
					<table class="settings">
						<tr>
							<th colspan="2">PNG settings</th>
						</tr>
						<!-- transparent -->
						<tr>
							<td>Transparent:</td>
							<td><input type="checkbox" id="transparent" disabled="disabled" autocomplete="off" /></td>
						</tr>
						<!-- background color -->
						<tr>
							<td>bgColor:</td>
							<td><input type="text" id="bgColor" value="#FFFFFF" disabled="disabled" autocomplete="off" /></td>
						</tr>
						
						<tr>
							<th colspan="2">Size settings:</th>
						</tr>
						
						<!-- use custom size -->
						<tr>
							<td>Use custom size:</td>
							<td><input type="checkbox" id="useCustomSize" disabled="disabled" autocomplete="off" /></td>
						</tr>
						
						<!-- width -->
						<tr>
							<td>Width:</td>
							<td><input type="text" disabled="disabled" id="imgWidth" autocomplete="off" value="550" /></td>
						</tr>
						<!-- height -->
						<tr>
							<td>Height:</td>
							<td><input type="text" disabled="disabled" id="imgHeight" autocomplete="off" value="400"></td>
						</tr>
						<!-- resizing mode -->
						<tr>
							<td>Resizing mode:</td>
							<td><select id="resizingMode" disabled="disabled" autocomplete="off">
									<option value="Fit">Fit</option>
									<option value="Recalculate">Recalculate</option>
									<option value="Stretch">Stretch</option>
									<option value="RecalculateByProportions">RecalculateByProportions</option>
								</select></td>
						</tr>
						<!-- crop extra space -->
						<tr>
							<td>Crop extra space:</td>
							<td><input type="checkbox" disabled="disabled" id="cropExtraSpace" autocomplete="off"></td>
						</tr>
	 				</table>
					<!-- export -->
					<table width="100%">
						<tr><td align="right"><input type="button" id="btnExport" disabled="disabled" value="Export to Server As PNG Image" onclick="exportImage()" /></td></tr>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<table class="settings" width="100%">
						<tr>
							<th>Image:</th>
						</tr>
						<tr>
							<td><img id="res" /></td>
						</tr>
					</table>
				</td>
				<td></td>
			</tr>
		</table>
	</body>
</html>

Sample Description

How to use this sample?

In this sample you can tune any PNG export settings and then export chart as PNG image and send it to server by clicking "Export to Server As PNG Image" button.

to top

How it works

AnyStock allows to obtain a snapshot of the chart being displayed to user in the Base64-encoded format and passing that to the server script by an AJAX-assisted POST request. The server-side script, in its turn, can handle that data any way you need it - save it, pass or modify, and so on.

This sample creates exportSettings object based on user settings and then invokes getPNGImageBase64Encoded function to get PNG chart snapshot, which is passed to exportImage function, that sends base64 encoded image to save.php script.

You can read more about this feature at: Export to a Server article.

to top

AnyChartStock JavaScript API

This sample uses the following methods, properties and events from AnyChartStock JavaScript API:

Item Type Description
getPNGImageBase64Encoded Method Gets a chart screenshot as a base64 encoded PNG image.
setXMLFile Method Sets chart XML configuration file path.
write Method Adds the chart to HTML DOM as a child of the specified container.
onChartDraw Event This event is dispatched when the AnyChart Stock is drawn.

to top

Prerequisites

This section lists all configuration, data and auxiliary files required for this sample.

Configuration file

SWF files

  • AnyChartStock.swf - AnyChart Stock component.
  • Preloader.swf - AnyChart Stock helper component that loads the main component (AnyChartStock.swf) and displays loading progress.

JavaScript Libraries

  • jquery.min.js - A JavaScript jQuery library. Learn more at jQuery official site.
  • AnyChartStock.js - A JavaScript library that is shipped with AnyChart Stock component. It is used to embed the component into HTML DOM and to comunicate with the Flash part.

Additional Files

to top

The information contained in this website is for general information purposes only. All sample data provided on this site is for demonstration purposes only.

The logos and names of other companies and products mentioned on this site are copyright and/or trademarks of their respective owners.

The content on this site, including news, quotes, data and other information, is provided for your personal information only, and is intended for demonstration purposes only. Content on this site is not appropriate for the purposes of making a decision to carry out a transaction or trade. Nor does it provide any form of advice (investment, tax, legal) amounting to investment advice, or make any recommendations regarding particular financial instruments, investments or products.

In no event AnyChart will be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website.

This site may point to other Internet sites that may be of interest to you, however AnyChart does not endorse or take responsibility for the content on such other sites

Market data and News provided by and copyright RediNews, Incorporated.