score:2
Accepted answer
It's way easier to just destroy and render a new chart. The chart.update
method works better to manage small pieces of data, like adding or removing a few points.
You just need to call chart.destroy
and run your chart creation function again.
$(function(){
var chart;
var ctx = document.getElementById("searchChart").getContext("2d");
var initPeriod = $('#search_period').val();
var jsonURL = $('#search_period').data('src');
var jsonData;
var initChart = function (data) {
chart = new Chart(ctx, {
type: 'line',
data: data,
options: {
responsive: true,
legend: {
display: false
},
elements:{
point:{
radius:0
}
},
scales:{
xAxes:[
{
gridLines:{
display:false
}
}
],
yAxes:[
{
ticks: {
beginAtZero: true
}
}
]
}
}
});
};
// fetch data
$.getJSON( jsonURL, function( results ) {
// copy json object
jsonData = results;
// init chart
initChart(results[initPeriod])
});
// on new period select
$('#search_period').change(function(){
// get new period
var activePeriod = $(this).val();
// render chart again
initChart(jsonData[activePeriod]);
});
});
.panel-searches__graph{
margin:20px 0 0 0;
}
<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/2.7.2/Chart.bundle.min.js"></script>
<select name="search_period" id="search_period" data-src="https://api.myjson.com/bins/1d6l2o">
<option value="day">Day</option>
<option value="week">Week</option>
<option value="month" selected>Month</option>
<option value="year">Year</option>
</select>
<div class="panel-searches__graph">
<canvas id="searchChart" width="500" height="150"></canvas>
</div>
Source: stackoverflow.com
Related Query
- Chart.js load new data from saved JSON object
- Load data from a JSON object into an array
- How to pass a chart.js chart data object in json from a controller in asp.net mvc
- Chart not displaying from JSON data
- Angular / ng2-charts: Fetching json data in chart object showing: Cannot read property 'length' of undefined
- Charts.js: Load new data set to an existing chart
- Populating Chart.Js line graph with multiple lines using data from a JSON Object
- Chart JS can't access data from array within an object
- get JSON data from function to give value to Chart JS
- Chart.js - Multiple JSON data object array [{x: Date, y: count}....] representing each dataset of the chart
- Load data from Google Analytics in a Chart.js chart
- How to fetch data from Json file on charts(bar-chart/pie chart etc) in angular?
- How to load new chart (chartsjs) from api call with jquery?
- Data not displaying in Chart JS from PHP JSON
- getting additional value fields from data source for dx.chartjs doughnut chart
- How to parse a Chart using data from a nested object in ChartJS
- how to add multiple data in chart js dynamically from JSON
- Not being able to display value from json data into chart js
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- How to load Json data onto a Charts.js chart
- chart.js load totally new data
- Chartjs random colors for each part of pie chart with data dynamically from database
- Get Chart js object from selector
- React ChartJS prevent new data from being added into state after it's redrawn?
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- Parsing JSON data into Chart.js bar chart
- Multiple lines / data series from JSON file with Chart.js
- Using data from API with Chart JS
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
More Query from same tag
- angular-chart.js doughnut chart : How do I change width of the arc of a doughnut?
- How to vary the thickness of doughnut chart, using ChartJs.?
- How do I use Chart.js and Bootstrap at same time? (various jQuery versions)
- Alternating bars in bar chart in chart.js are not labelled
- ChartJS horizontal bar with numbers on both scales
- Why does nuxt give me this error with vue-chartjs?
- chart.js Failed to create chart: can't acquire context from the given item
- How to shade between 2 lines on a line graph using chart.js version2
- Editing Chart.js legend template
- How to make a chart with a string values instead of data?
- EJS Tags with ChartJS?
- increasing php variable in javascript loop
- How to remove Grid lines except for zero line and border Chartjs?
- Add Commas to ChartJS Data Points
- Cannot change the color of column chart according to its value
- How to hide more than one legend on Chartjs?
- Dynamically set chart type with Angular Chart?
- How to draw multiple color bar in a bar chart using chart.js
- How to display pie slice data and tooltip together using chart.js
- I have a setInterval function that graphs.
- Convert FirestoreCollection into an array?
- chart.js increase value on click
- Chart.Js : How to change the color of a point, based on a third data
- generating a Chart.js chart with python data
- Chart.js time scale graph - xAxis labelling
- react-chartjs-2 how to set multiple background levels within a line chart
- Php array and chartjs
- Why chart renders after second click - react-chartjs-2
- Chart js plugin annotation not working in vue
- chartjs mixed type display want to set display none for some bar