带有开关/单选按钮的Chart.js条形图,用于对数据进行分组/条形图故障排除

时间:2018-08-11 00:06:28

标签: javascript charts chart.js bar-chart

我的主要目标是创建一个开关/单选按钮以对数据进行分组(而不是在顶部放置过滤器)。请看截图以供参考:

enter image description here

enter image description here

是否可以对数据进行分组并将过滤器按钮变成开关?

如果不可能的话,我的计划B是一次显示所有数据,并将过滤器放在顶部。 这是代码和jsfiddle链接:

<!-- Charts library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>

<div class="chart-wrapper">
  <canvas id="myChart" width="400" height="200"></canvas>
  <script>
    var ctx = document.getElementById("myChart").getContext('2d');
    var myChart = new Chart(ctx, {
        type: 'bar',
      data: {
        labels: ["option 1", "option 2"],

            datasets: [
        {
        label: 'yes',
        data: [714,233],
        backgroundColor: '#004831',
        },
        {
        label: 'no',
        data: [800, 451],
        backgroundColor: '#76B900'
        }
            ]},

        options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero:true
            }
            }]
        }
        }
        });
    </script>
</div>

https://jsfiddle.net/fariadesign/kL810rnq/9/

感谢您的帮助。

谢谢!

1 个答案:

答案 0 :(得分:3)

它像这样工作...

var chart = null;

var chartData = {
  labels: ['Question 1', 'Question 2'],
  datasets: [
    {label: 'yes', backgroundColor: '#004831', data: [714, 233]},
    {label: 'no', backgroundColor: '#76B900', data: [800, 451]}
  ]
};

var chartOptions = {
  tooltips: {mode: 'index', intersect: true},
  legend: {display: false},
  responsive: true,
  scales: {
    xAxes: [{
      ticks: {beginAtZero: true},
      stacked: true
    }],
    yAxes: [{
      ticks: {beginAtZero: true},
      stacked: false
    }]
  }
};

$(function() {

  chart = new Chart($('#chart'), {
    type: 'bar',
    data: chartData,
    options: chartOptions
  });
  
  $("input#toggle[type='checkbox']").click(function (event) {
    var isStacked = false;
    if ($(this).is(':checked')) {
      isStacked = true;
    } else {
      isStacked = false;
    }
    
    chart.options.scales = {
      xAxes: [{ stacked: isStacked }],
      yAxes: [{ stacked: isStacked }]
    };
    chart.update();
  });
  
});
canvas {
   -moz-user-select: none;
   -webkit-user-select: none;
   -ms-user-select: none;
}
.toggle {
    position: relative;
    margin: 20px auto;
    width: 55px;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select: none;
}
.toggle-checkbox {
    display: none;
}
.toggle-label {
    display: block; overflow: hidden; cursor: pointer;
    height: 20px; padding: 0; line-height: 20px;
    border: 0px solid #FFFFFF; border-radius: 30px;
    background-color: #9E9E9E;
}
.toggle-label:before {
    content: "";
    display: block; width: 30px; margin: -5px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 31px;
    border-radius: 30px;
    box-shadow: 0 6px 12px 0px #757575;
}
.toggle-checkbox:checked + .onoffswitch-label {
    background-color: #42A5F5;
}
.toggle-checkbox:checked + .toggle-label, .toggle-checkbox:checked + .toggle-label:before {
   border-color: #42A5F5;
}
.toggle-checkbox:checked + .toggle-label .toggle-inner {
    margin-left: 0;
}
.toggle-checkbox:checked + .toggle-label:before {
    right: 0px; 
    background-color: #2196F3; 
    box-shadow: 3px 6px 18px 0px rgba(0, 0, 0, 0.2);
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<div class="toggle">
    <input id="toggle" type="checkbox" class="toggle-checkbox"/>
    <label for="toggle" class="toggle-label">&nbsp;</label>
</div>
<canvas id="chart" width="100%" height="100%"></canvas>

这是docs