ChartJS根据下拉选择更改图表

时间:2015-10-21 13:07:52

标签: javascript jquery chart.js

每次用户选择ChartJS中的新图表类型时,我都会尝试确定重新加载画布的正确方法,现在它不会更改图表类型:

chartJs.js

 var ctx = document.getElementById("myChart").getContext("2d");
    ctx.canvas.width = 600;
    ctx.canvas.height = 200;
    var determineChart = document.getElementById("chartType");
    if( determineChart = "line") {
        var myChart = new Chart(ctx).Line(data, options);
        myChart.destroy();
    }
    else if (determineChart = "column"){
        var myChart = new Chart(ctx).Bar(data, options);

    }

index.html

<select id="chartType">
    <option value="line">Line</option>
    <option value="column">Column</option>
    <option value="area">Area</option>
    <option value="areaspline">Area Spline</option>
    <option value="pie">Pie(Test)</option>
</select>

2 个答案:

答案 0 :(得分:3)

一个问题是在条件检查中使用赋值运算符(=)而不是逻辑运算符(=====)。另外,我不会将if / else逻辑与可用的图表类型结合起来。

var ctx = document.getElementById("myChart").getContext("2d");
 ctx.canvas.width = 600;
 ctx.canvas.height = 200;

 var dataMap = {
     'line': {
         method: 'Line',
         data: {
             labels: ["January", "February", "March", "April", "May", "June", "July"],
             datasets: [{
                 label: "My First dataset",
                 fillColor: "rgba(220,220,220,0.5)",
                 strokeColor: "rgba(220,220,220,0.8)",
                 highlightFill: "rgba(220,220,220,0.75)",
                 highlightStroke: "rgba(220,220,220,1)",
                 data: [65, 59, 80, 81, 56, 55, 40]
             }, {
                 label: "My Second dataset",
                 fillColor: "rgba(151,187,205,0.5)",
                 strokeColor: "rgba(151,187,205,0.8)",
                 highlightFill: "rgba(151,187,205,0.75)",
                 highlightStroke: "rgba(151,187,205,1)",
                 data: [28, 48, 40, 19, 86, 27, 90]
             }],
         }
     },
     'pie': {
         method: 'Pie',
         data: [{
             value: 300,
             color: "#F7464A",
             highlight: "#FF5A5E",
             label: "Red"
         }, {
             value: 50,
             color: "#46BFBD",
             highlight: "#5AD3D1",
             label: "Green"
         }, {
             value: 100,
             color: "#FDB45C",
             highlight: "#FFC870",
             label: "Yellow"
         }]
     }
 };

var currentChart;

 function updateChart() {
     if(currentChart){currentChart.destroy();}
   
     var determineChart = $("#chartType").val();

     var params = dataMap[determineChart]
     currentChart = new Chart(ctx)[params.method](params.data, {});
 }

 $('#chartType').on('change', updateChart)
 updateChart();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.js'></script>
<canvas id="myChart"></canvas>
<select id="chartType">
    <option value="line">Line</option>
    <option value="pie">Pie</option>
</select>

答案 1 :(得分:0)

据我所知,我们无法直接在Charts.js中更新图表类型,那么我们必须销毁原始图表并重绘该图表

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<div class="container-div">
    <div class="controls-div">
        <select name="chartType" id="chartType" onchange="updateChartType()">
            <option value="line">Line</option>
            <option value="bar">Bar</option>
            <option value="doughnut">Doughnut</option>
            <option value="radar">Radar</option>
            <option value="polarArea">Polar Area</option>
        </select>
    </div>
    <div class="chart-container" style="position: relative; width:85vw">
        <canvas id="my_Chart"></canvas>
    </div>
</div>
$('.cube-1').click(function name(e) {

    if(e.target.className == "cube-1")
    {
      $('.cube-1').append('<div class="cube-2"><div class="click-me">click me to 
      hide this cube</div></div>');
    }

});

 $(document).on('click','.cube-2',function name(e) 
 {
      $(this).remove();
      // how to add event click on "click-me" to hide "cube-2"??
 })