8👍
One issue is the use of the assignment operator (=
) instead of a logical operator (==
or ===
) in your conditional checks. Additionally, I wouldn’t couple the if/else logic with the available chart types.
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>
- [Chartjs]-How to vary the thickness of doughnut chart, using ChartJs.?
- [Chartjs]-Chart.js is not rendered until zoom in in angular 8
1👍
as i know we can’t update chart type directly in Charts.js then we have to destroy original chart and redraw the chart
// Define data set for the chart
let data = [18, 10,7, 8, 18, 20, 45, 8, 0, 67];
myData = {
labels: ["January", "February", "March", "April", "May", "June", "July","September","October","November"],
datasets: [{
label: "Data, baby!",
fill: false,
backgroundColor: [
"#aec7e8", "#ffbb78", "#d62728 ", "#98df8a ", "#1f77b4", "#ff7f0e ", "#2ca02c ",
"#FF6380","#FF0000","#40E0D0","#FA8072","#339905","#98FB98","#FF8C00","#00FA9A",
"#DC143C",
"#FF1493",
"#FF69B4",
"#F08080",
"#FFA500",
"#FF4500",
],
borderColor: 'rgb(190, 99, 255)',
data: data,
}]
};
// Default chart defined with type: 'line'
Chart.defaults.global.defaultFontFamily = "monospace";
var ctx = document.getElementById('my_Chart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: myData
});
// Function runs on chart type select update
function updateChartType() {
// here we destroy/delete the old or previous chart and redraw it again
myChart.destroy();
myChart = new Chart(ctx, {
type: document.getElementById("chartType").value,
data: myData,
});
};
<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>
Source:stackexchange.com