如何在ChartJS中使用溢出滚动修复图表图例宽度 - 高度

时间:2017-04-05 11:16:59

标签: javascript jquery css chart.js pie-chart

我想为我的项目创建饼图,所以我正在使用ChartJS,但是mapJS存在一些问题,如果我使用10到20个数据字段,时间图表看起来不错,但是当我我在一个图表中应用了超过50到60个数据字段,因此该图表看起来更小而不是图例,并且如果有时数据字段太多而不是图表隐藏,则只能看到图例,图表就消失了。

我想将高度或宽度设置为该图例,如果有更多数据,也应用于滚动条。

以下是供您参考的小提琴链接

https://jsfiddle.net/KDM1010/5um78rbk/

enter image description here

<canvas id="myChart" width="500" height="300"></canvas>


var ctx = document.getElementById("myChart").getContext('2d');
    var myChart = new Chart(ctx, {
      type: 'pie',
      data: {
        labels: ["lable_1", "lable_2", "lable_3", "lable_4", "lable_5", "lable_6", "lable_7", "lable_8", "lable_9", "lable_10", "lable_11", "lable_12", "lable_13", "lable_14", "lable_15", "lable_16", "lable_17", "lable_18", "lable_19", "lable_20", "lable_21", "lable_22", "lable_23", "lable_24", "lable_25", "lable_26", "lable_27", "lable_28", "lable_29", "lable_30", "lable_31", "lable_32", "lable_33", "lable_34", "lable_35", "lable_36", "lable_37", "lable_38", "lable_39", "lable_40", "lable_41", "lable_42", "lable_43", "lable_44", "lable_45", "lable_46", "lable_47", "lable_48", "lable_49", "lable_50"],
        datasets: [{
          backgroundColor: ["#2ecc71","#3498db","#95a5a6","#9b59b6","#f1c40f","#e74c3c","#34495e","#2ecc71","#3498db","#9a5a6","#9b59b6","#f1c40f","#e74c3c","#34495e","#2ecc71","#3498db","#95a5a6","#9b59b6","#f1c40","#e74c3c","#34495e","#2ecc71","#3498db","#95a5a6","#9b59b6","#f1c40f","#e74c3c","#34495e","#2ecc71","#3498db","#95a5a6","#9b59b6","#f1c40f","#e74c3c","#34495e","#2ecc71","#3498db","#955a6","#9b59b6","#f1c40f","#e74c3c","#34495e","#2ecc71","#3498db","#95a5a6","#9b59b6","#f1c40f,"#e74c3c","#34495e","#2ecc71","#3498db","#95a5a6","#9b59b6","#f1c40f","#e74c3c","#34495e"
          ],
          data: [74, 22, 77, 19, 72, 17, 55, 14, 64, 93, 99, 58, 6, 13, 31, 61, 19, 97, 25, 94, 50, 83, 3, 15, 54, 19, 60, 41, 37, 27, 51, 21, 36, 69, 80, 20, 64, 74, 79, 48, 66, 1, 94, 57, 5, 18, 83, 41, 64, 48]
        }]
      }
    });

3 个答案:

答案 0 :(得分:13)

你可以尝试一件事

尝试将图例分隔为不同的div。现在您可以完全控制div,您可以使用自定义位置和自己的css考虑以下代码:

 <div id="js-legend" class="chart-legend"></div>
 document.getElementById('js-legend').innerHTML = myChart.generateLegend();

供参考,请参阅此FIDDLE

对于处理图例交互式,您需要扩展回调函数并进行如下操作:

$("#js-legend > ul > li").on("click",function(e){
        var index = $(this).index();
        $(this).toggleClass("strike")
        var ci = e.view.myChart;
        var curr = ci.data.datasets[0]._meta[0].data[index];
        curr.hidden = !curr.hidden
        ci.update();
}) 

对于传奇,请参阅此FIDDLE

解释

我将图表绑定到窗口,以便事件视图可以获取它,稍后由索引操纵数据和更新的运行时使用几个css技巧来说明打击效果。

答案 1 :(得分:1)

我没有找到任何解决方案,但是如果要显示太多标签,您可以关闭图例(我在我的项目中这样做)-此处最多设置了15个标签:

options : {
        responsive: true,
        maintainAspectRatio: false,
        layout: {
          padding: {
             left     : 0,
             right    : 0,
             top      : 0,
             bottom   : 0
          }
        },
        animation: {
          duration : 5000
        },
        legend: {
          display: this.chartValues.length <= 15,
        }
}

您还可以腾出更大的空间来绘制图表。将此添加到您的scss文件:

.chartjs-render-monitor{
    height: 300px !important;
}

我希望它能帮助某人

答案 2 :(得分:0)

如其他答案所述,一个可能的解决方案是将图例外部化为纯 HTML div,您可以完全控制。

现在,在 ChartJS v3 中,12 函数已被删除。

不过,维护者发布了所谓的 htmlLegend 插件,该插件将图例外部化,这是链接:

HTML Legend (External DIV) - ChartJS Docs

该插件创建了一个交互式图例,可以单击其项目以显示/隐藏原始数据集。

注意:虽然有一个可滚动的图例可能会派上用场,但图例中有太多项目可能会使图表难以辨认,应该考虑以不同的方式表示数据,也许是通过拆分数据更多图表或介绍搜索功能