如何在jqPlot图表的区域上显示数值而非百分比

7

I have javascript code as:

var plot1 = jQuery.jqplot ('chartdiv', [data], 
{ 
  seriesDefaults: {
        // Make this a pie chart.
        renderer: jQuery.jqplot.PieRenderer, 
        rendererOptions: {
          // Put data labels on the pie slices.
          // By default, labels show the percentage of the slice.
          showDataLabels: true,
          dataLabels: 'value'
        }
  }, 
  legend: { show:true, location:'e'}
});


var handler = function(ev, gridpos, datapos, neighbor, plot) { 
    if (neighbor) { 
        alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]); 
    }
}; 

$.jqplot.eventListenerHooks.push(['jqplotClick', handler]); 

现在通过使用dataLabels: 'value',我能够显示值,但是显示的值是51而不是50.667。值被四舍五入了。但我需要显示准确的值。如何做到这一点?

我的第二个问题是,当我将鼠标指针悬停在图表的任何区域上时,我想显示一些内容。这可以使用jqplotDataMouseOver完成,但如何使用它?

1个回答

11

您的第一个问题可以通过使用 dataLabelFormatString 属性来解决:

seriesDefaults: {
        // Make this a pie chart.
        renderer: jQuery.jqplot.PieRenderer, 
        rendererOptions: {
          // Put data labels on the pie slices.
          // By default, labels show the percentage of the slice.
          showDataLabels: true,
          dataLabels: 'value',
          dataLabelFormatString:'%.4f'
        }
  }, 

'%.4f'会显示小数点后四位。

你的第二个问题比较困难。显然,使用jqplot时柱状图上的事件不是很完善。但是该链接中的最后一个建议对我有用:

$('#chartdiv').bind('jqplotDataMouseOver', function (ev, seriesIndex, pointIndex, data) { alert('series: '+seriesIndex+', point: '+pointIndex+', data: '+data)}); 

这是一个jsfiddle,记得缓存JS文件,因为jqplot不允许热链接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接