Recent

Author Topic: [SOLVED] Looking for advice how to print 2 charts together  (Read 5063 times)

AL

  • Sr. Member
  • ****
  • Posts: 261
[SOLVED] Looking for advice how to print 2 charts together
« on: April 01, 2020, 04:40:54 pm »
I have a form with 2 tacharts on it.
I can use the print option to print / save to Jpg each individual chart.
I would like to print both charts on the same page and save both charts in a jpg.
How would you approch that?

I tought of printing the form itself, but then i have also all buttons.
Copying the form to a canvas then 'erasing" the buttons before printing 
I have also a tchartlist on the form that would be nice to include in the print.

Any ideas welcome.
Thanks

here is how the from looks like
« Last Edit: April 02, 2020, 12:05:33 am by AL »
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

eljo

  • Sr. Member
  • ****
  • Posts: 468
Re: Looking for advice how to print 2 charts together
« Reply #1 on: April 01, 2020, 05:13:00 pm »
use lazreport and add as many charts as you want to a single page and print them. There should be a lazreport sample installed with taChart components.
relevant forum thread
https://forum.lazarus.freepascal.org/index.php/topic,16571.0.html

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Looking for advice how to print 2 charts together
« Reply #2 on: April 01, 2020, 05:28:17 pm »
As for printing, please look at the print demo which comes with TAChart. It defines a rectangle on the paper into which the chart will be printed. In the same way you defne a second rectangle for the second chart, and you just duplicated the code used for the first chart.

As for saving as a combined jpeg, you should use the function Chart.SaveToImage(format) where format is the class of the image to be created, e.g. TBitmap. This way you can save each chart into a separate bitmap, and later you can combine them by drawing them on the canvas of the final jpeg.

See attachment.

AL

  • Sr. Member
  • ****
  • Posts: 261
Re: Looking for advice how to print 2 charts together
« Reply #3 on: April 01, 2020, 06:03:08 pm »
Two good ideas/solutions,  Thank You
I have some study to make to use Lazreport.  It is not clear to me how items are added to the report.  I will look in the docs.

WP I like you solution, it will add less overhead to the program.
Any idea how I could add the tchartlistbox which is at the right of the bottom chart
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Looking for advice how to print 2 charts together
« Reply #4 on: April 01, 2020, 06:36:03 pm »
Any idea how I could add the tchartlistbox which is at the right of the bottom chart
When you want it like a listbox then I think you must draw it yourself on the printer and the jpeg canvas - which may be some work...

Or you use Legend built into the chart. Set Legend.UseSideBar to false if you want the legend to be inside the chart as you did with the ChartListBox. Then make sure that the legend is hidden (Legend.Visible = false, which is the default anyway). In the printing routine you set Legend.Visible to true before calling Chart.Draw, and afterwards you switch it back to off again. The same with the output to the combined image (change Legend visibility before/after Chart.SaveToImage). Note that the legend shows the visible series only, and it does not show any checkboxes. So, the result will look a bit different from the screen.

AL

  • Sr. Member
  • ****
  • Posts: 261
Re: Looking for advice how to print 2 charts together
« Reply #5 on: April 01, 2020, 07:00:29 pm »
Ok , that wil do the job
Thank you

Update:   Implemented, works very well.  Thank you again WP.
« Last Edit: April 02, 2020, 12:05:09 am by AL »
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

AL

  • Sr. Member
  • ****
  • Posts: 261
Re: [SOLVED] Looking for advice how to print 2 charts together
« Reply #6 on: April 02, 2020, 09:12:48 pm »
Continuation on a parallel subject:

To save the same to the clipboard:
- Do I loose image quality by assigning the jpg to the clipboard or is it better to use the bitmap?
- To make it MacOS compatible do I need to transform it to tiff myself or the clipbrd unit takes care of this?

Thanks
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: [SOLVED] Looking for advice how to print 2 charts together
« Reply #7 on: April 02, 2020, 10:35:59 pm »
- Do I loose image quality by assigning the jpg to the clipboard or is it better to use the bitmap?
Yes. Can't you use a png? It is lossless but provides some compression, too.

- To make it MacOS compatible do I need to transform it to tiff myself or the clipbrd unit takes care of this?
In macOS, I created a chart and copied it to the clipboard with Chart.CopyToClipboardBitmap (as a TBitmap). Then I pasted the clipboard into a TImage on the same form (Image.Picture.Assign(Clipboard)); it works. And I could also paste the image to the Notes app. Since TAChart does not do any format conversion when copying to the clipboard, I think there is no compatibility issue. But I may be wrong - I am not very experienced with macOS.

AL

  • Sr. Member
  • ****
  • Posts: 261
Re: [SOLVED] Looking for advice how to print 2 charts together
« Reply #8 on: April 03, 2020, 12:24:15 am »
Thank you.
My question was more copy the 2 charts to the clipboard just like you did for saving to Jpeg and Printing. Once I get the 2 charts in a jpeg how do I transfer this to the Clipboard.


If I do clipboard.assign(jpg) in your example it does not give an error but produce no result.

I tried to copy the 2 img to a new trasterimg instead of a jpg but this produce a runtine error. 
I was trying  to follow the same logic but replace jpg with the NewImg

NewImg.Canvas.Draw(0, 0, img); 
« Last Edit: April 03, 2020, 12:28:53 am by AL »
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: [SOLVED] Looking for advice how to print 2 charts together
« Reply #9 on: April 03, 2020, 12:57:53 am »
Please try the attached demo. The step combining the two charts to a common bitmap is extracted and available as separate function. It creates a raster image with the class given as parameter. It uses standard clipboard methods for clipboard access.

Please test. It is working on Windows, but I could not test on macOS because data transfer to the mac-VM seems to be broken ATM.

AL

  • Sr. Member
  • ****
  • Posts: 261
Re: [SOLVED] Looking for advice how to print 2 charts together
« Reply #10 on: April 03, 2020, 02:04:02 am »
WP you are my hero!

Works no problem.  I tried it in Mojave and the clipboard and Jpeg are OK
                             I could not try the printer because of the VM printer is not setup
                             but I have found a Mac owner to try my program, so I will confirm shortly.

Thank you very much
Laz 3.1, fpc 3.2.2, Win10
Laz 3.1  fpc 3.2.2, MacOS Monterey running on VMWare/Win 10
Laz 3.1  fpc 3.2.2 Ubuntu 20.04

 

TinyPortal © 2005-2018