API Reference
Namespaces
Classes
Events
Global
Externals

Class: Share

CIQ. Share


new Share()

Manages chart sharing and uploading.

See the Chart Sharing tutorial for more details.

Methods


createImage(stx [, params], cb)

Creates a png image of the current chart and everything inside the container associated with the chart when it was instantiated, including HTML. Elements outside the chart container will NOT be included.

If widthPX and heightPX are passed in then the image will be scaled to the requested dimensions.

It will dynamically try to load js/thirdparty/html2canvas.min.js if not already loaded.

This function is asynchronous and requires a callback function. The callback will be passed a data object or canvas which can be sent to a server or converted to an image.

Important Notes:

  • This method will rely on Promises. If your browser does not implement Promises, be sure to include a polyfill.

  • This method does not always work with Safari.

  • Canvases can only be exported if all the contents including CSS images come from the same domain, or all images have cross origin set properly and come from a server that supports CORS; which may or may not be possible with CSS images.

  • When using the charts from file:///, make sure to include html2canvas statically instead of allowing this method to load it dynamically.
    Example (adjust path as needed):
    <script src="js/thirdparty/html2canvas.min.js"></script>
    or
    import src="/js/thirdparty/html2canvas.min.js"

Parameters:
Name Type Argument Description
stx CIQ.ChartEngine

Chart object

params object <optional>

Parameters to describe the image.

Properties
Name Type Argument Description
widthPX number <optional>

Width of image to create. If passed then params.heightPX will adjust to maintain ratio.

heightPX number <optional>

Height of image to create. If passed then params.widthPX will adjust to maintain ratio.

imageType string <optional>

Specifies the file format your image will be output in. The default is PNG and the format must be supported by your browser.

hide array <optional>

Array of strings; array of the CSS selectors of the DOM elements to hide, before creating a PNG

cb function

Callback when image is available fc(data) where data is the serialized image object

Version:
  • ChartIQ Advanced Package plug-in
Since:
  • 3.0.0 Function signature changed to take parameters.
  • 4.0.0 Addition of parameters.hide.

getClipboard()

Attempts to read a shareID from the clipboard.

Since:
  • 9.0.0

Returns:

shareID

Type
Promise.<string>

loadChartFromID(stx, shareID)

Loads a chart with the configuration for the given shareID.

Parameters:
Name Type Description
stx CIQ.ChartEngine

Chart Object

shareID string

Share ID

Since:
  • 9.0.0

Returns:
Type
Promise.<void>

loadChartFromLayout(chart, configuration)

Loads a chart with the given configuration.

Parameters:
Name Type Description
chart object

Chart Object

configuration string

Share configuration

Since:
  • 9.0.0

Returns:
Type
Promise.<void>

onClipboard(cb)

Adds a listener for paste events on clipboard.

Parameters:
Name Type Description
cb function
Since:
  • 9.0.0


saveChartLayout(stx)

Persists the current chart layout to the cloud. Returns an ID that can be used to retrieve the layout from the cloud.

Parameters:
Name Type Description
stx CIQ.ChartEngine

Chart object

Since:
  • 9.0.0

Returns:

id

Type
Promise.<string>

shareChart(stx [, override], cb)

Convenience function that serves as a wrapper for createImage and uploadImage. It will create an image using the default parameters. If you wish to customize the image you must use CIQ.Share.createImage separately and then call CIQ.Share.uploadImage.

Parameters:
Name Type Argument Description
stx CIQ.ChartEngine

Chart Object

override object <optional>

Parameters that overwrite the default hosting location from https://share.chartiq.com to a custom location.

Properties
Name Type Argument Description
host object <optional>
path object <optional>
cb function

Callback when the image is uploaded.

Since:
  • 2015-11-01

Example
// here is the exact code this convenience function is using
	CIQ.Share.createImage(stx, {}, function(imgData){
		var id=CIQ.uniqueID();
		var host="https://share.chartiq.com";
		var url= host + "/upload/" + id;
		if(override){
			if(override.host) host=override.host;
			if(override.path) url=host+override.path+"/"+id;
		}
		var startOffset=stx.getStartDateOffset();
		var metaData={
			"layout": stx.exportLayout(),
			"drawings": stx.exportDrawings(),
			"xOffset": startOffset,
			"startDate": stx.chart.dataSegment[startOffset].Date,
			"endDate": stx.chart.dataSegment[stx.chart.dataSegment.length-1].Date,
			"id": id,
			"symbol": stx.chart.symbol
		};
		var payload={"id": id, "image": imgData, "config": metaData};
		CIQ.Share.uploadImage(imgData, url, payload, function(err, response){
			if(err!==null){
				CIQ.alert("error sharing chart: ",err);
			}else{
				cb(host+response);
			}
		});
		// end sample code to upload image to a server
	});

uploadImage(dataImage, url [, payload], cb)

Uploads an image to a server. The callback will take two parameters. The first parameter is an error condition (server status), or null if there is no error. The second parameter (if no error) will contain the response from the server. 'payload' is an optional object that contains meta-data for the server. If payload exists then the image will be added as a member of the payload object, otherwise an object will be created 'dataImage' should be a data representation of an image created by the call canvas.toDataURL such as is returned by CIQ.Share.createImage If you are getting a status of zero back then you are probably encountering a cross-domain ajax issue. Check your access-control-allow-origin header on the server side

Parameters:
Name Type Argument Description
dataImage string

Serialized data for image

url string

URL to send the image

payload object <optional>

Any additional data to send to the server should be sent as an object.

cb function

Callback when image is uploaded

Version:
  • ChartIQ Advanced Package plug-in