Teechart: End labels on logarithmic x-axis - teechart

If I have a logarithmic x-axis with value from 10 to 400 it displays 10 and 100, but I also want it to display the end value 400.
Another problem is if the values goes from 11 to 400, it only displays label at 100. Here I want to display 11, 100 and 400.
Anyone know what axis/label property to set for this?

To draw such labels you should use custom labels.
I'll assume you are using TeeChart VCL and I'll show code in Delphi, but it would be similar with TeeChart .NET, TeeChart ActiveX or TeeChart Java.
If I have a logarithmic x-axis with value from 10 to 400 it displays 10 and 100, but I also want it to display the end value 400.
You could do it as follows:
uses Series, Math;
procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
Chart1.View3D:=False;
Chart1.Legend.Hide;
Chart1.Walls.Hide;
Chart1.Gradient.Visible:=False;
Chart1.Color:=clWhite;
Chart1.Axes.Bottom.Logarithmic:=True;
with Chart1.AddSeries(TFastLineSeries) as TFastLineSeries do
begin
for i:=10 to 400 do
AddXY(i, sin(i/100));
end;
with Chart1.Axes.Bottom.Items do
begin
Clear;
Add(10, '10');
Add(100, '100');
Add(400, '400');
end;
end;
Another problem is if the values goes from 11 to 400, it only displays label at 100. Here I want to display 11, 100 and 400.
You could do it as follows:
uses Series, Math;
procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
Chart1.View3D:=False;
Chart1.Legend.Hide;
Chart1.Walls.Hide;
Chart1.Gradient.Visible:=False;
Chart1.Color:=clWhite;
Chart1.Axes.Bottom.Logarithmic:=True;
with Chart1.AddSeries(TFastLineSeries) as TFastLineSeries do
begin
for i:=11 to 400 do
AddXY(i, sin(i/100));
end;
with Chart1.Axes.Bottom.Items do
begin
Clear;
Add(11, '11');
Add(100, '100');
Add(400, '400');
end;
end;

Related

Do an IF by DBGrid Column Title

I want to create a different Popup for each column. Since the columns order can be changed I need to identify that by the column title but I have not found a solution.
These are two method I have applied without success.
procedure TForm2.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ACol, ARow: Integer;
begin
if Y < DBGrid1.DefaultRowHeight then
begin
(Sender as TDBGrid).MouseToCell(X, Y, ACol, ARow);
if Button = mbRight then
begin
if DBGrid1.SelectedColumn.FieldName = 'Title1' then
BEGIN
ShowMessage('Title1'+ IntToStr(ACol));
end;
if DBGrid1.SelectedColumn.FieldName = 'Title2' then
BEGIN
ShowMessage('Title2'+ IntToStr(ACol));
end;
end;
end;
end;
It is not functional because it identifies the columns by id and not the name so if the user change the columns order it will not work fine.
Also this faily code
procedure TForm2.Button2Click(Sender: TObject);
var
i: Integer;
CaptionText: string;
begin
for i := 0 to DBGrid1.Columns.Count - 1 do
case DBGrid1.Columns[i].FieldName of
'TEST':
begin
DBGrid1.Columns[i].Title.Caption := 'REPLACE TEXT';
end;
end;
end;
It simply replace all titles in a click.
What I'm looking to do is to create an IF like this that handle on right clicking column title:
if selected column name = 'test' then
begin
showmessage('You have selected test column');
end;
I'll use the if to create dynamic popup to apply filters.
The code below is an event handler for a TDBGrid's MouseMove event which
displays the Caption of the Title of the column the mouse is over on the caption of the form.
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
Col,
Row : Integer;
begin
Col := DBGrid1.MouseCoord(X, Y).X;
Row := DBGrid1.MouseCoord(X, Y).Y;
if (dgIndicator in DBGrid1.Options) then
Dec(Col);
if (Col >= 0) and (Col < DBGrid1.Columns.Count) then
Caption := DBGrid1.Columns[Col].Title.Caption
else
Caption := '';
end;
Note that the
if (dgIndicator in DBGrid1.Options) then
Dec(Col);
is to adjust the behaviour for correct operation if the dgIndicator option is turned off.
Obviously, instead of
Caption := DBGrid1.Columns[Col].Title.Caption
you could do
MenuItem.Caption := DBGrid1.Columns[Col].Title.Caption
to copy the column title to a menuitem's caption. I think you probably don't need to do any comparison of the Title.Caption's value with any hard-coded constants, but obviously that's your choice.
Btw, if you would rather access the name of the dataset field which is supplying the displayed values for the Column's contents, you can read the Column's FieldName property.

Writing datas to memobox from .txt file using Lazarus freepascal?

