如何禁用chart.js中的内置图例

时间:2016-05-06 12:22:23

标签: javascript jquery charts

我想禁用图表中的内置图例(彩色矩形)。广告同时我想保留我创建的自定义图例:document.getElementById('legendq3')。innerHTML = myDoughnutChart.generateLegend();

 <canvas id="myChart"></canvas>
                        <div id="legendq3"></div>
                        <script> 
                            var ctx = document.getElementById("myChart");

                            var data = {
                                labels: [
                                    "Red",
                                    "Green",
                                    "Yellow"
                                ],
                                datasets: [
                                    {
                                        data: [300, 50, 100],
                                        backgroundColor: [
                                            "#FF6384",
                                            "#36A2EB",
                                            "#FFCE56"
                                        ],
                                        hoverBackgroundColor: [
                                            "#FF6384",
                                            "#36A2EB",
                                            "#FFCE56"
                                        ]
                                    }]
                            };

                            var options = {
                                 legendTemplate :'<ul>'
                                                +'<% for (var i=0; i<datasets.length; i++) { %>'
                                                +'</li>'
                                                +'<span style=\"background-color:<%=datasets[i].lineColor%>\"></span>'
                                                +'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
                                                +'</li>'
                                                +'</ul>'

                            }

                            var myDoughnutChart = new Chart(ctx, {
                                type: 'doughnut',
                                data: data,
                                options: options
                            });

                            //myDoughnutChart.defaults.global.legends.display = false;

                            document.getElementById('legendq3').innerHTML = myDoughnutChart.generateLegend();
                        </script>

1 个答案:

答案 0 :(得分:2)

我发现只有这个解决方案。将其添加到选项

legend: {
    display: false,
}

以下是http://jsfiddle.net/qy1hr7mx/

的示例