Chart.js: Multiple stacked datasets - charts

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]],
}
]

Related

Mapbox - Get region from autocomplete result

How can I get region from the selected result from autocomplete?
From the result I am getting, there is 3rd object named region but actually it is department not region.
Here is the example address:
54b route de brie, 91800 Brunoy, France
Mapbox is giving me: Essonne // Its department not region
But actually its: Ile-de-France
How do I get the correct region?
Here is my working demo:
https://jsfiddle.net/rv085oL1/
That information isn't included. But if you just need your site to work in France, it would be straightforward to include a lookup table mapping from département to région, using the last two characters of the short_code. Here's one: https://gist.github.com/SiamKreative/f1074ed95507e69d08a0
"regions": {
"alsace": [67, 68],
"aquitaine": [40, 47, 33, 24, 64],
"auvergne": [43, 3, 15, 63],
"basse-normandie": [14, 61, 50],
"bourgogne": [21, 58, 71, 89],
"bretagne": [29, 35, 22, 56],
"centre": [45, 37, 41, 28, 36, 18],
"champagne-ardenne": [10, 8, 52, 51],
"corse": ["2b", "2a"],
"franche-compte": [39, 25, 70, 90],
"haute-normandie": [27, 76],
"languedoc-roussillon": [48, 30, 34, 11, 66],
"limousin": [19, 23, 87],
"lorraine": [55, 54, 57, 88],
"midi-pyrennees": [46, 32, 31, 12, 9, 65, 81, 82],
"nord-pas-de-calais": [62, 59],
"pays-de-la-loire": [49, 44, 72, 53, 85],
"picardie": [2, 60, 80],
"poitou-charentes": [17, 16, 86, 79],
"provences-alpes-cote-dazur": [4, 5, 6, 13, 84, 83],
"rhones-alpes": [38, 42, 26, 7, 1, 74, 73, 69],
"ile-de-france": [77, 75, 78, 93, 92, 91, 95, 94]
},

How to install chart.js

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

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 %>',

How to search historic information from IBM Bluemix Weather Almanac API

I need to receive some information from Weather Company Data For IBM Bluemix APIs about a specific period (from jan 2012 to jan 2015).
The documentation includes this example API:
https://twcservice.mybluemix.net:443/api/weather/v1/geocode/33.40/-83.42/almanac/daily.json?units=e&start=0112&end=0115
But this is the result:
{"metadata":{"transaction_id":"1472145329818:-319071226","status_code":400},"success":false,"errors":[{"error":{"code":"PVE-0003","message":"The field 'start' contains a value '112' which is outside the expected range of [1 to 12]."}}]}
Can you let me know how I can search the historic information?
Thank you
https://twcservice.eu-gb.mybluemix.net/api/weather/v1/geocode/33.40/-83.42/almanac/daily.json?start=0112&end=0115&units=e
It works for me!!
{
"metadata": {
"language": "en-US",
"transaction_id": "1501849033676:-1423101749",
"version": "1",
"latitude": 33.4,
"longitude": -83.42,
"units": "e",
"expire_time_gmt": 1501869684,
"status_code": 200
},
"almanac_summaries": [
{
"class": "almanac",
"station_id": "095988",
"station_name": "MONTICELLO",
"almanac_dt": "0112",
"interval": "D",
"avg_hi": 56,
"avg_lo": 28,
"record_hi": 75,
"record_hi_yr": 1916,
"record_lo": 0,
"record_lo_yr": 1982,
"mean_temp": 42,
"avg_precip": 0.13,
"avg_snow": 0.1,
"record_period": 30
},
{
"class": "almanac",
"station_id": "095988",
"station_name": "MONTICELLO",
"almanac_dt": "0113",
"interval": "D",
"avg_hi": 56,
"avg_lo": 28,
"record_hi": 77,
"record_hi_yr": 1911,
"record_lo": 8,
"record_lo_yr": 1918,
"mean_temp": 42,
"avg_precip": 0.12,
"avg_snow": 0,
"record_period": 30
},
{
"class": "almanac",
"station_id": "095988",
"station_name": "MONTICELLO",
"almanac_dt": "0114",
"interval": "D",
"avg_hi": 56,
"avg_lo": 28,
"record_hi": 78,
"record_hi_yr": 1937,
"record_lo": 10,
"record_lo_yr": 1918,
"mean_temp": 42,
"avg_precip": 0.13,
"avg_snow": 0,
"record_period": 30
},
{
"class": "almanac",
"station_id": "095988",
"station_name": "MONTICELLO",
"almanac_dt": "0115",
"interval": "D",
"avg_hi": 56,
"avg_lo": 28,
"record_hi": 80,
"record_hi_yr": 1932,
"record_lo": 11,
"record_lo_yr": 1964,
"mean_temp": 42,
"avg_precip": 0.12,
"avg_snow": 0,
"record_period": 30
}
]
}