I've got a project at schools which requires to write datas from a .txt file to a "memobox" in Lazarus freepascal.
There are datas in order like this.
Budapest tomato 23
Dublin tv 45
Rosslare projector 43
etc.
I have to read these datas from a .txt file and then write them into a memobox in Lazarus freepascal.
If I am not mistaken I have already copyed the datas from the .txt file but I have no idea how to write them.
I've already written this code:
type
cityname:integer;
product:string;
quantity:integer;
var
Form1: TForm1;
ceg:array[1..5] of rektip;
db:integer;
implementation
procedure TForm1.Button1Click(Sender: TObject);
var f:textfile; i:integer; a:char;
begin
assignfile(f,'termek.txt');
reset(f);
readln(f,db);
While not eof(f) do
begin
readln(f,ceg[i].varosnev,a,ceg[i].termek,a,ceg[i].darabszam);
end;
db:=1;
closefile(f);
end;
procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
For i:=1 to db do
Memo1.lines.add(ceg[i].varosnev,ceg[i].termek,IntToStr(ceg[i].darabszam));
end;
end;
I would like to know how to fix it.
The code you posted is incomplete, so I assume:
type
rektip = record
varosnev: string;
termek: string;
darabszam: Integer;
end;
There is a lot wrong with your approach:
readln(f,db);
While not eof(f) do
begin
readln(f,ceg[i].varosnev,a,ceg[i].termek,a,ceg[i].darabszam);
end;
db:=1;
closefile(f);
end;
You are not initializing nor updating i, so you are reading all data into the same record, and you don't even know which one (i could be 100 and you'd be writing the data to somewhere unknown -- there is no ceg[100]). That results in undefined behaviour (and that can be a crash too).
Do something like:
var
ceg: array of rektip;
...
begin
AssignFile(f, 'termek.txt');
Reset(f);
Readln(f, db);
if db = 0 then
Exit;
SetLength(ceg, db);
i := 0;
while not eof(f) do
begin
Readln(f, ceg[i].varosnev, ceg[i].termek, ceg[i].darabszam);
Inc(i);
if i > High(ceg) then
Break;
end;
SetLength(ceg, i); // remove any empty slots.
CloseFile(f);
end;
Now you can put them into the TMemo:
for i := Low(ceg) to High(ceg) do
begin
Memo1.Lines.Add(Format('%s %s %d', [ceg[i].varosnev, ceg[i].termek, ceg[i].darabszam]));
end;
Note that the code above, reading from the file, assumes the file looks like:
3
Budapest tomato 23
Dublin tv 45
Rosslare projector 43
i.e. each "record" is on a line of its own and the first line contains the number of records.

Stacked Chart not stacking into a single bar by date

I have a dataset which returns data like below
Date, Area, Sales
2017-06-01 00:00:00.000 Canteen 435.29
2017-06-01 00:00:00.000 Gym 26
2017-06-01 00:00:00.000 Nails 75
2017-06-01 00:00:00.000 Uncategorized 482.5
I am trying to create a stacked bar chart using this data
The number of series will be different
I have the logic below to create the series, but I am not sure how to add each value, e.g. I need the X Axis to be by date and have 2 bars. Each bar is then stacked with 3 series, as per the above data.
while not tblSalesBreakdownByDate.Eof do
begin
nIndex := objList.IndexOf(tblSalesBreakdownByDateCategory.AsString);
if nIndex = -1 then
begin
objSeries := TBarSeries.Create(Self);
objSeries.MultiBar := TMultiBar.mbStacked;
objSeries.Title := tblSalesBreakdownByDateCategory.AsString;
chrtBreakdownByDate.AddSeries(objSeries);
objList.AddObject(objSeries.Title, objSeries)
end
else
objSeries := objList.Objects[nIndex];
objSeries.Add(tblSalesBreakdownByDateTotalSales.AsFloat, tblSalesBreakdownByDateTransactionDate.AsString);
tblSalesBreakdownByDate.Next;
end;
When this is rendered, instead of 1 bar which is stacked, I get 3 bars
How do I get this in 1 bar stacked for the date?
Is there something special I need to with the Axis?
Cheers
Paul
This gives me one single stacked column:
uses Series;
procedure TForm1.FormCreate(Sender: TObject);
begin
with Chart1.AddSeries(TBarSeries) as TBarSeries do
begin
MultiBar:=mbStacked;
Marks.Hide;
Title:='Canteen';
Add(435, '2017-06-01');
end;
with Chart1.AddSeries(TBarSeries) as TBarSeries do
begin
MultiBar:=mbStacked;
Marks.Hide;
Title:='Gym';
Add(25, '2017-06-01');
end;
with Chart1.AddSeries(TBarSeries) as TBarSeries do
begin
MultiBar:=mbStacked;
Marks.Hide;
Title:='Nails';
Add(95, '2017-06-01');
end;
with Chart1.AddSeries(TBarSeries) as TBarSeries do
begin
MultiBar:=mbStacked;
Marks.Hide;
Title:='Uncategorized';
Add(455, '2017-06-01');
end;
end;

