Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom tooltips #12

Open
mgajowiak opened this issue Dec 20, 2017 · 3 comments
Open

Custom tooltips #12

mgajowiak opened this issue Dec 20, 2017 · 3 comments

Comments

@mgajowiak
Copy link

Following the chart.js documentation to customize tooltips I could use options: tooltip: callbacks, but this option has been commented in code. How can I customise tooltips content?

@jamieoneill
Copy link

jamieoneill commented Feb 27, 2018

I've found this, it needs to be written in JavaScript as the custom option not the callback option.

Doc for custom here http://www.chartjs.org/docs/latest/configuration/tooltip.html#external-custom-tooltips. I'll just show how to get started.

Create a new JavaScriptFunction object.
Below code is bare minimum to just change the the title of the tooltip to "Test".

JavaScriptFunction function = new JavaScriptFunction("function(tooltipModel)" 
                                        + "{ var tooltipEl = document.getElementById('chartjs-tooltip');"
					+ ""
					+ "if (!tooltipEl) { "
					+ "tooltipEl = document.createElement('div'); "
					+ "tooltipEl.id = 'chartjs-tooltip'; "
					+ "tooltipEl.innerHTML = \"<table></table>\" "
					+ "document.body.appendChild(tooltipEl); }"
					+ ""
					+ ""
					+ "function getBody(bodyItem) {return bodyItem.lines; }"
					+ ""
					+ "var titleLines = tooltipModel.title || [];"
					+ "var bodyLines = tooltipModel.body.map(getBody);"
					+ ""
					+ "titleLines.forEach(function(title) {"
					+ "innerHtml += '<tr><th>test</th></tr>';"
					+ "});"
					+ ""
					+ ""
					+ "}");

Add the JavaScriptFunction to the options and continue to create chart as normal.

LineOptions options = new LineOptions()
                    .setTitle(new Title().setText("ToolTip Testing").setDisplay(true))
		    .setTooltips(new Tooltips().setCustom(function));

When rendered should look like this:

  "options" : {
    "title" : {
      "display" : true,
      "text" : "ToolTip Testing"
    },
    "tooltips" : {
      "custom" : function(tooltipModel){ var tooltipEl = document.getElementById('chartjs-tooltip');if (!tooltipEl) { tooltipEl = document.createElement('div'); tooltipEl.id = 'chartjs-tooltip'; tooltipEl.innerHTML = "<table></table>" document.body.appendChild(tooltipEl); }function getBody(bodyItem) {return bodyItem.lines; }var titleLines = tooltipModel.title || [];var bodyLines = tooltipModel.body.map(getBody);titleLines.forEach(function(title) {innerHtml += '<tr><th>test</th></tr>';});}
    }
  }

Reference the documentation for all options.
It will still have to be done in a string containing Javascript, someone correct me if i am wrong but i don't see a way of doing it all in Java.

*note: can only be done with one chart on screen at a time due to the getElementById call.

@jochec
Copy link

jochec commented Nov 1, 2018

I have implemented this functionality in Tooltip callbacks + update fill #22 pull request

@mdewilde
Copy link
Owner

mdewilde commented Nov 2, 2018

@jochec changes are now available in 2.5.0 on Maven Central.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants