- Aug 9, 2022
- 9
- 2
- 1
- Customer Identifier
- E114627
About chart.js
Chart.js is a simple and flexible JavaScript library for displaying charts. Chart.js is freely available under the MIT license.Chart.js Xbase++ usage
Although the handling of the library itself is not subject of this article, the usage with the Xbase++ XbpHtmlViewer2 asset turns out to be quite simple.Because XbpHtmlViewer2 is available as an asset it has to be added to the Xbase++ target with the asset manager of the workbench. After that the following code can be compiled and linked:
Xbase++:
#include "xbp.ch"
PROCEDURE Main()
LOCAL oDlg
LOCAL oXbp
LOCAL cTestHtml
SET CHARSET TO ANSI
// Create a form for hosting the HTML viewer object
oDlg := XbpDialog():new( AppDesktop() )
oDlg:taskList := .T.
oDlg:create( ,,, {640,680} )
CenterControl( oDlg )
// Create the HTML viewer object and arrange for it to cover the form's
// content area
oXbp := XbpHTMLViewer2():new( oDlg:drawingArea )
oXbp:layoutAlign := XBPLAYOUT_LEFT + XBPLAYOUT_TOP + XBPLAYOUT_RIGHT + XBPLAYOUT_BOTTOM
oXbp:create( ,, {0,0}, oDlg:ClientSize )
// Load test.html from drive and show it.
cTestHtml := StrTran( AppName(.T.), AppName(.F.), ) + "test.html"
oXbp:navigate( cTestHtml )
oDlg:showModal()
oDlg:destroy()
RETURN
PROCEDURE AppSys()
// Prevent creation of the default console window
RETURN
HTML:
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chart.js Line Chart</title>
<!--Chart.js JS CDN-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
<script>
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 7, 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: {
y: {
beginAtZero: true
}
}
}
});
</script>
</body>
</html>
See also:
Xbase++ documentation: Class XbpHTMLViewer()ILX article: Xbase++ Workbench Asset Management
Chart.js homepage: https://www.chartjs.org/
Chart.js support: https://stackoverflow.com/questions/tagged/chart.js
Chart.js documentation: https://www.chartjs.org/docs/latest/
Chart.js examples: https://www.chartjs.org/docs/latest/samples/information.html
Chart.js licence: https://github.com/chartjs/Chart.js/blob/master/LICENSE.md
Last edited by a moderator: