jQuery flot 饼图标签格式

Lea*_*ria 3 css jquery charts flot pie-chart

我正在尝试格式化我的查询浮图饼图标签和图例。

这是我迄今为止创建的内容:

在此处输入图片说明

这就是我正在尝试创建的(我使用 photoshop 完成的):

在此处输入图片说明

如您所见,我正在努力在饼图中包含百分比和值(看到百分比是粗体而值不是),并垂直居中对齐图例。

这是代码:

(function () {

    var data = [
        { label: "Sales & Marketing",  data: 9545, color: "#62A83B"},
        { label: "Research & Development",  data: 16410, color: "#2897CB"},
        { label: "General & Administration",  data: 4670, color: "#DEAB34"}
    ];

    $(document).ready(function () {
        $.plot($("#expenses-chart"), data, {
             series: {
                pie: {
                    show: true
                }
             },
             legend: {
                show: true,
                labelFormatter: function(label, series) {
                    var percent= Math.round(series.percent);
                    var number= series.data[0][1]; //kinda weird, but this is what it takes
                    return('&nbsp;<b>'+label+'</b>:&nbsp;'+ percent + '%');
                }
             }
        });
    });

})();
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢!

Mar*_*ark 5

您的第一个问题主要是标记和 CSS 的问题。我会将图例放在它自己的 div 和样式中以垂直对齐它。

<style>
  #wrapper {
    display: inline-flex;
  }
  #legend {
    margin: auto 5px;
  }
  #expenses-chart{
    float: left; 
    width: 300px; 
    height: 300px;
  }
  .pieLabel{
    color: #fff;
  }
</style>

<div id="wrapper">
  <div id="expenses-chart"></div>
  <div id="legend"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

对于饼图中的标签,您需要为标签指定一个自定义格式化程序:

$.plot($("#expenses-chart"), data, {
  series: {
    pie: {
      show: true,
      radius: 150,
      label: {
        show: true,
        radius: 0.5, // place in middle of pie slice
        formatter: function(label, series){
          var percent = Math.round(series.percent);
          var number = series.data[0][2]; // this is the y value of the first point
          return ('&nbsp;<b>' + percent + '%</b><br/>$' + number); // custom format
        }
      }
    }
  },
  legend: {
    show: true,
    container: $("#legend")
  }
}
Run Code Online (Sandbox Code Playgroud)

把它放在一起会产生(这里的例子):

在此处输入图片说明