javascript - 如何通过按下按钮(chart.js)来制作新图表?

标签 javascript chart.js

我想知道是否可以通过按按钮从条形图转到饼图?

<script>        
var ctx = document.getElementById("myCanvas");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["2012", "2013", "2014", "2015", "2016", "2017"],
        datasets: [{
            label: 'Miljoner ton',
            data: [56.38, 59.3, 61.81, 58.83, 52.32, 66.86],
            backgroundColor: 'rgb(124,181,236)'
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }

            }]
        }
    }
});

function adddata() {

    myChart.data.datasets[0].data[7] = 14;
    myChart.data.labels[7] = "Newly added";

    myChart.update();

}
</script>

最佳答案

点击完美链接https://codepen.io/mateegojra/pen/GXVOap

var canvas = document.getElementById("barChart");
var ctx = canvas.getContext('2d');
// We are only changing the chart type, so let's make that a global variable along with the chart object:
var chartType = 'bar';
var myBarChart;

// Global Options:
Chart.defaults.global.defaultFontColor = 'grey';
Chart.defaults.global.defaultFontSize = 16;

var data = {
  labels: ["2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016"],
  datasets: [{
    label: "UFO Sightings",
    fill: true,
    lineTension: 0.1,
    backgroundColor: "rgba(0,255,0,0.4)",
    borderColor: "green", // The main line color
    borderCapStyle: 'square',
    pointBorderColor: "white",
    pointBackgroundColor: "green",
    pointBorderWidth: 1,
    pointHoverRadius: 8,
    pointHoverBackgroundColor: "yellow",
    pointHoverBorderColor: "green",
    pointHoverBorderWidth: 2,
    pointRadius: 4,
    pointHitRadius: 10,
    data: [10, 13, 17, 12, 30, 47, 60, 120, 230, 300, 310, 400],
    spanGaps: true,
  }]
};

// Notice the scaleLabel at the same level as Ticks
var options = {
  scales: {
    yAxes: [{
      ticks: {
        beginAtZero: true
      }
    }]
  },
  title: {
    fontSize: 18,
    display: true,
    text: 'I want to believe !',
    position: 'bottom'
  }
};


// We add an init function down here after the chart options are declared.

init();

function init() {
  // Chart declaration:
  myBarChart = new Chart(ctx, {
    type: chartType,
    data: data,
    options: options
  });
}

function toggleChart() {
  //destroy chart:
  myBarChart.destroy();
  //change chart type: 
  this.chartType = (this.chartType == 'bar') ? 'line' : 'bar';
  //restart chart:
  init();
}
body{
  background-color: black;
}
.as-console-wrapper{display: none !important }
#barChart{
  background-color:rgba(255,255,255,0.1);
  border-radius: 6px;
/*   Check out the fancy shadow  on this one */

}

.btn{
  color:black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script>
<!--
THe post that goes with this pen:
https://codepen.io/k3no/post/learning-by-example-getting-started-with-chart-js 
-->

<div class="container">
  <br />
  <div class="row">
    <div class="col-md-1"></div>
    <div class="col-md-10">
<!--       Chart.js Canvas Tag -->
      <canvas id="barChart"></canvas>
    </div>
    <div class="col-md-1"></div>    
  </div>
  <br />
  <div class="row">
    <div class="col-md-1"></div>    
    <div class="col-md-10"><button type="button" class="btn btn-success btn-md" onclick="toggleChart();">Toggle Chart </button></div>
    <div class="col-md-1"></div>
  </div>
</div>

关于javascript - 如何通过按下按钮(chart.js)来制作新图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52516568/

相关文章:

javascript - D3 Stacked Bar Chart 中引用 JS 数组

javascript - Angular 与 wavesurfer.js 在 IE10 中不起作用

chart.js - 通过PHP、MySQL通过ajax调用更新图表数据

javascript - 替换chartjs数据

javascript - x,y 数值的最后一个数据点(散点图)的 x 轴结束

javascript - 如何在 JavaScript mustache 模板上添加简单的 onclick?

javascript - JS错误: <parameter>.包含不是一个函数,不知道为什么

c# - 更新面板刷新后运行 Javascript

javascript - 如何向 chart.js 折线图添加点?

javascript - 如何创建 X 轴和 Y 轴都使用字符串的图表?