1

I'm creating some pie charts with Plotly.js but I can't figure out how to set the value format. If my number is 19.231, I get 19.2. I want 19.2310, with 4 digit precision. My data and layout follow here:

var data = [{
    values: values,
    labels: labels,
    type: 'pie',
    marker: {
        colors: colors
    },
    textfont: {
        family: 'Helvetica, sans-serif',
        size: 48,
        color: '#000'
    }
}];
var layout = {
    height: 1350,
    width: 1500,
    title: title,
    titlefont: {
        family: 'Helvetica, sans-serif',
        size: 58,
        color: '#000'
    },
    legend: {
        x: 1.1,
        y: 0.5,
        size: 40,
        font: {
            family: 'Helvetica, sans-serif',
            size: 48,
            color: '#000'
        }
    }
};
3
  • Can you add some of your values to have a minimal, verifiable example? Commented Apr 20, 2017 at 13:14
  • They are generated on the flow depending on the user input. Values are floats, plotly automatically round them to one digit after the comma. Commented Apr 20, 2017 at 13:17
  • 1
    if you try var values = [1, 2, 4.6]; you get 13.2%, 26.3% and 60.5% instead of 13.1579%, 26.3158% and 60.5263% Commented Apr 20, 2017 at 13:27

2 Answers 2

2

As far as I know you cannot do that directly in Plotly but you could add text values with your desired precision and set textinfo to text.

var values = [Math.random(), Math.random(), Math.random()];
var sum = values.reduce(function(pv, cv) { return pv + cv; }, 0);
var digits = 4;
var rounded_values = [];

for (var i = 0; i < values.length; i += 1) {
	rounded_values.push(Math.round(values[i]/sum * 100 * 10**digits) / 10**digits + '%');
}

var myPlot = document.getElementById('myPlot');
var data = [{
    values: values,
    type: 'pie',
    text: rounded_values,
	textinfo: 'text'
}];
Plotly.plot(myPlot, data);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myPlot"></div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, It's exactly what I was watching for.
2

I saw the document find some rule

about Advanced Hovertemplate it said

      data: [
        {
          type: "scatter",
          mode: "markers",
          x: x,
          y: y,
          text: t,
          marker: { size: s, sizeref: 4000, sizemode: "area" },
          transforms: [{ type: "groupby", groups: c }],
          hovertemplate:
            "<b>%{text}</b><br><br>" +
            "%{yaxis.title.text}: %{y:$,.0f}<br>" +
            "%{xaxis.title.text}: %{x:.0%}<br>" +
            "Number Employed: %{marker.size:,}" +
            "<extra></extra>"
        }
      ],

you can change here %{y:$,.0f} 0f to 4f . like this

hovertemplate: '%{y:$,.4f}',

Polt.ly JS document

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.