Using chart.js from a desktop application

I Love Xbase++ (ILX)
The portal for Xbase++ developers worldwide

Anna Hristova

New member
Staff member
I am using always the latest Xbase++ build
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
Test.html is an example of a bar chart. Chart.js is integrated via cdnjs.cloudflare.com. However, it is also possible to load chart.js locally from the hard disk.
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>
After starting the sample application, a dialog opens on which the bar chart is displayed.
1664551839329.png

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:

Igor Golobrodskiy

New member
I am using always the latest Xbase++ build
Oct 20, 2022
1
0
1
Customer Identifier
E062905
We do not use the workbench for out applications, so please can you give advise/instructions me on how I can use XbpHTMLViewer2.
Regards
Steve Poore
I did an example in the workbench according this manual and after building downloaded the next 3 files:
alaska-software.xbphtmlviewer2.dll
alaska-software.webview2core.dll
alaska-software.xbphtmlviewer2.lib.

Then I add these files in my project and it works now:)