How to install chart.js - charts

I know this is a really dumb question but how to you install chartjs to use in a project ive looked at the documentation and it dosent say were or how to install it,i downloaded it from GitHub, I'm using xampp. thanks

If I understand correctly, you're looking to use ChartJS altogether. The package you download from Github includes a folder called Dist, which is where the distribution files are held.
Inside you'll find 4 files. Two are "bundles", which include Moment.JS used for time-scales. The other two don't. Finally, 2 are minified, the others aren't.
Basically, to "install" ChartJS, all you need to do is make sure it's getting referenced in your install. For the sake of simplicity, here's a CDN link of ChartJS v2.5:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
Include that in the header of your page and you can now use ChartJS.
All we have to do now is render a chart:
<canvas id="myChart" width="400" height="400"></canvas>
Finally, initiate the chart. Here's the example code from the start of the docs:
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
Your page should now render a bar chart!

Check This
...
<script>
javascript
angular.module("app", ["chart.js"])
// Optional configuration
.config(['ChartJsProvider', function (ChartJsProvider) {
// Configure all charts
ChartJsProvider.setOptions({
chartColors: ['#FF5252', '#FF8A80'],
responsive: false
});
// Configure all line charts
ChartJsProvider.setOptions('line', {
showLines: false
});
}])
.controller("LineCtrl", ['$scope', '$timeout', function ($scope, $timeout) {
$scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
$scope.onClick = function (points, evt) {
console.log(points, evt);
};
// Simulate async data update
$timeout(function () {
$scope.data = [
[28, 48, 40, 19, 86, 27, 90],
[65, 59, 80, 81, 56, 55, 40]
];
}, 3000);
}]);
</script>
</html>

Type in angular CLI - npm install --save chart.js

npm i chart.js
For more information about Chart.js, Visit https://installmd.com/i/javascript/chart-js

Related

Chart JS not showing when xAxes is added

I edited the existing tutorial and try to make a time series graph. I want to have a 24 hour format x axis. But when I add the xAxes property, the graph does not show up.
Link:
https://jsfiddle.net/cnpante/t54vk1fc/1/
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{{ title }}</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
<script src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
This is my xAxes property.
xAxes: [{
type: 'time',
time: {
format: 'HH:mm',
unit: 'hour',
unitStepSize: 1,
displayFormats: {
'minute': 'HH:mm',
'hour': 'HH:mm',
min: '00:00',
max: '23:59'
}
}
}],
seems to work fine here,
see following working snippet.
looks like the chart.js library is not included in the fiddle.
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["00:00", "01:25", "06:34", "15:21", "20:01", "22:00"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: false,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}],
xAxes: [{
type: 'time',
time: {
format: 'HH:mm',
unit: 'hour',
unitStepSize: 1,
displayFormats: {
'minute': 'HH:mm',
'hour': 'HH:mm',
min: '00:00',
max: '23:59'
}
}
}],
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<canvas id="myChart" width="600" height="300"></canvas>

Chart.js: Multiple stacked datasets

I am trying to create bar chart with each bar have different sets of data.
This is how normally things work in chart.js (without nested array of data) but this is not helpful for me:
datasets: [
{
label: "My First Label",
stack: "Stack 0"
data: [65, 59, 20, 81, 56, 55, 40],
},
{
label: "My Second Label",
stack: "Stack 0"
data: [65, 59, 20, 81, 56, 55, 40],
}
]
And the below code doesn't work as I am trying to feed data with nested array, is there a way to make it work?
datasets: [
{
label: "My First Label",
data: [[65, 59, 20, 81], [56, 55, 40]],
},
{
label: "My Second Label",
data: [[65, 59, 20], [30, 81, 56, 55, 40]],
}
]

Create chart in SPA(Aurelia) with MVVM pattern

I've an Aurelia app on which I've to display some charts. I'm quite familiar with the MVVM pattern in .Net, but not in javascript.
I've installed Chart.js in my app. In my viewModel, I've imported it (import { Chart } from 'chart.js';).
In my view I've a prepared a Canva:
<canvas id="myChart" width="400" height="400"></canvas>
Now, how am I supposed to generate this chart? Because for me it's kind of a view issue for what I create, so should I create a <script> tag and put the creation code in it? How do I bind with the data that will come from the view models?
Do I need to do some <require from="..:"></require> in the view?
Or should I instantiate the chart from the ViewModel?(in that case, when should I create it since in the constructor, the dom element will already exist, and in my opinion this break the loosely coupled approach).
Thank you!
EDIT
Here is currently what I've after #valichek answer:
ViewModel:
import { Aurelia, inject } from 'aurelia-framework';
import { Router, RouterConfiguration } from 'aurelia-router';
import { Chart } from 'chart.js';
import { log } from "../services/log";
export class ClanChest {
viewCanva: HTMLCanvasElement;
constructor() {
}
public attached() {
log.info('Starting to attach');
var myChart = new Chart(this.viewCanva, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
log.info('Finished');
}
}
View:
<template>
<h1>ClanChest</h1>
<canvas ref="viewCanva" width="400" height="400"></canvas>
</template>
The code is executed, I've no errors, but so far I've nothing displayed, the canva has been resized to 0x0 and that's it.
There are two places in VM: bind (data for the component are available) and attached (ready for DOM changes)
You can read more about component lifecycle here http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/creating-components/3
Also there is ref attribute that could be useful, when accessing the <canvas />
http://aurelia.io/hub.html#/doc/article/aurelia/binding/latest/binding-basics/5
Update: I have created own working example:
components/chartjs/chart.js
import {containerless} from 'aurelia-framework';
import ChartJs from 'chart.js'
#containerless()
export class Chart {
container;
constructor() {
}
attached() {
this.myChart = new ChartJs(this.container, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
}
components/chartjs/chart.html:
<template>
<canvas ref="container" width="400" height="400"></canvas>
</template>
somewhere on page:
<require from="components/chartjs/chart"></require>
<chart></chart>
Note: this is just example, maybe you will need some optimizations, like checking and avoiding memory leaks if present when component is re-attached

Chart.js and EJS: Display String, Not Calculation Result

I am using Chart.js in an EJS template and trying to display a partial date consisting of Year/month, e.g.: 2017/02 or 2017-02, but I can't find a way to display the string rather than the calculated value, e.g.: 1008.5 or 2015.
This displays as '2017/02' in a table in EJS:
<%= legend %>
But in my chart, I get 1008.5:
<canvas id="myChart" width="400" height="400"></canvas>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [<%= labels %>],
datasets: [{
label: <%= legend %>,
data: [<%= visits %>],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
I've tried using <%- %>, as well as HTML entities for '/', but that doesn't help.
It is necessary to quote strings, e.g.:
label: '<%= legend %>',

Charts implementation in Ionic2 framework

What's the easiest way to implement responsive charts (preferably pie chart) in Ionic2 framework?
If you still looking for an answer, I have created a sample ionic 2 project with ChartJs 2.0 and displayed a pie chart in the home page.
You need to include the chartjs.bundle.js file inside your index.html file as
<script src="assets/libs/Chart.bundle.js"></script>
and then in home.html
render the chart as
<chart [labels]="labels" [data]="data" type="bar"></chart>
and in home.ts
...
import { Chart } from 'ng2-chartjs2';
#Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
labels: string[] = ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"];
data: Chart.Dataset[] = [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}
];
}
If you need a fully compiled working project source code for reference, get it from my repo here
Hope it'll help.