score:0
When are you calling the buildStockChart()
method?
You should call it right after the for loop into the callback you pass to the subscribe method, since that's the moment when this.stockOpen
is populated (before that moment it will be empty as you are seeing in the console).
score:0
As @Joseph Agbing, I was unable to get it work with angular 7. I'm now using chart.js
only
npm install chart.js --save
with into my someChart.component.html
<div style="display: block"><!--Mandatory div including chart-->
<canvas id="canvas">{{chart}}</canvas>
</div>
into my someChart.component.ts
called from my httpClient.post(...).subscribe(lData => (setChartDataFromEntities(lDataProcessed), ...)
import { Chart } from 'chart.js';
export class someClass {
/**
*
* @param aDate
* @param aChargeUnitArray
*/
setChartDataFromEntities( aDate: Date, aChargeUnitArray: ChargeUnit[] ){
console.debug('setChartDataFromEntities->', aChargeUnitArray)
let lChartDataArray = []
let lChartDataLineDataArray: Array<Number> = []
let lChartLabelsArray: string[] = []
let l_s: string
aChargeUnitArray.forEach(element => {
lChartDataLineDataArray.push(element.charge)
lChartLabelsArray.push(MiscHelper.dateTimeHMSForChart(element.timestamp))
});
lChartDataArray.push(
{
data: lChartDataLineDataArray,
label: MiscHelper.dateForGui(aDate),
}
)
this.chart = new Chart('canvas', {
type: 'line',
data: {
labels: lChartLabelsArray,
datasets: lChartDataArray
},
options: {
legend: {
display: false
},
scales: {
xAxes: [{
display: true
}],
yAxes: [{
display: true
}],
}
}
});
this.statusMessage = 'Chart loaded'
}
hope it helps somebody more than the day I wasted trying to get it work...
score:3
i also have same problem with chart JS on angular so i force to use another chart. im now using angular 2 chart js.
i think the problem here is the delay of data fetch by API, the CHART component is already render on html view but the data is still not fetch by the API service.
try to add this code on your code block. This will handle the data if API service data is available. ()=>{this.buildStockChart();}
this.stocksService.getStockData()
.subscribe(
(response) => {
for(var i = 0; i < response.length; i++) {
this.stockOpen.push(response[i]['open']);
}
console.log('after loop: ', this.stockOpen);
},
()=>{
this.buildStockChart();
}
);
console.log('real: ', this.stockOpen);
console.log('test: ', this.testData);
}
This chart is easy to manage for dynamic instances.
Hope this chart will work on you.
Source: stackoverflow.com
Related Query
- Using data from API with Chart JS
- Initialize a Chart.js chart with data from an api
- How to get Data from API to display chart using chartjs in Vuejs
- The chart doesn't display the data from my call to the API with Axios
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- How to fill my chart (chart.js) with my own data from API
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to create chartjs chart with data from database C#
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
- How to make aspecific API call from within a Line Chart class in ReactJS using react-chartjs-2?
- Using chart.js with laravel passing data from controller to view
- How to display chart using Chartjs with JSON data in Laravel
- Populating Chart.Js line graph with multiple lines using data from a JSON Object
- How to create a chart with chartjs.org with data from an array?
- Line Chart using Chart js with time data
- Real-time line chart with ChartJS using Ajax data
- Visualization of charts using real time data from MSSQL with node.js webserver
- How to split data from api to get a chart
- Send data from php to chart with ajax
- Chart.js not rendering chart with data from mySQL database
- Using number/text input field to set the data values in ChartJs stops the chart from being displayed
- Displaying Chart with data retrieved from Entity Framework in Controller
- How to load new chart (chartsjs) from api call with jquery?
- Why is the data from my API not displaying on my chart.js chart in React?
- Live updating chart with data from database with Chart.js
- Changing chart data dynamically with C# from SQL database
- Create a pie chart using an array from a data table in chart.js
- How to create a chart.js scatter chart with data from two lists
- getting additional value fields from data source for dx.chartjs doughnut chart
More Query from same tag
- Chartjs Bar Chart Legend
- ChartJS - Help me custom tooltip
- Chart.js sciptable options: accessing the values of the data object?
- How to apply data dynamically on initialisation for chart.js in angular?
- Why is Chart.js forEach is undefined
- Chart.js: Combined Line and Bar Data
- How to show group by data just like sample image using Chart.js?
- How to lazy evaluate and execute javascript?
- Sql Server query that returns prices from each shop on each date and adds a 0 if no data is present for the shopId on a particular date
- Error in created hook (Promise/async): "TypeError: Cannot read property 'position' of undefined"
- vue-chartjs remove top rectangle (datasets label)
- Getting data into script tag from route?
- Change the background color of tooltip dynamically using chart color
- ChartJS - adding scroll to horizontal legend in Line chart
- Grouping the object by key for Chartjs bar chart
- Variable in JavaScript with firebase
- Vue watch not noticing changes on component prop
- How to get an attribute of element by jquery
- Error ReferenceError: CanvasGradient is not defined
- Chart.js - Multiple generateLegend() functions on same page
- chart.js Line chart with different background colors for each section
- Using Chart.js version 3, How to left justify the y-axis labels on a stacked bar chart?
- Chart.js is not respecting my container dimensions
- How to display value labels above graph bars using chart.js
- vue-chartjs load data from parent component
- Unable to produce desired UI output for Chartjs tooltips
- how to see labels without losing middle text in doughnut chart
- Add Padding Right to Extended Line Chart in Chart.js V2
- chart js showing different width of bar
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js