Skip to content

JavaScript functions passed as string won't execute #48

@jdefrance-stessa

Description

@jdefrance-stessa

Hi Everyone!
In this very example, trying to format y axes as well as tooltip values with currency format.
Ultimately, I also want to use JavaScript to make some of the series clickable.. etc.

As per this gem documentation, this normally is possible:

You can put anything in the options hash that Chart.js recognises. To pass a JavaScript function as an option value, wrap it in quotation marks to make it a string.

In practice, I don't seem to be able to make it work.

<div> <%= bar_chart @chart_data, { responsive: true, legend: { position: "right", }, maintainAspectRatio: false, tooltips: { mode: 'label', intersect: true, callbacks: { title: "function(tooltipItem) { return moment(tooltipItem[0].xLabel).format('ll'); }", label: "function(tooltipItem, data) { return data.datasets[tooltipItem.datasetIndex].label + ': ' + tooltipItem.yLabel; }" } }, scales: { xAxes: [{ stacked: true, }], yAxes: [{ stacked: false, userCallback: "function(value, index, values) { value = value.toString(); value = value.split(/(?=(?:...)*$)/); value = value.join(','); return '$' + value; }" }] }} %> </div>

Is this a know bug/issue?
Should I be doing this differently?

Alternatively, is there any way to access the ctx and chart variables from the generated script, so I can eventually attach these callbacks in raw JS, in the case this gem doesn't allow it?

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions