Hiển thị các bài đăng có nhãn convert chartjs. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn convert chartjs. Hiển thị tất cả bài đăng

Thứ Tư, 24 tháng 11, 2021

Customizable multi-type, multi-y axis code ChartJS – Part 2

 Following the part code of ChartJS 2.x, after part 1 of ChartJS, we discussed the multi-line display of the chart. We continue to present to you some ChartJS modifications that may be required for your requirements in part 2 of this article will edit some more interesting options.

In addition to the multi lines, we will have multi y axes, which is a very important property when you want to display multiple lines with different data indicators on the same chart.

Highlights in this post:

– Show multiple yAxis columns, corresponding color annotation.

– Connect the 2 point line with data or 2 adjacent points.

– Ignoring the empty point or not ignore the empty point.

– Show and hide the lines with one click.

 

code-chartjs-connect-the-2-point-ignore-null-point

Connect the 2 point line with data and Ignoring the empty point.

 

code-chartjs-connect-the-2-point-not-ignore-null-point

Connect the 2 adjacent points and not ignore the empty point.

 

code-chartjs-hide-the-lines-with-one-click

Show and hide the lines with one click head label.

 

1. HTML tag to display Chart

<div class=”chart-container” style=”position: relative; height:auto; width:50%; margin:0 auto”>
<canvas id=”myChart” width=”900″ height=”300″></canvas>
</div>

 

2. Set default data retrieved from database

$data_source = [
“labels” => [‘2020-01’, ‘2020-02’, ‘2020-03’, ‘2020-04’, ‘2020-05’],
“el1” => [‘1’, ‘3’, null, null, ‘2’],
“el2” => [’11’, null, ’22’, ‘null’, ’33’],
“el3” => [’14’, ’25’, ’26’, ’30’, ’33’]
];

 

3. CSS

<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>

 

4. Load library ChartJS

<script src=”assets/chartjs/2.13.0moment.min.js”></script>
<script src=”assets/chartjs/2.8.0Chart.min.js”></script>
<script src=”assets/chartjs/utils.js”></script>

5. Show Chart with data

<script>
var datas = <?php echo json_encode($data_source); ?>;
var ctx = document.getElementById(‘myChart’);
var myChart = new Chart(ctx, {
type: ‘line’,
data: {
labels: datas[“labels”],
datasets: [
{
label: ‘Label1’,
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: datas[“el1”],
borderColor : “red”,
backgroundColor : “red”,
lineTension: 0, // strainghtlines
yAxisID: ‘Label1’,
},
{
label: ‘Label2’,
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: datas[“el2”],
borderColor : “blue”,
backgroundColor : “blue”,
lineTension: 0, // straightlines
yAxisID: ‘Label2’
},
{
label: ‘Label3’,
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: datas[“el3”],
borderColor : “#a9a518”,
backgroundColor : “#a9a518”,
lineTension: 0,
yAxisID: ‘Label3’
},
]
},
options: {
responsive: true,
title: {
display: true,
text: ‘Label’
},
tooltips: {
mode: ‘index’,
intersect: true
},
annotation: {
annotations: [
{
type: ‘line’,
mode: ‘horizontal’,
scaleID: ‘y-axis-0’,
value: 5,
borderColor: ‘rgb(75, 192, 192)’,
borderWidth: 4,
label: {
enabled: false,
content: ‘Label’
}
}
]
},
scales: {
yAxes: [
// {
//     ticks: {
//         beginAtZero: true // Common set all: set start y column = 0
//     }
// },
{
id: ‘Label3’,
type: ‘linear’,
position: ‘left’,
ticks: {
min: 0,
fontColor: “#a9a518”, // color of yAxes
},
},
{
id: ‘Label2’,
type: ‘linear’,
position: ‘left’,
ticks: {
min: 0,
fontColor: “blue”, // color of yAxes
},
},
{
id: ‘Label1’,
type: ‘linear’,
position: ‘left’,
ticks: {
min: 0,
fontColor: “red”, // color of yAxes
},
},
]
},
// true: connect the 2 point line, ignoring the empty point
// false: connect 2 adjacent points, do not ignore the empty point
spanGaps: false
}
});
</script>

 

Download full code at here:


Thứ Năm, 18 tháng 11, 2021

Code Chart JS with multi type – Part 1

We all know how powerful ChartJS 2.x is when it comes to chart rendering, and in some cases ChartJS is more robust when compared to Google Chart. In this content, we will talk about multi-type show in ChartJS.

Assuming you are using PHP server, we need json_encode to show in JS. In addition to the line chart, you can set up in the form of bar, radar, …

Line chart

Bar chart

 

1. Html tag to show chart:

<div class=”chart-container” style=”position: relative; height:auto; width:50%; margin:0 auto”>
<canvas id=”myChart” width=”900″ height=”300″></canvas>
</div>

– Set default data_source get from php:

$data_source = [
“labels” => [
‘2020-01’,
‘2020-02’,
‘2020-03’,
‘2020-04’,
‘2020-05′,
],
“el1” => [
’10’, ’15’, ’20’, ’25’, ’31’
],
“el2” => [
’11’, ’22’, ’22’, ’28’, ’32’
],
“el3” => [
’14’, ’25’, ’26’, ’30’, ’33’
]
];

 

2. Css:

<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>

 

3. JS library

<!– // start chartjs  –>
<script src=”assets/chartjs/2.13.0moment.min.js”></script>
<script src=”assets/chartjs/2.8.0Chart.min.js”></script>
<script src=”assets/chartjs/utils.js”></script>

4. Map js code to show the chart

var datas = <?php echo json_encode($data_source); ?>;
var ctx = document.getElementById(‘myChart’);
var myChart = new Chart(ctx, {
type: ‘line’,
data: {
labels: datas[“labels”],
datasets: [
{
label: ‘Label1’,
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: datas[“el1”],
borderColor : “red”,
backgroundColor : “red”,
lineTension: 0, // strainghtlines
},
{
label: ‘Label2’,
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: datas[“el2”],
borderColor : “blue”,
backgroundColor : “blue”,
lineTension: 0, // straightlines
},
{
label: ‘Label3’,
borderColor: window.chartColors.blue,
borderWidth: 2,
fill: false,
data: datas[“el3”],
borderColor : “#a9a518”,
backgroundColor : “#a9a518”,
lineTension: 0,
},

]
},
options: {
responsive: true,
title: {
display: true,
text: ‘Label’
},
tooltips: {
mode: ‘index’,
intersect: true
},
annotation: {
annotations: [
{
type: ‘line’,
mode: ‘horizontal’,
scaleID: ‘y-axis-0’,
value: 5,
borderColor: ‘rgb(75, 192, 192)’,
borderWidth: 4,
label: {
enabled: false,
content: ‘Label’
}
}
]
}
}
});

 

Download full code at here

Download full code

Size: 119 KB

 

Readmore: Customizable multi-type, multi-y axis code ChartJS – Part 2