0

I'm trying to plot a dataset into a chartJS line chart:

This is my data:

[{"close":0.00786609},{"close":0.00784},{"close":0.00784},
 {"close":0.00780507},{"close":0.007816},{"close":0.00781599},
 {"close":0.00780166},{"close":0.00778403},{"close":0.00782001},
 {"close":0.0078},{"close":0.00778},{"close":0.007799},
 {"close":0.00775057},{"close":0.0077688},{"close":0.00775001}]

This is my code:

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    // The type of chart we want to create
    type: 'line',

    // The data for our dataset
    data: {

        datasets: [{
            label: "My First dataset",
            backgroundColor: 'rgb(255, 99, 132)',
            borderColor: 'rgb(255, 99, 132)',
            data: <%= JSON.stringify(prices) %>,
        }]
    },

    // Configuration options go here
    options: {}
});

How can I fix this?

1 Answer 1

1

You have configured your chart to be of type "line". The documentation for this type specifies that a dataset's data can be an Array of Numbers or an Array of Points, where a Point is an Object with a x property and a y property.

Your data is a JSON stringified Array of objects, each with a close property.

You need to map your prices Array into an Array of Numbers. In JavaScript, this could be done as:

prices.map(function (price) {
    return price.close;
});

I have created a fiddle as an example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.