score:3
You could directly use the graph
object without assigning it to another variable ( considering graph
is a global variable ) to add any sort of data from another function.
Here is a quick example of that ...
var ctx = $('#graph');
var graph = new Chart(ctx, {
type: 'line',
data: {
labels: ["January", "February", "March", "April", "May"],
datasets: [{
label: "Sales",
data: [1, 2, 3, 4, 5],
fill: false,
borderColor: '#07C',
}]
}
});
function addData() {
graph.data.labels.push('June')
graph.data.datasets[0].data.push(6);
graph.update();
}
<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.5.0/Chart.min.js"></script>
<button onclick="addData()">Add Data</button>
<canvas id="graph"></canvas>
in case: the graph
variable is inside a function then, you'd have to make the variable global to use it from another function
score:2
//get object
const chartJS: Chart = $("#myChart").data('chart');
//change something
chartJS.config.options.scales.yAxes[0].ticks.min = minVal <= 0 ? minVal : minVal -
//update
chartJS.update();
score:7
You can just do this:
var graph = Chart.getChart('graph')
Works in v3 - not sure when it was introduced.
score:9
Perhaps:
var ctx = $('#graph');
var graph = new Chart(ctx, {
type: 'line',
data: someData,
});
ctx.data('graph', graph);
and later:
var graph = $('#graph').data('graph');
graph.data.labels.push(1)
graph.data.datasets[0].data.push(10);
graph.update();
// //
Source: stackoverflow.com
Related Query
- Get Chart js object from selector
- How to get ChartJs object from dynamically created chart
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
- Get Dataset Values from Chart JS onHover in Version 3
- How to get Data from API to display chart using chartjs in Vuejs
- Chart JS can't access data from array within an object
- Chart.js: get chart data from canvas
- How to split data from api to get a chart
- How to get line x value from Label chart js
- get JSON data from function to give value to Chart JS
- angular 4 line chart data is not showing data from http get
- Get value of min/lowest label from y-axis chart
- How to get certain text from json object value
- How to pass a chart.js chart data object in json from a controller in asp.net mvc
- getting additional value fields from data source for dx.chartjs doughnut chart
- How do you get the width of a datalabel from the Chartjs plugin after the chart animates?
- How to parse a Chart using data from a nested object in ChartJS
- Chartjs bar chart trying to get labels from datasets
- ChartJS: Get value from another another Chart
- How to get a value from function javascript and print it to the chart
- Angularjs line chart get data from sql
- How to clear a chart from a canvas so that hover events cannot be triggered?
- Chartjs random colors for each part of pie chart with data dynamically from database
- How to prevent first/last bars from being cut off in a chart with time scale
- Chart.js Bar Chart - how to chart bars from 0
- Chart from chart.js to pdf
- ChartJs how to get from mulitiple dateset which dataset bar is clicked
- How to add datas to chart js from javascript array itself?
- Adding Chart.js line chart to Jinja2/Flask html page from JS file
- get yLabel value onclick chart js
More Query from same tag
- Socket.io is not sending data to frontend as intended
- mongoose.Schema for IoT Data Array and fetch the data to chart.js
- Resize the width and height of the ng2-charts
- Show wrong information in chart js 2
- Background colour of line charts in chart.js
- Update charts in chartjs and angular
- Angular 2 ng2-charts donut change segment color
- Zoom Function For Chart.js
- Chart.js and EJS: Display String, Not Calculation Result
- ChartJS large data range
- How to have custom colors in ng2-charts and chart.js according to data?
- How to reduce number of multiple gridlines in stack bar graph chart.js
- Chartjs: Custom text on y axis at certain level
- How to send data to chart js using angular
- Chartjs bar min, max and avarage value
- How to always show line chart tooltip in ionic-angular.?
- ChartJS radar scale points
- How to dynamically set ChartJs line chart width based on dataset size?
- Chartjs: Is it possible to add phases in line chart?
- How to show only the data points that have a change in Chartjs?
- Skip dates with no values in chart js
- How to put 2 charts next to each other
- How do I incorporate Luxon timezone options with chart.js?
- Chart.js axes label font size
- Add padding between two y-axis scales using chart.js
- Chart JS Not Displaying Chart
- How to add a toggle inside a title of the barchart js
- Can't change the default color and font size of labels in react-chartjs-2
- How to reduce spaces between grids for graph in chart.js
- How do I access Chart.js default settings? (& related -- how do I turn the tool tips on?)