|
| 1 | +*--------------------------------------------------------------- |
| 2 | +. |
| 3 | +. Program Name: chart_png_samp.pls |
| 4 | +. Description: PL/B and Google Charts and PNG Generation |
| 5 | +. See link: |
| 6 | +. "https://developers.google.com/chart/interactive/docs/quick_start" |
| 7 | +. "https://developers.google.com/chart/interactive/docs/printing" |
| 8 | +. |
| 9 | +. For web server change plbwebstartwv.html to look like |
| 10 | +. example_plbwebstartwv.html |
| 11 | +. |
| 12 | +. |
| 13 | +. Revision History: |
| 14 | +. |
| 15 | +. Date: 07/15/2025 |
| 16 | +. Original code |
| 17 | +. |
| 18 | + INCLUDE plbequ.inc |
| 19 | + INCLUDE plbmeth.inc |
| 20 | + INCLUDE plbstdlib.inc |
| 21 | + |
| 22 | +// Runtime status variables |
| 23 | +isGui BOOLEAN |
| 24 | +isWebSrv BOOLEAN |
| 25 | +isPlbSrv BOOLEAN |
| 26 | +isWebview BOOLEAN |
| 27 | +isWebCliApp BOOLEAN |
| 28 | +isSmallScreen BOOLEAN |
| 29 | + |
| 30 | +// Global data |
| 31 | +mainForm PLFORM chart_png_samp.plf |
| 32 | +Client CLIENT |
| 33 | +htmlBuffer DIM 4096 |
| 34 | +htmlPart1 INIT "<head>": |
| 35 | + "<script type='text/javascript' src='https://www.google.com/jsapi'></script>",0xD,0xA: |
| 36 | + "<script type='text/javascript'>",0xD,0xA: |
| 37 | + "// Load the Visualization API and the piechart package.",0xD,0xA: |
| 38 | + "google.load('visualization', '1.0', {'packages':['corechart']});",0xD,0xA: |
| 39 | + "// Set a callback to run when the Google Visualization API is loaded.",0xD,0xA: |
| 40 | + "google.setOnLoadCallback('ready');",0xD,0xA: |
| 41 | + "// Callback that creates and populates a data table,",0xD,0xA: |
| 42 | + "// instantiates the pie chart, passes in the data and",0xD,0xA: |
| 43 | + "// draws it.",0xD,0xA: |
| 44 | + "function drawChart() {",0xD,0xA: |
| 45 | + "// Create the data table.",0xD,0xA: |
| 46 | + "var data = new google.visualization.DataTable();",0xD,0xA |
| 47 | + |
| 48 | +htmlPart2 INIT "// Wait for the chart to finish drawing before calling the getImageURI() method.",0xD,0xA: |
| 49 | + "google.visualization.events.addListener(chart, 'ready', function () {",0xD,0xA: |
| 50 | + "document.getElementById('chart_png').innerHTML = '<a href=#"' + chart.getImageURI() + '#">Printable version</a>';",0xD,0xA: |
| 51 | + "});",0xD,0xA: |
| 52 | + "chart.draw(data, options);",0xD,0xA: |
| 53 | + "}",0xD,0xA: |
| 54 | + "</script>",0xD,0xA: |
| 55 | + "</head>",0xD,0xA: |
| 56 | + "<body>",0xD,0xA: |
| 57 | + "<!--Div that will hold the pie chart-->",0xD,0xA: |
| 58 | + "<div id='chart_png' style='display: none'></div>",0xD,0xA: |
| 59 | + "<div id='chart_div'></div>",0xD,0xA: |
| 60 | + "<script type='text/javascript'>",0xD,0xA: |
| 61 | + "drawChart();",0xD,0xA: |
| 62 | + "</script>",0xD,0xA: |
| 63 | + "</body>",0xD,0xA |
| 64 | + |
| 65 | +htmlChart1 INIT "data.addColumn('string', 'Topping');",0xD,0xA: |
| 66 | + "data.addColumn('number', 'Slices');",0xD,0xA: |
| 67 | + "data.addRows([",0xD,0xA: |
| 68 | + " ['Mushrooms', 3],",0xD,0xA: |
| 69 | + " ['Onions', 1],",0xD,0xA: |
| 70 | + " ['Olives', 1],",0xD,0xA: |
| 71 | + " ['Zucchini', 1],",0xD,0xA: |
| 72 | + " ['Pepperoni', 2]",0xD,0xA: |
| 73 | + " ]);",0xD,0xA: |
| 74 | + "// Set chart options",0xD,0xA: |
| 75 | + "var options = {'title':'How Much Pizza I Ate Last Night',",0xD,0xA: |
| 76 | + " 'width':500,",0xD,0xA: |
| 77 | + " 'height':400};",0xD,0xA: |
| 78 | + "// Instantiate and draw our chart, passing in some options.",0xD,0xA: |
| 79 | + "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));",0xD,0xA |
| 80 | + |
| 81 | +htmlChart2 INIT "data.addColumn('string', 'Task');",0xD,0xA: |
| 82 | + "data.addColumn('number', 'Hours per Day');",0xD,0xA: |
| 83 | + " data.addRows([",0xD,0xA: |
| 84 | + " ['Work', 11],",0xD,0xA: |
| 85 | + " ['Eat', 2],",0xD,0xA: |
| 86 | + " ['Commute', 2],",0xD,0xA: |
| 87 | + " ['Watch TV', 2],",0xD,0xA: |
| 88 | + " ['Sleep', 7]",0xD,0xA: |
| 89 | + " ]);",0xD,0xA: |
| 90 | + "var options = {",0xD,0xA: |
| 91 | + "title: 'My Daily Activities',",0xD,0xA: |
| 92 | + " is3D: true,",0xD,0xA: |
| 93 | + " 'width':500,",0xD,0xA: |
| 94 | + " 'height':400};",0xD,0xA: |
| 95 | + "// Instantiate and draw our chart, passing in some options.",0xD,0xA: |
| 96 | + "var chart = new google.visualization.PieChart(document.getElementById('chart_div'));",0xD,0xA |
| 97 | + |
| 98 | + |
| 99 | +pngData DIM ^65000 |
| 100 | +pngBinary DIM ^65000 |
| 101 | +seq FORM "-1" |
| 102 | +pngFile FILE BUFFER=65000 |
| 103 | +*--------------------------------------------------------------- |
| 104 | +// <program wide variables> |
| 105 | +*................................................................ |
| 106 | +. |
| 107 | +. Code start |
| 108 | +. |
| 109 | + CALL Main |
| 110 | + LOOP |
| 111 | + EVENTWAIT |
| 112 | + REPEAT |
| 113 | + STOP |
| 114 | +*................................................................ |
| 115 | +. |
| 116 | +. Check what type of runtime support we have |
| 117 | +. |
| 118 | +CheckStatus LFUNCTION |
| 119 | + ENTRY |
| 120 | +runtimeData DIM 50 |
| 121 | +runtimeVersion DIM 5 |
| 122 | +unused DIM 1 |
| 123 | +plbRuntime DIM 9 |
| 124 | +WidthData DIM 5 |
| 125 | +checkWidth FORM 5 |
| 126 | +. |
| 127 | +. Check for character mode runtime |
| 128 | +. |
| 129 | + GETMODE *GUI=isGui |
| 130 | + |
| 131 | + IF (isGui) |
| 132 | +. |
| 133 | +. check windows console. |
| 134 | +. |
| 135 | + CLOCK VERSION,runtimeData |
| 136 | + UNPACK runtimeData,runtimeVersion,unused,plbRuntime |
| 137 | + CHOP plbRuntime |
| 138 | + |
| 139 | + IF (NOCASE plbRuntime == "PLBCON") |
| 140 | + CLEAR isGui |
| 141 | + ENDIF |
| 142 | + |
| 143 | + IF (NOCASE plbRuntime == "PLBWEBSV") |
| 144 | + SET isWebSrv |
| 145 | + ENDIF |
| 146 | + |
| 147 | + IF (NOCASE plbRuntime == "PLBSERVE") |
| 148 | + SET isPlbSrv |
| 149 | + ENDIF |
| 150 | + |
| 151 | + ENDIF |
| 152 | + |
| 153 | + IF (isGui) |
| 154 | + WINHIDE |
| 155 | + |
| 156 | + Client.Getstate Giving isWebCliApp Using $CLI_STATE_CORDOVA |
| 157 | + Client.Getstate Giving isWebview Using $CLI_STATE_BOOTSTRAP5 |
| 158 | +. |
| 159 | +. Check for a small screen (such as iphone) |
| 160 | +. |
| 161 | + IF (isWebSrv) |
| 162 | + Client.GetWinInfo Giving WidthData Using 0x2 |
| 163 | + MOVE WidthData To checkWidth |
| 164 | + MOVE (checkWidth <= 700) to isSmallScreen |
| 165 | + ENDIF |
| 166 | + |
| 167 | + ENDIF |
| 168 | + |
| 169 | + FUNCTIONEND |
| 170 | + |
| 171 | +*................................................................ |
| 172 | +. |
| 173 | +. Chart 1 |
| 174 | +. |
| 175 | +Chart1 LFUNCTION |
| 176 | + ENTRY |
| 177 | + PACK htmlBuffer,htmlPart1,htmlChart1,htmlPart2 |
| 178 | + htmlChart.InnerHtml Using htmlBuffer |
| 179 | + SETPROP btnOutPng,*enabled=$TRUE |
| 180 | + FUNCTIONEND |
| 181 | + |
| 182 | +*................................................................ |
| 183 | +. |
| 184 | +. Chart 2 |
| 185 | +. |
| 186 | +Chart2 LFUNCTION |
| 187 | + ENTRY |
| 188 | + PACK htmlBuffer,htmlPart1,htmlChart2,htmlPart2 |
| 189 | + htmlChart.InnerHtml Using htmlBuffer |
| 190 | + SETPROP btnOutPng,*enabled=$TRUE |
| 191 | + FUNCTIONEND |
| 192 | + |
| 193 | +*................................................................ |
| 194 | +. |
| 195 | +. Output a PNG file |
| 196 | +. |
| 197 | +OutputPng LFUNCTION |
| 198 | + ENTRY |
| 199 | +scanFP INTEGER 2 |
| 200 | +result FORM 4 |
| 201 | + |
| 202 | + htmlChart.GetAttr giving pngData using "chart_png","html" |
| 203 | + SCAN "data:image/png;base64,", pngData |
| 204 | + IF EQUAL |
| 205 | + MOVEFPTR pngData,scanFP |
| 206 | + SCAN "#>", pngData |
| 207 | + IF EQUAL |
| 208 | + LENSET pngData |
| 209 | + RESET pngData,(scanFP+22) |
| 210 | + DECODE64 pngData,pngBinary |
| 211 | + PREP pngFile, "chart.png" |
| 212 | + WRITE pngFile,SEQ;*LL,pngBinary; |
| 213 | + CLOSE pngFile |
| 214 | + |
| 215 | + ALERT NOTE,"The file chart.png has been generated.",result |
| 216 | + ENDIF |
| 217 | + ENDIF |
| 218 | + FUNCTIONEND |
| 219 | + |
| 220 | +*................................................................ |
| 221 | +. |
| 222 | +. Main - Main program entry point |
| 223 | +. |
| 224 | +Main LFUNCTION |
| 225 | + ENTRY |
| 226 | +result FORM 4 |
| 227 | + |
| 228 | + CALL CheckStatus |
| 229 | + |
| 230 | + IF (!isGui) |
| 231 | + KEYIN "This runtime can't run this program. ",result |
| 232 | + STOP |
| 233 | + ENDIF |
| 234 | + |
| 235 | + IF (!isWebview) |
| 236 | + ALERT STOP,"This program requires WebView support.",result |
| 237 | + STOP |
| 238 | + ENDIF |
| 239 | + |
| 240 | +. |
| 241 | +. On smaller screens change to % positioning for left and width |
| 242 | +. |
| 243 | + IF (isWebCliApp || isSmallScreen) |
| 244 | + SETMODE *PERCENTCONVERT=1 |
| 245 | + ENDIF |
| 246 | + |
| 247 | + FORMLOAD mainForm |
| 248 | + |
| 249 | + IF (!isWebSrv) |
| 250 | + PACK htmlBuffer,htmlPart1,htmlChart1,htmlPart2 |
| 251 | + htmlChart.InnerHtml Using htmlBuffer |
| 252 | + ENDIF |
| 253 | + |
| 254 | + SETPROP btnOutPng,*enabled=$FALSE |
| 255 | + SETFOCUS btnChart1 |
| 256 | + |
| 257 | + FUNCTIONEND |
0 commit comments