Page 1 of 1

Question on Templates

Posted: Mon Nov 06, 2006 4:30 pm
by 9242408
Hi,

I have a scrollbox with about 14 charts in it now. I have added some code to add and remove charts, so the end user has some control. Id like to enable the user to set up their own editing for each chart. Is there some way to call the chart editor without going through each chart and coding it? Also some users may only need 12 of the 14. How does it work to save those charts as templates and then later be able to load them back into the scrollbox?

Also any ideas to allow the user to set somehow the series data to a specific column from a text file or string grid?

Thanks,

Tom

Posted: Mon Nov 06, 2006 5:21 pm
by narcis
Hi Tom,
Is there some way to call the chart editor without going through each chart and coding it?


You can use a TeeChart event and use it for all your charts like the code below. Also notice that you can also assign this event at design-time.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart2.OnDblClick:=Chart1DblClick;
end;

procedure TForm1.Chart1DblClick(Sender: TObject);
begin
  ChartEditor1.Chart:=Sender as TChart;
  ChartEditor1.Execute;
end;
Also some users may only need 12 of the 14. How does it work to save those charts as templates and then later be able to load them back into the scrollbox?
Please read Tutorial 12 - Exporting and Importing Charts for this. You'll find the tutorials at TeeChart's program group created by the binary installer.
Also any ideas to allow the user to set somehow the series data to a specific column from a text file or string grid?


Yes, the users can do it using the chart editor as shown in Tutorial 8 - Database access. You'll find examples of populating a chart from a text file in the features demo at All Features\Welcome!\Components\Text Source. Double click the yellow memo in the example to execute the chart editor and edit series datasource. The features demo can also be found at TeeChart's program group.

Posted: Mon Nov 06, 2006 6:03 pm
by 9242408
[/quote]You can use a TeeChart event and use it for all your charts like the code below. Also notice that you can also assign this event at design-time.

Code: Select all

procedure TForm1.FormCreate(Sender: TObject);
begin
  Chart2.OnDblClick:=Chart1DblClick;
end;

procedure TForm1.Chart1DblClick(Sender: TObject);
begin
  ChartEditor1.Chart:=Sender as TChart;
  ChartEditor1.Execute;
end;
[/quote]

This sounds good, but I never know for sure how many charts I have. How can I code Chart1 or Chart2? The code I use to create a new one at runtime, I just keep an integer of how many I have plus the new one added. I dont see how I can say Chart(i).OnDblClick?

I know I can set this for each chart thats there when the form loads, but when a new one arrives I am not sure how to set that?

Thanks for the tutorials. The only problem in my situation is that it non db related. Which poses another issue because I want to set the series names for each chart through the chart editor so its available, but I imagine I would have to set that for each chart to?

Thanks,

Tom

Posted: Tue Nov 07, 2006 9:26 am
by narcis
Hi Tom,
This sounds good, but I never know for sure how many charts I have. How can I code Chart1 or Chart2? The code I use to create a new one at runtime, I just keep an integer of how many I have plus the new one added. I dont see how I can say Chart(i).OnDblClick?

I know I can set this for each chart thats there when the form loads, but when a new one arrives I am not sure how to set that?
You can add a line doing this in your run-time chart creation routine.
Thanks for the tutorials. The only problem in my situation is that it non db related. Which poses another issue because I want to set the series names for each chart through the chart editor so its available, but I imagine I would have to set that for each chart to?


Yes, you'll have to do it for each chart. This could also be added to the chart creation/initialization routine.

Posted: Tue Nov 07, 2006 11:57 am
by 9242408
Narcis,

Thanks, couple more questions though...
You can add a line doing this in your run-time chart creation routine.
How? I really have no idea how or where to implement such.

I kind of change things a bit and am allowing the user to designate the number of charts they need. Once thats loaded then i set all the series with headers from a text file I have them open as well. Everything works great so.
But I am unsure how to implement calling the chart editor from a chart? Especially if I dont know what chart they are wanting to edit.

Heres my code for anyone interested.

Code: Select all

procedure TForm1.AdvToolBarButton12Click(Sender: TObject);
Var
i,mycol,k: integer;
f : TForm5;
MyChart : TChart;
lastchart,c,seriesnum: integer;
tmpLineSeries:TFastlineSeries;
begin
f := TForm5.Create(self);
lastchart := 0;
c := 1;
seriesnum := 0;
if f.ShowModal = mrOk then
begin
//First get the channel list to load up the series names
  datagrid2.Delimiter := Chr(9);
  setchannelsDialog.Title := 'First we need the channels from a Track Test File.';
if not SetChannelsDialog.Execute then
begin
 abort;
end
 else
 datagrid2.LoadFromCSV(SetChannelsDialog.FileName);
 //Load up signal names, first find startrow
  begin
  i:=0;
  s := datagrid2.Cells[10,i];
  repeat
    begin
      s := datagrid2.Cells[10,i];
      i := i + 1;
      if s <> '' then
        startrow := i;
      end
  until s <> '';
  end;

  //Now we can load the series based off from our channels we have
  for i := 1 to f.numcharts.AsInteger do
   begin
     MyChart:=TChart.Create(Self);
     MyChart.Parent := JvScrollBox1;
     MyChart.Height := 246;
     MyChart.Width := 1000;
     MyChart.Left := 0;
     MyChart.Top := lastchart;
     MyChart.View3D := False;
     MyChart.BackColor := ClWhite;
     MyChart.Color := ClWhite;
     MyChart.Title.Caption := 'TChart';
     MyChart.Name := 'Chart'+IntToStr(i);
     with mychart.Border do
     begin
     visible := true;
     Color:=clBlack;
     end;
     comboflat1.Items.Add(mychart.Name);
     lastchart := MyChart.Height*i+c;
     Inc(c);
     //Now load the series to our chart from the channel list we got
     for mycol := 0 to datagrid2.ColCount - 1 do
         begin
           s := datagrid2.Cells[mycol,startrow-1];
           tmpLineSeries:=TFastlineSeries.Create(self);
           mychart.AddSeries(tmpLineSeries);
           tmpLineSeries.FillSampleValues(10);
           mychart.Series[seriesnum].Title := s;
           //mychart.Series[seriesnum].Name := s;
           Inc(seriesnum);
         end;
         seriesnum := 0;
         mychart.Legend.LegendStyle := lsSeries;
         mychart.Legend.Alignment := laTop;
         mychart.Legend.Shadow.HorizSize := 0;
         mychart.Legend.Shadow.VertSize := 0;
   end;
end;
 datagrid2.ClearRows(1,datagrid2.TotalRowCount);
 datagrid2.RowCount := 2;

end;
Any thought or ideas?

Thanks,

Tom

Posted: Tue Nov 07, 2006 12:36 pm
by narcis
Hi Tom,

Yes, this is pretty easy, you need to do something like the line I added below when you initialize your chart.

Code: Select all

  //Now we can load the series based off from our channels we have
  for i := 1 to f.numcharts.AsInteger do
   begin
     MyChart:=TChart.Create(Self);
     MyChart.Parent := JvScrollBox1;
     MyChart.Height := 246;
     MyChart.Width := 1000
     ...
     //Add this line to add the event to the chart
     MyChart.OnDblClick:=Chart1DblClick; 

Posted: Tue Nov 07, 2006 12:43 pm
by 9242408
narcis wrote:Hi Tom,

Yes, this is pretty easy, you need to do something like the line I added below when you initialize your chart.

Code: Select all

  //Now we can load the series based off from our channels we have
  for i := 1 to f.numcharts.AsInteger do
   begin
     MyChart:=TChart.Create(Self);
     MyChart.Parent := JvScrollBox1;
     MyChart.Height := 246;
     MyChart.Width := 1000
     ...
     //Add this line to add the event to the chart
     MyChart.OnDblClick:=Chart1DblClick; 
Yes this would seem easy, however chart1dblclick will not work because I do not have a chart1 on the form yet? Right away it shows in the ide that its not defined yet. What do I do about that?

Also, I load a flatcombo with the charts I am creating so hopefully the user could select the chart and press a button to edit. I have tried this but it throws an exception. Heres the code trying to accomplish this:

Code: Select all

procedure TForm1.AdvGlowButton1Click(Sender: TObject);
Var
mychart: TChart;
begin
mychart.Name := comboflat1.Text;
charteditor1.Chart := mychart;
charteditor1.Execute;
end;

Thanks,

Tom

Posted: Tue Nov 07, 2006 2:47 pm
by narcis
Hi Tom,
Yes this would seem easy, however chart1dblclick will not work because I do not have a chart1 on the form yet? Right away it shows in the ide that its not defined yet. What do I do about that?


The easiest way to achieve that I can think of is adding a chart at design-time, generate the event with the object inspector so that the method for it is generated and then remove the chart.
Also, I load a flatcombo with the charts I am creating so hopefully the user could select the chart and press a button to edit. I have tried this but it throws an exception. Heres the code trying to accomplish this:

Code:
procedure TForm1.AdvGlowButton1Click(Sender: TObject);
Var
mychart: TChart;
begin
mychart.Name := comboflat1.Text;
charteditor1.Chart := mychart;
charteditor1.Execute;
end;
This doesn't work because the mychart variable doesn't have any valid chart assigned. If you created the charts sequentially you could call them using the comboflat index, something like:

if comboflat1.SelectedIndex=1 then charteditor1.Chart := Chart1;
if comboflat1.SelectedIndex=2 then charteditor1.Chart := Chart2;
...

Or may be better using a switch statement :wink:

Posted: Tue Nov 07, 2006 4:05 pm
by 9242408
The easiest way to achieve that I can think of is adding a chart at design-time, generate the event with the object inspector so that the method for it is generated and then remove the chart.
I tried this to no avail, once I delete that chart Im no longer holding that procedure. Can I have a procedure that uses that same stuff but the sender would be mychart?

Seems like there would be some way around it?

The code you showed for setting the comboflat index to chart1 and so on wont work either because I dont have a chart1 identified.

Posted: Wed Nov 08, 2006 8:32 am
by narcis
Hi Tom,
I tried this to no avail, once I delete that chart Im no longer holding that procedure. Can I have a procedure that uses that same stuff but the sender would be mychart?

Seems like there would be some way around it?
Yes, it works fine for me here dropping a TChart in the form, generating the event, adding a code line to it (even if its a comment line), then remove the chart and the IDE won't remove the event as it has code implemented. Finally use something like this:

Code: Select all

procedure TForm8.Chart1DblClick(Sender: TObject);
begin
  ChartEditor1.Chart:=Sender as TChart;
  ChartEditor1.Execute;
end;

procedure TForm8.FormCreate(Sender: TObject);
var MyChart: TChart;
begin
  MyChart:=TChart.Create(self);
  MyChart.Parent:=self;
  MyChart.OnDblClick:=Chart1DblClick;
end;
The code you showed for setting the comboflat index to chart1 and so on wont work either because I dont have a chart1 identified.
I see, the only way I can come around for now is using an array to store your charts and do something like this:

charteditor1.Chart := ChartArray[comboflat1.SelectedIndex] as TChart;

Posted: Wed Nov 08, 2006 11:00 am
by 9242408
Thanks, I ultimately did this, except I left the chart on the form and made it invisible. Works great actually. I originally didnt put any code in the procedure before deleting the chart, thats why it didnt work.

Ive used several charting tools in VB.NEt and you guys are by far the most advanced and best Ive seen yet.


Thanks,

Tom

Posted: Wed Nov 08, 2006 11:17 am
by narcis
Hi Tom,

Thank you very much for your words. I'm glad to hear your problems are solved.