Widget-Methods

Here is a list of supported widget's methods. You can call them using widget object returned to you by widget's constructor.

Remark: Please note that it's safe to call any method only after onChartReady callback is fired. So the common practice is to do smth like

widget.onChartReady(function() {
    // now it's safe to call any other widget's methods
});

Methods

Before 1.5 [[Chart Methods]] belonged to the Widget. Please see the full list of actions [[here|Chart-Methods]]

  • Subscribing To Chart Events
    • [[onChartReady(callback)|Widget-Methods#onchartreadycallback]]
    • [[onGrayedObjectClicked(callback)|Widget-Methods#ongrayedobjectclickedcallback]]
    • [[onShortcut(shortcut, callback)|Widget-Methods#onshortcutshortcut-callback]]
    • [[subscribe(event, callback)|Widget-Methods#subscribeevent-callback]]
    • [[unsubscribe(event, callback)|Widget-Methods#unsubscribeevent-callback]]
  • Chart Actions
    • [[chart()|Widget-Methods#chart]]
    • [[setLanguage(locale)|Widget-Methods#setlanguagelocale]]
    • [[setSymbol(symbol, interval, callback)|Widget-Methods#setsymbolsymbol-interval-callback]]
    • [[remove()|Widget-Methods#remove]]
    • [[closePopupsAndDialogs()|Widget-Methods#closepopupsanddialogs]]
    • [[selectLineTool(drawingId)|Widget-Methods#selectlinetooldrawingid]]
    • [[selectedLineTool()|Widget-Methods#selectedlinetool]]
  • Saving/Loading Charts
    • [[save(callback)|Widget-Methods#savecallback]]
    • [[load(state)|Widget-Methods#loadstate]]
    • [[getSavedCharts(callback)|Widget-Methods#getsavedchartscallback]]
    • [[loadChartFromServer(chartRecord)|Widget-Methods#loadchartfromserverchartrecord]]
    • [[saveChartToServer(onCompleteCallback, onFailCallback, saveAsSnapshot, options)|Widget-Methods#savecharttoserveroncompletecallback-onfailcallback-saveassnapshot-options]]
    • [[removeChartFromServer(chartId, onCompleteCallback)|Widget-Methods#removechartfromserverchartid-oncompletecallback]]
  • Custom UI Controls
    • [[onContextMenu(callback)|Widget-Methods#oncontextmenucallback]]
    • [[createButton(options)|Widget-Methods#createbuttonoptions]]
  • Dialogs
    • [[showNoticeDialog(params)|Widget-Methods#shownoticedialogparams]]
    • [[showConfirmDialog(params)|Widget-Methods#showconfirmdialogparams]]
    • [[showLoadChartDialog()|Widget-Methods#showloadchartdialog]]
    • [[showSaveAsChartDialog()|Widget-Methods#showsaveaschartdialog]]
  • Getters
    • [[symbolInterval(callback)|Widget-Methods#symbolintervalcallback]]
    • [[mainSeriesPriceFormatter()|Widget-Methods#mainseriespriceformatter]]
    • [[getIntervals()|Widget-Methods#getintervals]]
    • [[getStudiesList()|Widget-Methods#getstudieslist]]
  • Customization
    • [[addCustomCSSFile(url)|Widget-Methods#addcustomcssfileurl]]
    • [[applyOverrides(overrides)|Widget-Methods#applyoverridesoverrides]]
    • [[applyStudiesOverrides(overrides)|Widget-Methods#applystudiesoverridesoverrides]]
  • :chart: [[Trading Terminal]] specific
    • [[showSampleOrderDialog(order)|Widget-Methods#chart-showsampleorderdialogorder]]
    • [[watchList()|Widget-Methods#chart-watchlist]]
  • :chart: Multiple Charts Layout
    • [[chart(index)|Widget-Methods#chart-chartindex]]
    • [[activeChart()|Widget-Methods#chart-activechart]]
    • [[chartsCount()|Widget-Methods#chart-chartscount]]
    • [[layout()|Widget-Methods#chart-layout]]
    • [[setLayout(layout)|Widget-Methods#chart-setlayoutlayout]]

Subscribing To Chart Events

onChartReady(callback)

  1. callback: function()

The Charting Library will call the callback provided once when chart is initialized and ready. You can safely call all other methods from this moment.

onGrayedObjectClicked(callback)

  1. callback: function(subject)
    1. subject: object {type, name}
      1. type: drawing | study
      2. name: string, name of a subject which was clicked

The Library will call the callback provided every time when user clicks on grayed out object. Example:

new TradingView.widget({
    drawings_access: {
        type: "black",
        tools: [
            { name: "Regression Trend" },
            { name: "Trend Angle", grayed: true },
        ]
    },
    studies_access: {
        type: "black",
        tools: [
            { name: "Aroon" },
            { name: "Balance of Power", grayed: true },
        ]
    },
    <...> // other widget settings
});

widget.onChartReady(function() {
    widget.onGrayedObjectClicked(function(data) {
        // this function will be called when one tries to
        // create Balance Of Power study or Trend Angle shape

        alert(data.name + " is grayed out!");
    })
});

onShortcut(shortcut, callback)

  1. shortcut
  2. callback: function(data)

The Library will call the callback provided every time when shortcut is pressed.

Example:

widget.onShortcut("alt+s", function() {
  widget.executeActionById("symbolSearch");
});

subscribe(event, callback)

  1. event: can be
Event name Library Version Description
toggle_sidebar drawing toolbar is shown/hidden
indicators_dialog Indicators dialog is shown
toggle_header chart heder is shown/hidden
edit_object_dialog Chart/Study Properties dialog is shown
chart_load_requested new chart about to be loaded
chart_loaded
mouse_down
mouse_up
drawing 1.7 a drawing is added to a chart. Arguments contains an object with value field that is the name of the drawing.
study 1.7 an indicator is added to a chart. Arguments contains an object with value field that is the name of the indicator.
undo 1.7
redo 1.7
reset_scales 1.7 reset scales button is clicked
compare_add 1.7 Compare dialog is shown
add_compare 1.7 Compare instrument is added
load_study template 1.7 A study template is loaded
onTick callback will be called every time when recent bar updates
onAutoSaveNeeded callback will be called every time when user changes the chart. Chart change means any user action that can be undone. The callback will not be called more than once in five seconds. See also auto_save_delay
onScreenshotReady callback will be called every time when user creates a screenshot and server returns the created image name
onMarkClick callback will be called every time when user clicks a [[mark on bar Marks-On-Bars]]. Mark ID will be passed as an argument
onTimescaleMarkClick callback will be called every time when user clicks a timescale mark. Mark ID will be passed as an argument
onSelectedLineToolChanged callback will be called every time when selected line tool changes
:chart: layout_about_to_be_changed amount or placement of charts about to be changed
:chart: layout_changed amount or placement of charts is changed
:chart: activeChartChanged active chart is changed
  1. callback: function(arguments)

The library will call callback when GUI event is happened. Every event can have different set of arguments.

unsubscribe(event, callback)

Unsubscribe a previously subscribed callback listener from a given event (which is one of the events in the table above).

Chart Actions

chart()

Returns a chart object that you can use to call [[Chart-Methods]]

setLanguage(locale)

  1. locale: [[language code|Localization]]

Sets the Widget's language. For now, this call reloads the chart. Please avoid using it.

setSymbol(symbol, interval, callback)

  1. symbol: string
  2. interval: string
  3. callback: function()

Makes the chart to change its symbol and resolution. Callback is called after new symbol's data arrived.

remove()

Removes chart widget from your page.

closePopupsAndDialogs()

Calling this method closes a context menu or a dialog if it is shown.

selectLineTool(drawingId)

  1. drawingId: may be one of the [[identifiers|Shapes and Overrides]] or
    1. cursor
    2. dot
    3. arrow_cursor
    4. eraser
    5. measure
    6. zoom
    7. brush

Selection of a drawing or cursor which is identical to a single click on a drawing button.

selectedLineTool()

Returns an [[identifier|Shapes and Overrides]] of the selected drawing or cursor (see above).

Saving/Loading Charts

save(callback)

  1. callback: function(object)

Saves the chart state to JS object. Charting Library will call your callback and pass the state object as argument. This call is a part of low-level [[save/load API|Saving-and-Loading-Charts]].

load(state)

  1. state: object

Loads the chart from state object. This call is a part of low-level [[save/load API|Saving-and-Loading-Charts]].

getSavedCharts(callback)

  1. callback: function(objects)

objects is an array of:

  1. id
  2. name
  3. image_url
  4. modified_iso
  5. short_symbol
  6. interval

Returns a list of chart descriptions saved on a server for current user.

loadChartFromServer(chartRecord)

  1. chartRecord is an object that you get using [[getSavedCharts(callback)|Widget-Methods#getsavedchartscallback]]

Loads and displays a chart from a server.

saveChartToServer(onCompleteCallback, onFailCallback, saveAsSnapshot, options)

  1. onCompleteCallback: function()
  2. onFailCallback: function()
  3. saveAsSnapshot: should be always false
  4. options: object { chartName }
    1. chartName: name of a chart. Should be specified for new charts and renaming.
    2. defaultChartName: default name of a chart. It will be used if current chart has no name.

Saves current chart to the server.

removeChartFromServer(chartId, onCompleteCallback)

  1. chartId: id should be got from a record received using [[getSavedCharts(callback)|Widget-Methods#getsavedchartscallback]]
  2. onCompleteCallback: function()

Removes chart from the server.

Custom UI Controls

onContextMenu(callback)

  1. callback: function(unixtime, price). This callback is expected to return a value (see below).

The Library will call the callback provided every time when user opens context menu on the chart. Unix time and price of context menu point will be provided as arguments. To customize context menu items you have to return array of items descriptors. Item descriptor has following structure:

{
    position: 'top' | 'bottom',
    text: 'Menu item text',
    click: <onItemClicked callback>
}
  • position: position of item in context menu
  • text: text of menu item
  • click: callback which will be called when user select your menu item

To add a separator use minus sign. Example: { text: "-", position: "top" }.

To remove an existing item from a menu use minus sign in front of the item text.
Example: { text: "-Objects Tree..." }

Example:

widget.onChartReady(function() {
    widget.onContextMenu(function(unixtime, price) {
        return [{
            position: "top",
            text: "First top menu item, time: " + unixtime + ", price: " + price,
            click: function() { alert("First clicked."); }
        },
        { text: "-", position: "top" },
        { text: "-Objects Tree..." },
        {
            position: "top",
            text: "Second top menu item 2",
            click: function() { alert("Second clicked."); }
        }, {
            position: "bottom",
            text: "Bottom menu item",
            click: function() { alert("Third clicked."); }
        }];
    });

createButton(options)

  1. options: object { align: "left" }
    1. align: "right" | "left". default: "left"

Creates a new DOM element in chart top toolbar and returns jQuery object for this button. You can use it to append custom controls right on the chart. Example:

widget.onChartReady(function() {
    widget.createButton()
        .attr('title', "My custom button tooltip")
        .on('click', function (e) { alert("My custom button pressed!"); })
        .append($('<span>My custom button caption</span>'));
});

Dialogs

Since 1.6 version

showNoticeDialog(params)

  1. params: object:
    1. title: text to be shown in the title
    2. body: text to be shown in the body
    3. callback: function to be called when ok button is pressed

This method shows a dialog with custom title and text and "OK" button.

showConfirmDialog(params)

  1. params: object:
    1. title: text to be shown in the title
    2. body: text to be shown in the body
    3. callback(result): function to be called when ok button is pressed. result is true if OK is pressed, otherwise it is false.

This method shows a dialog with custom title and text and "OK", "CANCEL" buttons.

showLoadChartDialog()

Displays Load chart dialog.

showSaveAsChartDialog()

Displays Save As... chart dialog.

Getters

symbolInterval(callback)

  1. callback: function(result)
    1. result: object {symbol, interval}

Since 1.4 the function returns the result immediately. Callback is kept for compatability.

Charting Library will call your callback with an object containing chart's symbol and interval.

mainSeriesPriceFormatter()

Returns object with method format that you can use to format prices. Introduced in 1.5.

getIntervals()

Returns an array of supported resolutions. Introduced in 1.7.

getStudiesList()

Returns an array of all studies ids. They can be used to create a study.

Customization

addCustomCSSFile(url)

  1. url should be absolute or relative path to 'static` folder

This method was introduced in version 1.3. Starting from 1.4 use custom_css_url instead.

applyOverrides(overrides)

Introduced in Charting Library 1.5

  1. overrides is an object. It is the same as overrides in Widget Constructor.

This method applies overrides to properties without reloading the chart.

applyStudiesOverrides(overrides)

Introduced in Charting Library 1.9

  1. overrides is an object. It is the same as studies_overrides in Widget Constructor.

This method applies studies overrides to indicators' style or inputs without reloading the chart.

:chart: Trading Terminal

The following methods are available in [[Trading Terminal]] only.

:chart: showSampleOrderDialog(order)

  1. order: object

Displays a sample order dialog. This dialog looks like Trading View Paper Trading one. Usually you don't need to use the sample dialog. This method is used in the trading sample.

:chart: watchList()

Introduced in Charting Library 1.9

Returns an object to manipulate the watchlist. The object has the following methods:

  1. getList() - allows you to get current list of the symbols.

  2. setList(symbols) - allows you to set a list of symbols into the watchlist. It will replace the whole list.

  3. onListChanged() - you can subscribe using [[Subscription]] object returned by this function to be notified when the watchlist is changed and unsubscribe from the event. If there is no WatchList null will be returned.

:chart: Multiple Charts Layout

:chart: chart(index)

  1. index: index of a chart starting from 0. index is 0 by default.

Returns a chart object that you can use to call [[Chart-Methods]]

:chart: activeChart()

Returns current active chart object that you can use to call [[Chart-Methods]]

:chart: chartsCount()

Returns amount of charts in the current layout

:chart: layout()

Returns current layout mode. Possible values: 4, 6, 8, s, 2h, 2-1, 2v, 3h, 3v, 3s.

:chart: setLayout(layout)

  1. layout: Possible values: 4, 6, 8, s, 2h, 2-1, 2v, 3h, 3v, 3s.

Changes current chart layout.

See Also

  • [[Chart-Methods]]
  • [[Customization Overview]]
  • [[Widget Constructor]]
  • [[Saving and Loading Charts|Saving-and-Loading-Charts]]
  • [[Overriding Studies' Defaults|Studies-Overrides]]
  • [[Overriding Chart's Defaults|Overrides]]
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,744评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,505评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,105评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,242评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,269评论 6 389
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,215评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,096评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,939评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,354评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,573评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,745评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,448评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,048评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,683评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,838评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,776评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,652评论 2 354

推荐阅读更多精彩内容