如何从canvas.js中的选项加载新图表?

时间:2019-10-01 06:40:04

标签: charts canvasjs

我想根据从下拉列表中选择的图表来加载图表。

假设单击饼图时,数据应显示在饼图中,而单击列时,数据应显示在柱状图中,依此类推。

我正在这样尝试:-

<script>
$(function()
{ 
       var barChart = new CanvasJS.Chart("barChartContainer", 
        {
            animationEnabled: true,
            theme: "light2",
            title:
            {
                text: "Gender wise Employees Length",
                fontSize: 20,
                fontFamily: "Trebuchet MS",
                fontWeight: "bold",
                margin: 10
            },
            axisY :{
                title: "Number of Employees"
            },
            data: [{        
                type: "funnel",  // column pie funnel line
                dataPoints: [                                           
                    { y: ${male}, label: "MALE" },                   
                    { y: ${female},  label: "FEMALE" }                  
                    ] 
                  }]
        });

       barChart.render(); 

       }
});
</script>
<div class="card shadow p-1 bg-white rounded">
    <div class="card-body">
        <div class="dropdown mr-20">
            <img src="${pageContext.request.contextPath}/resources/images/list.png" alt="edit" class="image dropdown-toggle" data-toggle="dropdown"/>
            <div class="dropdown-menu">
                <a class="dropdown-item" id="pie" href="#">Pie</a>
                <a class="dropdown-item" id="bar" href="#">Bar</a>
                <a class="dropdown-item" id="funnel" href="#">Funnel</a>
            </div>
        </div>
        <div id="barChartContainer" style="height: 240px; width: 100%;"></div>
    </div>
</div>
<script>
    $("#pie").click(function()
    {
         alert("Pie was clicked.");
    });
    $("#bar").click(function()
    {
         alert("Bar was clicked."); 
    });
    $("#funnel").click(function()
    {
         alert("Funnel was clicked.");
    });
</script>

1 个答案:

答案 0 :(得分:1)

在调用render之后,
我们可以按如下方式访问数据系列类型...

barChart.options.data[0].type = 'pie';

而不是拥有三个单独的点击事件,
将一键事件分配给下拉类。
然后使用单击的元素的ID设置图表类型...

$(".dropdown-item").click(function(e) {
  barChart.options.data[0].type = e.target.id;
  barChart.render();
});

请参阅以下工作片段...

$(function() {
  var barChart = new CanvasJS.Chart("barChartContainer", {
    animationEnabled: true,
    theme: "light2",
    title: {
      text: "Gender wise Employees Length",
      fontSize: 20,
      fontFamily: "Trebuchet MS",
      fontWeight: "bold",
      margin: 10
    },
    axisY: {
        title: "Number of Employees"
    },
    data: [{
      type: "funnel",
      dataPoints: [
        {y: 10, label: "MALE"},
        {y: 10, label: "FEMALE"}
      ]
    }]
  });

  barChart.render();

  $(".dropdown-item").click(function(e) {
    barChart.options.data[0].type = e.target.id;
    barChart.render();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div class="card shadow p-1 bg-white rounded">
  <div class="card-body">
    <div class="dropdown mr-20">
      <div class="dropdown-menu">
        <a class="dropdown-item" id="pie" href="#">Pie</a>
        <a class="dropdown-item" id="bar" href="#">Bar</a>
        <a class="dropdown-item" id="funnel" href="#">Funnel</a>
      </div>
    </div>
    <div id="barChartContainer" style="height: 240px; width: 100%;"></div>
  </div>
</div>