ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  makeChart2() in C++
Posted by Mike Meyers on Dec-09-2019 02:12
I'm trying to use C++ and ChartDirector for a web based CGI application. I have used Perl/Python successfully, but need some functionality for the webapp to be in C++.

My experience and documentation from Perl tells me to use makeChart2() to stream the binary chart to the browser. However using C++, makeChart2() is not available

[root@ws cgi-bin]# make
g++ -I../ChartDirector/include -L../ChartDirector/lib -Wl,-R../ChartDirector/lib simplebar.cpp -lchartdir -o simplebar
simplebar.cpp: In function ‘int main(int, char**)’:
simplebar.cpp:25:21: error: ‘class XYChart’ has no member named ‘makeChart2’
     std::cout << c->makeChart2(0);
                     ^
make: *** [simplebar] Error 1
[root@ws cgi-bin]#

The simplebar.cpp is the demo with minor changes that mimic what documentation would suggest we do in Perl

[root@ws cgi-bin]# diff ./simplebar.cpp ../ChartDirector/cppdemo/simplebar/simplebar.cpp
2d1
< #include <iostream>
24,25c23,24
<     std::cout << ("Content-type: image/pngnn");
<     std::cout << c->makeChart2(0);
---
>     // Output the chart
>     c->makeChart("simplebar.png");
30a30
>
[root@ws cgi-bin]#

In the end, I would like to be calling this from HTML like.


<HTML>
  <HEAD>
  </HEAD>

  <BODY>
      <CENTER><H1>Test Dashboard</H1>
      <BR><HR><BR>
      <IFRAME SRC="cgi-bin/simplebar" frameborder="0"></IFRAME>
      </CENTER>
  </BODY>

</HTML>

Anyone have experience with C++ CGI binaries exposing ChartDirector charts? If I could see sample code of the changes to cppdemo/simplebar/simplebar.cpp that would be required to invoke from the HTML above, that would be ideal.

Thanks,
Mike

  Re: makeChart2() in C++
Posted by Mike Meyers on Dec-09-2019 02:37
Sorry - I should add: Red Hat Enterprise Linux Server release 7.7

  Re: makeChart2() in C++
Posted by Peter Kwan on Dec-09-2019 18:46
Hi Mike,

It should be like:

... output Content-type as usual ...

// We need to pointer to the image bytes as well as the length
MemBlock m = c->makeChart(Chart::PNG);
fwrite(m.data, m.length, 1, stdout);

or

std::cout.write(m.data, m.length);

On Linux, you probably do not need to worry about "text mode" or "binary mode" output. In case someone in future finds this post and want to do similar things on Windows, the stdout on Windows may perform "text mode" conversions to the data. For example, "\n" may get converted to "\n\r", and bytes less than 0x20 (which are ASCII control characters) may also be modified. This will corrupt binary contents like images.  You may need to explicitly set the stdout to binary mode on windows.

setmode(fileno(stdout), O_BINARY);

Regards
Peter Kwan

  Re: makeChart2() in C++
Posted by Mike Meyers on Dec-10-2019 01:12
Thanks Peter! That got it.

For folks that may follow -

Here are the changes made to cppdemo/simplebar/simplebar.cpp so that I could use the code in a CGI binary called from an HTML tag:

    <IFRAME SRC="cgi-bin/simplebar" frameborder="0"></IFRAME>

example % diff ./simplebar.cpp ../ChartDirector/cppdemo/simplebar/simplebar.cpp
2,3d1
< #include <iostream>
< #include <stdio.h>
25,28c23,24
<     MemBlock png = c->makeChart(Chart::PNG);
<     std::cout << "Content-type: image/png\n\n";
<     std::cout.write(png.data, png.len);
<     fflush(stdout);
---
>     // Output the chart
>     c->makeChart("simplebar.png");
33a30
>
example %