Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stevetrader's avatar

Adding Graph to Spark Dashboard

I'm trying to add a graph to the dashboard area of Spark when a user logs in. I've copied everything from the metrics section of Spark because I didn't want to add any more libraries or anything. I'm using junk data for now just to make it work. All the data shows up in the array in vue, so I'm not sure what's going on.

Here's the home.js file:

Vue.component('home', {
    props: ['user'],

    data() {
        return {
            fg_usage: [
                { created_at: '2016-05-18 14:13:13', count: '123' },
                { created_at: '2016-05-19 14:13:13', count: '243' },
                { created_at: '2016-05-20 14:13:13', count: '214' },
            ]
        };
    },

    ready() {
        //this.fg_drawUsageChart();
    },

    methods: {

        fg_drawUsageChart() {
            return this.fg_drawChart(
                'fg_usageChart', 3, fg_usage => fg_usage.count
            );
        },

        fg_drawChart(id, days, dataGatherer, scaleLabelFormatter) {
            var dataset = this.fg_baseChartDataSet;

            dataset.data = _.map(_.last(this.fg_usage, days), dataGatherer);

            var data = {
                labels: _.last(this.fg_availableChartDates, days),
                datasets: [dataset]
            };

            var options = { responsive: true };

            if (arguments.length === 4) {
                options.scaleLabel = scaleLabelFormatter;
            }

            var chart = new Chart(
                document.getElementById(id).getContext('2d')
            ).Line(data, options);
        }

    },

    computed: {

        fg_availableChartDates() {
            return _.map(this.fg_usage, each => {
                return moment(each.created_at).format('M/D');
            });
        },

        fg_baseChartDataSet() {
            return {
                label: "Dataset",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "rgba(151,187,205,1)",
                pointColor: "rgba(151,187,205,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
            };
        }

    }
});

And here's the added line in home.blade.php:

<canvas id="fg_usageChart" height="100"></canvas>

Looks pretty straight forward, but I don't understand why it's breaking. It seems to break all js when I uncomment the ready portion.

Thanks.

0 likes
1 reply
stevetrader's avatar

Ok, so I finally figured out Chart.js was being brought in from cdn to handle this on the metrics page. So I've added it to the dashboard page, and my graph now works, except that it's super tiny (unreadable) even though the canvas tag is the same as the metrics page. No matter what I change for css the graph doesn't change. Any ideas?

Please or to participate in this conversation.