Jump to content

How to transfer the image on a TUniChart to a TImage or TCanvas


Woutero

Recommended Posts

Sherzod
How do I transfer the image on a TUniChart to a BitMap so that I can save the chart to a MemoryStream to be loaded by an TImage?

Alternatively, how do I transfer the image on a UniChart  to a TCanvas?

In Delphi it would be as below, but UniChart does not have Draw as a member:


ThisChart:TChart; 
BitMap : TBitMap;
MyStream :TMemoryStream;
X,Y : integer;


Bitmap:=TBitmap.Create;
  try
    X := ThisChart.Width;
    Y := ThisChart.Height;
    Bitmap.Height:=Y;
    Bitmap.Width:=X;
    ThisChart.Draw(Bitmap.Canvas, Rect(0, 0, X, Y));
    MyStream:=TMemoryStream.Create;
    Bitmap.SaveToStream(MyStream);
  finally
    Bitmap.Free;
  end;

Image1.Picture.Bitmap.LoadFromStream( MyStream );


 

Link to comment
Share on other sites

  • Woutero changed the title to How to transfer the image on a TUniChart to a TUniImage
  • Woutero changed the title to How to transfer the image on a TUniChart to a TImage or TCanvas

Thank you Sherzod.
Though when I do

 UniSession.AddJS('Form1.Chart1.chart.save({type: ''image/png''})');

then, I get an error: "Form1.Chart1.chart.save is not a function"

My MainForm is named Form1 and my UniChart is named Chart1.
I'm using UniGUI Complete Professional V1.90.0.1560 

Link to comment
Share on other sites

@Woutero

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniChart1.SaveImage;
end;

procedure TMainForm.UniChart1ChartImage(Sender: TUniCustomChart; Image: TGraphic);
var
  fName : string;
begin
  fName := UniServerModule.LocalCachePath + 'Chart.png';
  Image.SaveToFile(fName);
  UniSession.SendFile(fName);
end;

 

Link to comment
Share on other sites

Hi Sherzod
I have a problem.
The UniChart1.SaveImage will eventually trigger the UniChart1ChartImage event.
However, I need the UniChart1ChartImage event to fire earlier so that I can use the result still within the
UniButton1Click procedure.

Example:  UniButton1Click calls DoSomething


procedure DoSomething;
begin
  UniChart1.SaveImage;
  // I want to use the png file created by UniChart1ChartImage here.
  // but the UniChartImage event only triggers after DoSomething procedure has finished.

  // So I need to call  UniChart1.SaveImage earlier, before I execute DoSomething

end;

So I'm looking for an event that will trigger every time just after a Chart has been drawn/updated?
Which event fires just after a UniChart has been drawn/updated?
Then I can use that event to call UniChart1.SaveImage to create the png file upfront before DoSomething is executed.

Link to comment
Share on other sites

22 hours ago, Woutero said:

So I'm looking for an event that will trigger every time just after a Chart has been drawn/updated?

If I understand you correctly, you can use these client events I think:

image.png.5c7fa69259805d09d39eb8f483e13a02.png

Link to comment
Share on other sites

Thanks Sherzod

So how do I execute

UniChart1.SaveImage;

from within the chart.redraw event?

Remember that when I use

UniSession.AddJS('Form1.Chart1.chart.save({type: ''image/png''})');

then, I get an error: "Form1.Chart1.chart.save is not a function"

Link to comment
Share on other sites

Thanks for your continued help Sherzod. I really appreciate it.

As I understand, UniChart1.SaveImage; triggers the event: 

UniChart1ChartImage(Sender: TUniCustomChart; Image: TGraphic)

All I want to do is to trigger this event on request. Is that possible?   

Is there any UniSession.AddJS() code that can do this?

Link to comment
Share on other sites

Sherzod
I am able to trigger the UniChart1ChartImage event on demand by using a UniTimer set to RunOnce.
Every time I update a chart, I enable this Timer which then calls  UniChart.SaveImage
which then triggers the ChartImage event.

Link to comment
Share on other sites

On 4/13/2024 at 4:55 PM, Sherzod said:

@Woutero

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  UniChart1.SaveImage;
end;

procedure TMainForm.UniChart1ChartImage(Sender: TUniCustomChart; Image: TGraphic);
var
  fName : string;
begin
  fName := UniServerModule.LocalCachePath + 'Chart.png';
  Image.SaveToFile(fName);
  UniSession.SendFile(fName);
end;

 

Sherzod

When using Image.SaveToFile(FileName), how does one set the file type?

For example, if I want to use a jpg, how do I set JPG, or does UniGUI automatically look at the extension of the FileName?

Link to comment
Share on other sites

Also, when using Image.SaveToStream() within

procedure TMainForm.UniChart1ChartImage(Sender: TUniCustomChart; Image: TGraphic);

 what is the default image type, or how does one set it?  

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...