How to create a pan from composite image in Delphi

I'm kind of new to delphi graphics methods and I'm stucked at creating a ... viewport , thats how I call it while i was doing it for a project. I'm sorry I can't provide any code for it but I'm stuck at the logic part , searching google pointed me to some OnPaint , Draw methods. But those are not what I'm trying to accomplish, since I have , for example:
A 1600x1000 background image anchored to the client's top/bottom/right and left.
Multiple TImage elements placed at set x/y coords.
A "hotspot" like a map element in HTML where I can set the clickable areas (for the images i'm placing at step 2)
No zoom needed.
And the most important thing, while the background is dragged, those TImages placed on top of the background need to be dragged too.
My logic (in HTML/jQuery) was to create a #viewportBinder (which was the div i was dragging, transparent bg), followed by another div inside it called #viewtown (1600x1000, the background) which contains the divs (those TImages) placed at set coordinates in CSS.
So when I am dragging the viewportBinder, jQuery sets the new x/y on the #viewport. Implicitly, the divs (TImages) inside the #viewport are moving because the parent was positioned relative.
Does anybody have any experience with this kind of project ? Any snippet of code ?
To be more specific i'll give you my html example of what i accomplised and what i want to port into Delphi code: http://www.youtube.com/watch?v=9iYqzvZFnGA
Sorry if i'm not clear enough, i have no starting point since I have no experience with this in delphi at all. (using RAD Studio 2010)
A very short example how it could be realized in an easy way.
You would use a Paintbox for painting, 1 Backimage, an array of Records with info and transparent pngimages.
Canvas can be manipulated in offset/zoom/rotation.
Moving and hitdetection would happen in mousedown and mousemove.
It's not complete, but might give you an idea how it could be done.
[delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls,PNGImage, StdCtrls;
type
TBuilding=Record // record for building informations
Pos:TPoint;
PNGImage:TPngImage;
// what ever needed
End;
TBuildingArray=Array of TBuilding; // array of buildings
TForm1 = class(TForm)
PaintBox1: TPaintBox;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private-Deklarationen }
FXoffs,FYOffs,FZoom:Double; // offset and zoom for painting
FMouseDownPoint:TPoint;
FBackGroundPNG:TPNGImage;
FBuildingArray:TBuildingArray;
procedure Check4Hit(X, Y: Integer);
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
uses Math;
{$R *.dfm}
Procedure SetCanvasZoomAndRotation(ACanvas:TCanvas;Zoom:Double;Angle:Double;CenterpointX,CenterpointY:Double);
var
form : tagXFORM;
Winkel:Double;
begin
Winkel := DegToRad(Angle);
SetGraphicsMode(ACanvas.Handle, GM_ADVANCED);
SetMapMode(ACanvas.Handle,MM_ANISOTROPIC);
form.eM11 := Zoom * cos( Winkel);
form.eM12 := Zoom *Sin( Winkel) ;
form.eM21 := Zoom * (-sin( Winkel));
form.eM22 := Zoom * cos( Winkel) ;
form.eDx := CenterpointX;
form.eDy := CenterpointY;
SetWorldTransform(ACanvas.Handle,form);
end;
Procedure ResetCanvas(ACanvas:TCanvas);
begin
SetCanvasZoomAndRotation(ACanvas , 1, 0, 0,0);
end;
procedure TForm1.FormCreate(Sender: TObject);
var
Path:String;
i:Integer;
begin
FZoom := 1;
DoubleBuffered := true;
Path := ExtractFilePath(Paramstr(0));
FBackGroundPNG:=TPNGImage.Create;
FBackGroundPNG.LoadFromFile(Path + 'infect.png');
SetLength(FBuildingArray,3);
for I := 0 to High(FBuildingArray) do
begin
FBuildingArray[i].PNGImage := TPngImage.Create;
FBuildingArray[i].PNGImage.LoadFromFile(Path + Format('B%d.png',[i]));
FBuildingArray[i].Pos.X := I * 300;
FBuildingArray[i].Pos.Y := Random(1000);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
i:Integer;
begin
for I := 0 to High(FBuildingArray) do
begin
FBuildingArray[i].PNGImage.Free;
end;
FBackGroundPNG.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if FZoom=0.5 then FZoom := 1 else FZoom := 0.5;
PaintBox1.Invalidate;
end;
procedure TForm1.Check4Hit(X,Y:Integer);
var
i,Index:Integer;
R:TRect;
P:TPoint;
begin
index := -1;
for I := 0 to High(FBuildingArray) do
begin
R := Rect(FBuildingArray[i].Pos.X,FBuildingArray[i].Pos.Y
,FBuildingArray[i].Pos.X + FBuildingArray[i].PNGImage.Width
,FBuildingArray[i].Pos.Y + FBuildingArray[i].PNGImage.Height);
P := Point(Round((x - FXOffs)/FZoom) ,Round((y - FYOffs)/FZoom));
if PtInRect(R,P) then Index := i;
end;
if index > -1 then
begin
Caption := Format('Last hit %d',[index]);
end
else Caption := 'No Hit';
end;
procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Check4Hit(X,Y);
FMouseDownPoint.X := X;
FMouseDownPoint.Y := Y;
end;
procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ssLeft in Shift then
begin
FXoffs := -( FMouseDownPoint.X - X) ;
FYoffs := -( FMouseDownPoint.Y - Y) ;
if FXoffs>0 then FXoffs := 0;
if FYoffs>0 then FYoffs := 0;
PaintBox1.Invalidate;
end;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
i:Integer;
begin
SetCanvasZoomAndRotation(PaintBox1.Canvas,FZoom,0,FXoffs,FYOffs);
PaintBox1.Canvas.Draw(0,0,FBackGroundPNG);
for I := 0 to High(FBuildingArray) do
begin
PaintBox1.Canvas.Draw(FBuildingArray[i].Pos.X,FBuildingArray[i].Pos.Y,FBuildingArray[i].PNGImage);
end;
end;
end.
[/delphi]
Sorry, but for last several years i working with Lazarus instead of Delphi. But tis article will be informative: http://wiki.lazarus.freepascal.org/Developing_with_Graphics#Create_a_custom_control_which_draws_itself
About relative coordinates nothing to say - it is simple.
About dragging: A long time ago in a galaxy far, far away.. that was something like:
// To start dragging
procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
// To stop dragging
procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
// To perform dragging
procedure WMMouseMove(var Message: TWMMouseMove); message WM_MOUSEMOVE;

Image Grid by NGLN

I've found this great component and installed it, it's running great, but I have a slight problem with it. Which unfortunately I don't know how to do it myself.
Can someone help me add a new feature to this component . That would allow it take Images from ImageList ? I would fill up ImageList dynamicaly during my execution time.
Right now I'm doing the following to show a preview of the TILE :
procedure TTools.Preview_ImageExecute(Sender: TObject);
var image_temp : TBitmap;
begin
image_temp := TBitmap.Create;
LoadBitMap(ComboBox1.Text,image_temp,Main.ASDb1);
Image1.Picture.Bitmap:=image_temp;
image_temp.Free;
end;
Would like to use this somehow with the Image Grid... it should somehow allow me to load all my tiles.. I would use a For loop to fill it up...
Meanwhile I was playing with ListBox, and managed to do this :
procedure TTools.Lst1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var CenterText : integer;
begin
Lst1.Canvas.FillRect(Rect);
Il1.Draw(lst1.Canvas,rect.Left +4, rect.Top +4, Index);
Centertext := (rect.Bottom - rect.Top -lst1.Canvas.TextHeight(text)) div 2;
Lst1.Canvas.TextOut(rect.left + il1.width + 8, rect.Top + CenterText, lst1.Items.Strings[index]);
end;
procedure TTools.Lst1MeasureItem(Control: TWinControl; Index: Integer;
var Height: Integer);
begin
Height := IL1.Height+4;
end;
procedure TTools.Button4Click(Sender: TObject);
var i : integer;
image_temp : TBitmap;
begin
image_temp := TBitmap.Create;
for i:=0 to Main.Images.Count-1 do
begin
Lst1.Items.Add(Main.Images.Item[i].Name);
LoadBitMap(Main.Images.Item[i].Name,image_temp,Main.ASDb1);
IL1.AddMasked(image_temp, clNone);
end;
Image_temp.Free;
end;
This works, if I have 0 Columns, but I cant get it working with say 4 columns , can someone help ?
Greetings
Robert
Never Mind ... I solved it like this :
procedure TTools.Button4Click(Sender: TObject);
var i : integer;
image_temp : TBitmap;
begin
image_temp := TBitmap.Create;
for i:=0 to Main.Images.Count-1 do
begin
LoadBitMap(Main.Images.Item[i].Name,image_temp,Main.ASDb1);
ListView1.Items.Add.Caption:=Main.Images.Item[i].Name;
ListView1.Items.Item[i].ImageIndex:=i;
IL1.AddMasked(image_temp, clNone);
end;
Image_temp.Free;
end;
This load's the image names as well as the images from my Asphyre Image list...

Resources