Chart.js

Mike George

Power Member
Joined
May 12, 2018
Messages
590
Reaction score
121
Hello everyone,
Has anyone used chart.js ?
I recently implemented it in my website..but it takes 3-4seconds to load the graph..

Does anyone else have had similar experience?
 
How do you plug in the data ? Never had issue with that library
 
How do you plug in the data ? Never had issue with that library
Via DB

Im collecting the sale,cost and calculate the profit sale-cost

To calculate the daily profit

If u need to see some code PM ME
 
Post the code you got here, there are higer chances of help.

PHP:
@extends('admin.layouts.app')
@section('title', getOption('app_name') . ' - Graph')
@section('content')
    <div class="row">
        <div class="col-md-10 col-md-offset-1" style="position: relative; ">
            <canvas id="myChart">
            </canvas>
        </div>
    </div>
@endsection
@push('scripts')
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script>
<script type="text/javascript">
var ctx = document.getElementById("myChart");
    // if(screen.width < 600){
        ctx.height = screen.height * .6;
    // }
Chart.defaults.line.showLines=true;
Chart.defaults.global.animation.duration = 5000;
// Chart.defaults.scales.xAxes.gridLines.display=false;
var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: {!! $gDate !!},
        datasets: [{
            label: "Daily Profit",
            data: {!! $gData !!},
            borderColor: 'rgba(0,99,132,1)',
            borderWidth: 1,
            pointRadius: 2,
        },{
            label: "Daily Order Price",
            data: {!! $gData1 !!},
            borderColor: 'rgba(255,0,0,1)',
            borderWidth: 1,
            pointRadius: 2,
        }],       
    },
    options : {
          animation: {
            duration: 5000
          },
          maintainAspectRatio: false,
        }
});
console.log(screen.height);
</script>
@endpush
 
Dunno if it makes a difference but you have an animation set there to 5 seconds... Maybe that's what's causing the slowness?
 
Dunno if it makes a difference but you have an animation set there to 5 seconds... Maybe that's what's causing the slowness?
That is the effect ..the slowness is when you try to load the page

The graph takes 5sec to appear

The slowness occured when the data were too many
With dummy data its fine
 
Well that's your issue... Have you looked at D3.js or Plotly as an alternative that might solve the speed issue?
 
ChartJS is a small & handy library, It might become a bit slow while handling multiple datasets, but you can improve its performance by doing some tips like:
  1. You can reduce the number of rendered points (either by implementing a basic sampling algorithm on your backend or frontend, take a look at ChartJS's GitHub issues #3410 and #4053)
  2. You can implement caching on your backend (by rendering ChartJS canvas using PhantomJS, take a look at the screen-capturing section on the project's website)*
  3. You can also move your js tag in the head section of your page or inside the bundle.js file (if you're using Webpack, don't have a lot of affection but It's better than nothing)
* Tip 2 requires more development and can become very costy for little teams
 
Since it's about daily sales and profit, you could easily cache the results in stead of
making the engine calculate every time the page is loaded.
 
Thank you guys ..ill provably wait to collect some data because what i want in the end is Monthly Profit so i can have a look how much i made in a year and what profit i had each month

In the monthly graph it will be lighter inbelieve
 
Back
Top