Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Doughnut Chart.js中添加图例_Javascript_Jquery_Jscharts - Fatal编程技术网

Javascript 在Doughnut Chart.js中添加图例

Javascript 在Doughnut Chart.js中添加图例,javascript,jquery,jscharts,Javascript,Jquery,Jscharts,我使用ChartJS库添加了甜甜圈字符,并使用ajax调用填充数据。 Doughtnut渲染正确,但没有图例。根据文档,如果出现标签,数据将显示在图例和工具提示中,但我只有工具提示。 这是我的js代码 var pieChartCanvas = $('#pieChartPaid').get(0).getContext("2d"); var pieChart = new Chart(pieChartCanvas);

我使用ChartJS库添加了甜甜圈字符,并使用ajax调用填充数据。 Doughtnut渲染正确,但没有图例。根据文档,如果出现标签,数据将显示在图例和工具提示中,但我只有工具提示。 这是我的js代码

var pieChartCanvas = $('#pieChartPaid').get(0).getContext("2d");
                        var pieChart = new Chart(pieChartCanvas);
                        var PieData;

                        $.ajax({
                            type: "GET",
                            cache: false,
                            contentType: "application/json; charset=utf-8",
                            url: '@Url.Action("GetPaidInvoice", "Home")',
                            dataType: "json",
                            success: function (data) {
                                PieData = data;
                                var pieOptions = {
                                    //Boolean - Whether we should show a stroke on each segment
                                    segmentShowStroke: true,
                                    //String - The colour of each segment stroke
                                    segmentStrokeColor: "#fff",
                                    //Number - The width of each segment stroke
                                    segmentStrokeWidth: 2,
                                    //Number - The percentage of the chart that we cut out of the middle
                                    percentageInnerCutout: 50, // This is 0 for Pie charts
                                    //Number - Amount of animation steps
                                    animationSteps: 100,
                                    //String - Animation easing effect
                                    animationEasing: "easeOutBounce",
                                    //Boolean - Whether we animate the rotation of the Doughnut
                                    animateRotate: true,
                                    //Boolean - Whether we animate scaling the Doughnut from the centre
                                    animateScale: false,
                                    //Boolean - whether to make the chart responsive to window resizing
                                    responsive: true,
                                    // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
                                    maintainAspectRatio: true,
                                    //String - A legend template
                                    legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
                                };                            
                                //Create pie or douhnut chart
                                pieChart.Doughnut(PieData, pieOptions);

标签仅在工具提示上可见,但不会生成图例。

尝试在您的选项中添加“图例”:{“显示”:true,“位置”:“底部”}。否,它不起作用。我假设legendTemplate有问题,但我无法解决。如果您使用的是ChartJS v2,legendTemplate似乎已被删除。您现在必须使用legendCallback,如下所述:好的,谢谢,但此回调是pieOptions或pieChart对象的一部分?根据文档,它是pieOptions的一部分。请看这里:
    data=[
 {value:300,
color: "#00a65a",
highlight:"#00a65a",
label:"On time"
},
{value:200,
color:"#f39c12" ,
highlight:"#f39c12"
label:"Overdue"
},
...
]