vedika shrivas
vedika shrivas

Reputation: 333

How to plot a line chart in ChartJS?

I have created a dummy table and I want to plot a p/l column in type ' line'. Here's my code so far:

 var niftyProfit = [];
 var niftyProfitChartData = [];
 var sum = 0;
 data.forEach(function(obj) {
     niftyProfit.push(obj.profit);
 });
 for (var i in niftyProfit) {
     sum = sum + niftyProfit[i];
     niftyProfitChartData.push(sum);
 }

obj.profit is the p/l column data, but in a line chart. I want to populate the value, which I did in for loop for eg. consider profit = [4,2,-3,-5], then my final array to populate in chart would become [4,6,3,-2]. How would I achieve this? Here's a fiddle: http://jsfiddle.net/zkrh7/141/

Upvotes: 2

Views: 1334

Answers (1)

muasif80
muasif80

Reputation: 6006

I have updated the jsfiddle with following changes and it is working.

In the script

var data = [{"profit": 345}, {"profit":-4}, {"profit":58}, {"profit": -30}];
//or you can use
var data = [{"profit": 4}, {"profit":2}, {"profit":-3}, {"profit": -5}];

populateChartData(data);

and added the Chart library as external resource that you can see as well. Then

Also i had added the canvas to the html

<canvas id="niftyTrackerChart" width="800" height="400"></canvas>

Updated JSFiddle Link http://jsfiddle.net/muasif80/zkrh7/156/

Have a look at this link How to uses numbers array for line chart?

This is saying if you are only using numbers then you have to define a labels array too for the x-axis.

Upvotes: 2

Related Questions